]> code.delx.au - gnu-emacs/blob - lisp/progmodes/verilog-mode.el
Sync with upstream verilog-mode revision 3cd8144
[gnu-emacs] / lisp / progmodes / verilog-mode.el
1 ;;; verilog-mode.el --- major mode for editing verilog source in Emacs
2
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
4
5 ;; Author: Michael McNamara <mac@verilog.com>
6 ;; Wilson Snyder <wsnyder@wsnyder.org>
7 ;; http://www.verilog.com
8 ;; http://www.veripool.org
9 ;; Created: 3 Jan 1996
10 ;; Keywords: languages
11
12 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
13 ;; file on 19/3/2008, and the maintainer agreed that when a bug is
14 ;; filed in the Emacs bug reporting system against this file, a copy
15 ;; of the bug report be sent to the maintainer's email address.
16
17 ;; This code supports Emacs 21.1 and later
18 ;; And XEmacs 21.1 and later
19 ;; Please do not make changes that break Emacs 21. Thanks!
20 ;;
21 ;;
22
23 ;; This file is part of GNU Emacs.
24
25 ;; GNU Emacs is free software: you can redistribute it and/or modify
26 ;; it under the terms of the GNU General Public License as published by
27 ;; the Free Software Foundation, either version 3 of the License, or
28 ;; (at your option) any later version.
29
30 ;; GNU Emacs is distributed in the hope that it will be useful,
31 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 ;; GNU General Public License for more details.
34
35 ;; You should have received a copy of the GNU General Public License
36 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
37
38 ;;; Commentary:
39
40 ;; USAGE
41 ;; =====
42
43 ;; A major mode for editing Verilog and SystemVerilog HDL source code (IEEE
44 ;; 1364-2005 and IEEE 1800-2012 standards). When you have entered Verilog
45 ;; mode, you may get more info by pressing C-h m. You may also get online
46 ;; help describing various functions by: C-h f <Name of function you want
47 ;; described>
48
49 ;; KNOWN BUGS / BUG REPORTS
50 ;; =======================
51
52 ;; SystemVerilog is a rapidly evolving language, and hence this mode is
53 ;; under continuous development. Please report any issues to the issue
54 ;; tracker at
55 ;;
56 ;; http://www.veripool.org/verilog-mode
57 ;;
58 ;; Please use verilog-submit-bug-report to submit a report; type C-c
59 ;; C-b to invoke this and as a result we will have a much easier time
60 ;; of reproducing the bug you find, and hence fixing it.
61
62 ;; INSTALLING THE MODE
63 ;; ===================
64
65 ;; An older version of this mode may be already installed as a part of
66 ;; your environment, and one method of updating would be to update
67 ;; your Emacs environment. Sometimes this is difficult for local
68 ;; political/control reasons, and hence you can always install a
69 ;; private copy (or even a shared copy) which overrides the system
70 ;; default.
71
72 ;; You can get step by step help in installing this file by going to
73 ;; <http://www.verilog.com/emacs_install.html>
74
75 ;; The short list of installation instructions are: To set up
76 ;; automatic Verilog mode, put this file in your load path, and put
77 ;; the following in code (please un comment it first!) in your
78 ;; .emacs, or in your site's site-load.el
79
80 ; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
81 ; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
82
83 ;; Be sure to examine at the help for verilog-auto, and the other
84 ;; verilog-auto-* functions for some major coding time savers.
85 ;;
86 ;; If you want to customize Verilog mode to fit your needs better,
87 ;; you may add the below lines (the values of the variables presented
88 ;; here are the defaults). Note also that if you use an Emacs that
89 ;; supports custom, it's probably better to use the custom menu to
90 ;; edit these. If working as a member of a large team these settings
91 ;; should be common across all users (in a site-start file), or set
92 ;; in Local Variables in every file. Otherwise, different people's
93 ;; AUTO expansion may result different whitespace changes.
94 ;;
95 ; ;; Enable syntax highlighting of **all** languages
96 ; (global-font-lock-mode t)
97 ;
98 ; ;; User customization for Verilog mode
99 ; (setq verilog-indent-level 3
100 ; verilog-indent-level-module 3
101 ; verilog-indent-level-declaration 3
102 ; verilog-indent-level-behavioral 3
103 ; verilog-indent-level-directive 1
104 ; verilog-case-indent 2
105 ; verilog-auto-newline t
106 ; verilog-auto-indent-on-newline t
107 ; verilog-tab-always-indent t
108 ; verilog-auto-endcomments t
109 ; verilog-minimum-comment-distance 40
110 ; verilog-indent-begin-after-if t
111 ; verilog-auto-lineup 'declarations
112 ; verilog-highlight-p1800-keywords nil
113 ; verilog-linter "my_lint_shell_command"
114 ; )
115
116 ;; \f
117
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 "2014-05-31-3cd8144-vpo"
127 "Version of this Verilog mode.")
128 (defconst verilog-mode-release-emacs t
129 "If non-nil, this version of Verilog mode was released with Emacs itself.")
130
131 (defun verilog-version ()
132 "Inform caller of the version of this file."
133 (interactive)
134 (message "Using verilog-mode version %s" verilog-mode-version))
135
136 ;; Insure we have certain packages, and deal with it if we don't
137 ;; Be sure to note which Emacs flavor and version added each feature.
138 (eval-when-compile
139 ;; Provide stuff if we are XEmacs
140 (when (featurep 'xemacs)
141 (condition-case nil
142 (require 'easymenu)
143 (error nil))
144 (condition-case nil
145 (require 'regexp-opt)
146 (error nil))
147 ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
148 (condition-case nil
149 (load "skeleton")
150 (error nil))
151 (condition-case nil
152 (if (fboundp 'when)
153 nil ;; fab
154 (defmacro when (cond &rest body)
155 (list 'if cond (cons 'progn body))))
156 (error nil))
157 (condition-case nil
158 (if (fboundp 'unless)
159 nil ;; fab
160 (defmacro unless (cond &rest body)
161 (cons 'if (cons cond (cons nil body)))))
162 (error nil))
163 (condition-case nil
164 (if (fboundp 'store-match-data)
165 nil ;; fab
166 (defmacro store-match-data (&rest _args) nil))
167 (error nil))
168 (condition-case nil
169 (if (fboundp 'char-before)
170 nil ;; great
171 (defmacro char-before (&rest _body)
172 (char-after (1- (point)))))
173 (error nil))
174 (condition-case nil
175 (if (fboundp 'when)
176 nil ;; fab
177 (defsubst point-at-bol (&optional N)
178 (save-excursion (beginning-of-line N) (point))))
179 (error nil))
180 (condition-case nil
181 (if (fboundp 'when)
182 nil ;; fab
183 (defsubst point-at-eol (&optional N)
184 (save-excursion (end-of-line N) (point))))
185 (error nil))
186 (condition-case nil
187 (require 'custom)
188 (error nil))
189 (condition-case nil
190 (if (fboundp 'match-string-no-properties)
191 nil ;; great
192 (defsubst match-string-no-properties (num &optional string)
193 "Return string of text matched by last search, without text properties.
194 NUM specifies which parenthesized expression in the last regexp.
195 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
196 Zero means the entire text matched by the whole regexp or whole string.
197 STRING should be given if the last search was by `string-match' on STRING."
198 (if (match-beginning num)
199 (if string
200 (let ((result
201 (substring string
202 (match-beginning num) (match-end num))))
203 (set-text-properties 0 (length result) nil result)
204 result)
205 (buffer-substring-no-properties (match-beginning num)
206 (match-end num)
207 (current-buffer)))))
208 )
209 (error nil))
210 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
211 nil ;; We've got what we needed
212 ;; We have the old custom-library, hack around it!
213 (defmacro defgroup (&rest _args) nil)
214 (defmacro customize (&rest _args)
215 (message
216 "Sorry, Customize is not available with this version of Emacs"))
217 (defmacro defcustom (var value doc &rest _args)
218 `(defvar ,var ,value ,doc))
219 )
220 (if (fboundp 'defface)
221 nil ; great!
222 (defmacro defface (var values doc &rest _args)
223 `(make-face ,var))
224 )
225
226 (if (and (featurep 'custom) (fboundp 'customize-group))
227 nil ;; We've got what we needed
228 ;; We have an intermediate custom-library, hack around it!
229 (defmacro customize-group (var &rest _args)
230 `(customize ,var))
231 )
232
233 (unless (boundp 'inhibit-point-motion-hooks)
234 (defvar inhibit-point-motion-hooks nil))
235 (unless (boundp 'deactivate-mark)
236 (defvar deactivate-mark nil))
237 )
238 ;;
239 ;; OK, do this stuff if we are NOT XEmacs:
240 (unless (featurep 'xemacs)
241 (unless (fboundp 'region-active-p)
242 (defmacro region-active-p ()
243 `(and transient-mark-mode mark-active))))
244 )
245
246 ;; Provide a regular expression optimization routine, using regexp-opt
247 ;; if provided by the user's elisp libraries
248 (eval-and-compile
249 ;; The below were disabled when GNU Emacs 22 was released;
250 ;; perhaps some still need to be there to support Emacs 21.
251 (if (featurep 'xemacs)
252 (if (fboundp 'regexp-opt)
253 ;; regexp-opt is defined, does it take 3 or 2 arguments?
254 (if (fboundp 'function-max-args)
255 (let ((args (function-max-args `regexp-opt)))
256 (cond
257 ((eq args 3) ;; It takes 3
258 (condition-case nil ; Hide this defun from emacses
259 ;with just a two input regexp
260 (defun verilog-regexp-opt (a b)
261 "Deal with differing number of required arguments for `regexp-opt'.
262 Call `regexp-opt' on A and B."
263 (regexp-opt a b t))
264 (error nil))
265 )
266 ((eq args 2) ;; It takes 2
267 (defun verilog-regexp-opt (a b)
268 "Call `regexp-opt' on A and B."
269 (regexp-opt a b))
270 )
271 (t nil)))
272 ;; We can't tell; assume it takes 2
273 (defun verilog-regexp-opt (a b)
274 "Call `regexp-opt' on A and B."
275 (regexp-opt a b))
276 )
277 ;; There is no regexp-opt, provide our own
278 (defun verilog-regexp-opt (strings &optional paren _shy)
279 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
280 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
281 )
282 ;; Emacs.
283 (defalias 'verilog-regexp-opt 'regexp-opt)))
284
285 (eval-and-compile
286 ;; Both xemacs and emacs
287 (condition-case nil
288 (require 'diff) ;; diff-command and diff-switches
289 (error nil))
290 (condition-case nil
291 (require 'compile) ;; compilation-error-regexp-alist-alist
292 (error nil))
293 (condition-case nil
294 (unless (fboundp 'buffer-chars-modified-tick) ;; Emacs 22 added
295 (defmacro buffer-chars-modified-tick () (buffer-modified-tick)))
296 (error nil))
297 ;; Added in Emacs 24.1
298 (condition-case nil
299 (unless (fboundp 'prog-mode)
300 (define-derived-mode prog-mode fundamental-mode "Prog"))
301 (error nil)))
302
303 (eval-when-compile
304 (defun verilog-regexp-words (a)
305 "Call 'regexp-opt' with word delimiters for the words A."
306 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
307 (defun verilog-regexp-words (a)
308 "Call 'regexp-opt' with word delimiters for the words A."
309 ;; The FAQ references this function, so user LISP sometimes calls it
310 (concat "\\<" (verilog-regexp-opt a t) "\\>"))
311
312 (defun verilog-easy-menu-filter (menu)
313 "Filter `easy-menu-define' MENU to support new features."
314 (cond ((not (featurep 'xemacs))
315 menu) ;; GNU Emacs - passthru
316 ;; XEmacs doesn't support :help. Strip it.
317 ;; Recursively filter the a submenu
318 ((listp menu)
319 (mapcar 'verilog-easy-menu-filter menu))
320 ;; Look for [:help "blah"] and remove
321 ((vectorp menu)
322 (let ((i 0) (out []))
323 (while (< i (length menu))
324 (if (equal `:help (aref menu i))
325 (setq i (+ 2 i))
326 (setq out (vconcat out (vector (aref menu i)))
327 i (1+ i))))
328 out))
329 (t menu))) ;; Default - ok
330 ;;(verilog-easy-menu-filter
331 ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
332 ;; "----" ["MB" nil :help "Help MB"]))
333
334 (defun verilog-define-abbrev (table name expansion &optional hook)
335 "Filter `define-abbrev' TABLE NAME EXPANSION and call HOOK.
336 Provides SYSTEM-FLAG in newer Emacs."
337 (condition-case nil
338 (define-abbrev table name expansion hook 0 t)
339 (error
340 (define-abbrev table name expansion hook))))
341
342 (defun verilog-customize ()
343 "Customize variables and other settings used by Verilog-Mode."
344 (interactive)
345 (customize-group 'verilog-mode))
346
347 (defun verilog-font-customize ()
348 "Customize fonts used by Verilog-Mode."
349 (interactive)
350 (if (fboundp 'customize-apropos)
351 (customize-apropos "font-lock-*" 'faces)))
352
353 (defun verilog-booleanp (value)
354 "Return t if VALUE is boolean.
355 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
356 This function may be removed when Emacs 21 is no longer supported."
357 (or (equal value t) (equal value nil)))
358
359 (defun verilog-insert-last-command-event ()
360 "Insert the `last-command-event'."
361 (insert (if (featurep 'xemacs)
362 ;; XEmacs 21.5 doesn't like last-command-event
363 last-command-char
364 ;; And GNU Emacs 22 has obsoleted last-command-char
365 last-command-event)))
366
367 (defvar verilog-no-change-functions nil
368 "True if `after-change-functions' is disabled.
369 Use of `syntax-ppss' may break, as ppss's cache may get corrupted.")
370
371 (defvar verilog-in-hooks nil
372 "True when within a `verilog-run-hooks' block.")
373
374 (defmacro verilog-run-hooks (&rest hooks)
375 "Run each hook in HOOKS using `run-hooks'.
376 Set `verilog-in-hooks' during this time, to assist AUTO caches."
377 `(let ((verilog-in-hooks t))
378 (run-hooks ,@hooks)))
379
380 (defun verilog-syntax-ppss (&optional pos)
381 (when verilog-no-change-functions
382 (if verilog-in-hooks
383 (verilog-scan-cache-flush)
384 ;; else don't let the AUTO code itself get away with flushing the cache,
385 ;; as that'll make things very slow
386 (backtrace)
387 (error "%s: Internal problem; use of syntax-ppss when cache may be corrupt"
388 (verilog-point-text))))
389 (if (fboundp 'syntax-ppss)
390 (syntax-ppss pos)
391 (parse-partial-sexp (point-min) (or pos (point)))))
392
393 (defgroup verilog-mode nil
394 "Major mode for Verilog source code."
395 :version "22.2"
396 :group 'languages)
397
398 ; (defgroup verilog-mode-fonts nil
399 ; "Facilitates easy customization fonts used in Verilog source text"
400 ; :link '(customize-apropos "font-lock-*" 'faces)
401 ; :group 'verilog-mode)
402
403 (defgroup verilog-mode-indent nil
404 "Customize indentation and highlighting of Verilog source text."
405 :group 'verilog-mode)
406
407 (defgroup verilog-mode-actions nil
408 "Customize actions on Verilog source text."
409 :group 'verilog-mode)
410
411 (defgroup verilog-mode-auto nil
412 "Customize AUTO actions when expanding Verilog source text."
413 :group 'verilog-mode)
414
415 (defvar verilog-debug nil
416 "Non-nil means enable debug messages for `verilog-mode' internals.")
417
418 (defvar verilog-warn-fatal nil
419 "Non-nil means `verilog-warn-error' warnings are fatal `error's.")
420
421 (defcustom verilog-linter
422 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
423 "Unix program and arguments to call to run a lint checker on Verilog source.
424 Depending on the `verilog-set-compile-command', this may be invoked when
425 you type \\[compile]. When the compile completes, \\[next-error] will take
426 you to the next lint error."
427 :type 'string
428 :group 'verilog-mode-actions)
429 ;; We don't mark it safe, as it's used as a shell command
430
431 (defcustom verilog-coverage
432 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
433 "Program and arguments to use to annotate for coverage Verilog source.
434 Depending on the `verilog-set-compile-command', this may be invoked when
435 you type \\[compile]. When the compile completes, \\[next-error] will take
436 you to the next lint error."
437 :type 'string
438 :group 'verilog-mode-actions)
439 ;; We don't mark it safe, as it's used as a shell command
440
441 (defcustom verilog-simulator
442 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
443 "Program and arguments to use to interpret Verilog source.
444 Depending on the `verilog-set-compile-command', this may be invoked when
445 you type \\[compile]. When the compile completes, \\[next-error] will take
446 you to the next lint error."
447 :type 'string
448 :group 'verilog-mode-actions)
449 ;; We don't mark it safe, as it's used as a shell command
450
451 (defcustom verilog-compiler
452 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
453 "Program and arguments to use to compile Verilog source.
454 Depending on the `verilog-set-compile-command', this may be invoked when
455 you type \\[compile]. When the compile completes, \\[next-error] will take
456 you to the next lint error."
457 :type 'string
458 :group 'verilog-mode-actions)
459 ;; We don't mark it safe, as it's used as a shell command
460
461 (defcustom verilog-preprocessor
462 ;; Very few tools give preprocessed output, so we'll default to Verilog-Perl
463 "vppreproc __FLAGS__ __FILE__"
464 "Program and arguments to use to preprocess Verilog source.
465 This is invoked with `verilog-preprocess', and depending on the
466 `verilog-set-compile-command', may also be invoked when you type
467 \\[compile]. When the compile completes, \\[next-error] will
468 take you to the next lint error."
469 :type 'string
470 :group 'verilog-mode-actions)
471 ;; We don't mark it safe, as it's used as a shell command
472
473 (defvar verilog-preprocess-history nil
474 "History for `verilog-preprocess'.")
475
476 (defvar verilog-tool 'verilog-linter
477 "Which tool to use for building compiler-command.
478 Either nil, `verilog-linter, `verilog-compiler,
479 `verilog-coverage, `verilog-preprocessor, or `verilog-simulator.
480 Alternatively use the \"Choose Compilation Action\" menu. See
481 `verilog-set-compile-command' for more information.")
482
483 (defcustom verilog-highlight-translate-off nil
484 "Non-nil means background-highlight code excluded from translation.
485 That is, all code between \"// synopsys translate_off\" and
486 \"// synopsys translate_on\" is highlighted using a different background color
487 \(face `verilog-font-lock-translate-off-face').
488
489 Note: This will slow down on-the-fly fontification (and thus editing).
490
491 Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
492 entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
493 :type 'boolean
494 :group 'verilog-mode-indent)
495 ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
496 (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
497
498 (defcustom verilog-auto-lineup 'declarations
499 "Type of statements to lineup across multiple lines.
500 If 'all' is selected, then all line ups described below are done.
501
502 If 'declarations', then just declarations are lined up with any
503 preceding declarations, taking into account widths and the like,
504 so or example the code:
505 reg [31:0] a;
506 reg b;
507 would become
508 reg [31:0] a;
509 reg b;
510
511 If 'assignment', then assignments are lined up with any preceding
512 assignments, so for example the code
513 a_long_variable <= b + c;
514 d = e + f;
515 would become
516 a_long_variable <= b + c;
517 d = e + f;
518
519 In order to speed up editing, large blocks of statements are lined up
520 only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
521 are lineup only when \\[verilog-pretty-declarations] is typed."
522
523 :type '(radio (const :tag "Line up Assignments and Declarations" all)
524 (const :tag "Line up Assignment statements" assignments )
525 (const :tag "Line up Declarations" declarations)
526 (function :tag "Other"))
527 :group 'verilog-mode-indent )
528 (put 'verilog-auto-lineup 'safe-local-variable
529 '(lambda (x) (memq x '(nil all assignments declarations))))
530
531 (defcustom verilog-indent-level 3
532 "Indentation of Verilog statements with respect to containing block."
533 :group 'verilog-mode-indent
534 :type 'integer)
535 (put 'verilog-indent-level 'safe-local-variable 'integerp)
536
537 (defcustom verilog-indent-level-module 3
538 "Indentation of Module level Verilog statements (eg always, initial).
539 Set to 0 to get initial and always statements lined up on the left side of
540 your screen."
541 :group 'verilog-mode-indent
542 :type 'integer)
543 (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
544
545 (defcustom verilog-indent-level-declaration 3
546 "Indentation of declarations with respect to containing block.
547 Set to 0 to get them list right under containing block."
548 :group 'verilog-mode-indent
549 :type 'integer)
550 (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
551
552 (defcustom verilog-indent-declaration-macros nil
553 "How to treat macro expansions in a declaration.
554 If nil, indent as:
555 input [31:0] a;
556 input `CP;
557 output c;
558 If non nil, treat as:
559 input [31:0] a;
560 input `CP ;
561 output c;"
562 :group 'verilog-mode-indent
563 :type 'boolean)
564 (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
565
566 (defcustom verilog-indent-lists t
567 "How to treat indenting items in a list.
568 If t (the default), indent as:
569 always @( posedge a or
570 reset ) begin
571
572 If nil, treat as:
573 always @( posedge a or
574 reset ) begin"
575 :group 'verilog-mode-indent
576 :type 'boolean)
577 (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
578
579 (defcustom verilog-indent-level-behavioral 3
580 "Absolute indentation of first begin in a task or function block.
581 Set to 0 to get such code to start at the left side of the screen."
582 :group 'verilog-mode-indent
583 :type 'integer)
584 (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
585
586 (defcustom verilog-indent-level-directive 1
587 "Indentation to add to each level of `ifdef declarations.
588 Set to 0 to have all directives start at the left side of the screen."
589 :group 'verilog-mode-indent
590 :type 'integer)
591 (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
592
593 (defcustom verilog-cexp-indent 2
594 "Indentation of Verilog statements split across lines."
595 :group 'verilog-mode-indent
596 :type 'integer)
597 (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
598
599 (defcustom verilog-case-indent 2
600 "Indentation for case statements."
601 :group 'verilog-mode-indent
602 :type 'integer)
603 (put 'verilog-case-indent 'safe-local-variable 'integerp)
604
605 (defcustom verilog-auto-newline t
606 "Non-nil means automatically newline after semicolons."
607 :group 'verilog-mode-indent
608 :type 'boolean)
609 (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
610
611 (defcustom verilog-auto-indent-on-newline t
612 "Non-nil means automatically indent line after newline."
613 :group 'verilog-mode-indent
614 :type 'boolean)
615 (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
616
617 (defcustom verilog-tab-always-indent t
618 "Non-nil means TAB should always re-indent the current line.
619 A nil value means TAB will only reindent when at the beginning of the line."
620 :group 'verilog-mode-indent
621 :type 'boolean)
622 (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
623
624 (defcustom verilog-tab-to-comment nil
625 "Non-nil means TAB moves to the right hand column in preparation for a comment."
626 :group 'verilog-mode-actions
627 :type 'boolean)
628 (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
629
630 (defcustom verilog-indent-begin-after-if t
631 "Non-nil means indent begin statements following if, else, while, etc.
632 Otherwise, line them up."
633 :group 'verilog-mode-indent
634 :type 'boolean)
635 (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
636
637 (defcustom verilog-align-ifelse nil
638 "Non-nil means align `else' under matching `if'.
639 Otherwise else is lined up with first character on line holding matching if."
640 :group 'verilog-mode-indent
641 :type 'boolean)
642 (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
643
644 (defcustom verilog-minimum-comment-distance 10
645 "Minimum distance (in lines) between begin and end required before a comment.
646 Setting this variable to zero results in every end acquiring a comment; the
647 default avoids too many redundant comments in tight quarters."
648 :group 'verilog-mode-indent
649 :type 'integer)
650 (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
651
652 (defcustom verilog-highlight-p1800-keywords nil
653 "Non-nil means highlight words newly reserved by IEEE-1800.
654 These will appear in `verilog-font-lock-p1800-face' in order to gently
655 suggest changing where these words are used as variables to something else.
656 A nil value means highlight these words as appropriate for the SystemVerilog
657 IEEE-1800 standard. Note that changing this will require restarting Emacs
658 to see the effect as font color choices are cached by Emacs."
659 :group 'verilog-mode-indent
660 :type 'boolean)
661 (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
662
663 (defcustom verilog-highlight-grouping-keywords nil
664 "Non-nil means highlight grouping keywords more dramatically.
665 If false, these words are in the `font-lock-type-face'; if True then they are in
666 `verilog-font-lock-ams-face'. Some find that special highlighting on these
667 grouping constructs allow the structure of the code to be understood at a glance."
668 :group 'verilog-mode-indent
669 :type 'boolean)
670 (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
671
672 (defcustom verilog-highlight-modules nil
673 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
674 When true, mousing over module names will allow jumping to the
675 module definition. If false, this is not supported. Setting
676 this is experimental, and may lead to bad performance."
677 :group 'verilog-mode-indent
678 :type 'boolean)
679 (put 'verilog-highlight-modules 'safe-local-variable 'verilog-booleanp)
680
681 (defcustom verilog-highlight-includes t
682 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
683 When true, mousing over include file names will allow jumping to the
684 file referenced. If false, this is not supported."
685 :group 'verilog-mode-indent
686 :type 'boolean)
687 (put 'verilog-highlight-includes 'safe-local-variable 'verilog-booleanp)
688
689 (defcustom verilog-auto-declare-nettype nil
690 "Non-nil specifies the data type to use with `verilog-auto-input' etc.
691 Set this to \"wire\" if the Verilog code uses \"`default_nettype
692 none\". Note using `default_nettype none isn't recommended practice; this
693 mode is experimental."
694 :version "24.1" ;; rev670
695 :group 'verilog-mode-actions
696 :type 'boolean)
697 (put 'verilog-auto-declare-nettype 'safe-local-variable `stringp)
698
699 (defcustom verilog-auto-wire-type nil
700 "Non-nil specifies the data type to use with `verilog-auto-wire' etc.
701 Set this to \"logic\" for SystemVerilog code, or use `verilog-auto-logic'."
702 :version "24.1" ;; rev673
703 :group 'verilog-mode-actions
704 :type 'boolean)
705 (put 'verilog-auto-wire-type 'safe-local-variable `stringp)
706
707 (defcustom verilog-auto-endcomments t
708 "Non-nil means insert a comment /* ... */ after 'end's.
709 The name of the function or case will be set between the braces."
710 :group 'verilog-mode-actions
711 :type 'boolean)
712 (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
713
714 (defcustom verilog-auto-delete-trailing-whitespace nil
715 "Non-nil means to `delete-trailing-whitespace' in `verilog-auto'."
716 :version "24.1" ;; rev703
717 :group 'verilog-mode-actions
718 :type 'boolean)
719 (put 'verilog-auto-delete-trailing-whitespace 'safe-local-variable 'verilog-booleanp)
720
721 (defcustom verilog-auto-ignore-concat nil
722 "Non-nil means ignore signals in {...} concatenations for AUTOWIRE etc.
723 This will exclude signals referenced as pin connections in {...}
724 from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
725 for backward compatibility only and not set in new designs; it
726 may be removed in future versions."
727 :group 'verilog-mode-actions
728 :type 'boolean)
729 (put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
730
731 (defcustom verilog-auto-read-includes nil
732 "Non-nil means to automatically read includes before AUTOs.
733 This will do a `verilog-read-defines' and `verilog-read-includes' before
734 each AUTO expansion. This makes it easier to embed defines and includes,
735 but can result in very slow reading times if there are many or large
736 include files."
737 :group 'verilog-mode-actions
738 :type 'boolean)
739 (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
740
741 (defcustom verilog-auto-save-policy nil
742 "Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
743 A value of `force' will always do a \\[verilog-auto] automatically if
744 needed on every save. A value of `detect' will do \\[verilog-auto]
745 automatically when it thinks necessary. A value of `ask' will query the
746 user when it thinks updating is needed.
747
748 You should not rely on the 'ask or 'detect policies, they are safeguards
749 only. They do not detect when AUTOINSTs need to be updated because a
750 sub-module's port list has changed."
751 :group 'verilog-mode-actions
752 :type '(choice (const nil) (const ask) (const detect) (const force)))
753
754 (defcustom verilog-auto-star-expand t
755 "Non-nil means to expand SystemVerilog .* instance ports.
756 They will be expanded in the same way as if there was an AUTOINST in the
757 instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
758 :group 'verilog-mode-actions
759 :type 'boolean)
760 (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
761
762 (defcustom verilog-auto-star-save nil
763 "Non-nil means save to disk SystemVerilog .* instance expansions.
764 A nil value indicates direct connections will be removed before saving.
765 Only meaningful to those created due to `verilog-auto-star-expand' being set.
766
767 Instead of setting this, you may want to use /*AUTOINST*/, which will
768 always be saved."
769 :group 'verilog-mode-actions
770 :type 'boolean)
771 (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
772
773 (defvar verilog-auto-update-tick nil
774 "Modification tick at which autos were last performed.")
775
776 (defvar verilog-auto-last-file-locals nil
777 "Text from file-local-variables during last evaluation.")
778
779 (defvar verilog-diff-function 'verilog-diff-report
780 "Function to run when `verilog-diff-auto' detects differences.
781 Function takes three arguments, the original buffer, the
782 difference buffer, and the point in original buffer with the
783 first difference.")
784
785 ;;; Compile support
786 (require 'compile)
787 (defvar verilog-error-regexp-added nil)
788
789 (defvar verilog-error-regexp-emacs-alist
790 '(
791 (verilog-xl-1
792 "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
793 (verilog-xl-2
794 "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
795 (verilog-IES
796 ".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)" 2 3)
797 (verilog-surefire-1
798 "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
799 (verilog-surefire-2
800 "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
801 (verilog-verbose
802 "\
803 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
804 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
805 (verilog-xsim
806 "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
807 (verilog-vcs-1
808 "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
809 (verilog-vcs-2
810 "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
811 (verilog-vcs-3
812 "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
813 (verilog-vcs-4
814 "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
815 (verilog-verilator
816 "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
817 (verilog-leda
818 "^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 2)
819 )
820 "List of regexps for Verilog compilers.
821 See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
822
823 (defvar verilog-error-regexp-xemacs-alist
824 ;; Emacs form is '((v-tool "re" 1 2) ...)
825 ;; XEmacs form is '(verilog ("re" 1 2) ...)
826 ;; So we can just map from Emacs to XEmacs
827 (cons 'verilog (mapcar 'cdr verilog-error-regexp-emacs-alist))
828 "List of regexps for Verilog compilers.
829 See `compilation-error-regexp-alist-alist' for the formatting. For XEmacs.")
830
831 (defvar verilog-error-font-lock-keywords
832 '(
833 ;; verilog-xl-1
834 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
835 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
836 ;; verilog-xl-2
837 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 bold t)
838 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 3 bold t)
839 ;; verilog-IES (nc-verilog)
840 (".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
841 (".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 3 bold t)
842 ;; verilog-surefire-1
843 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
844 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
845 ;; verilog-surefire-2
846 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
847 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
848 ;; verilog-verbose
849 ("\
850 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
851 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
852 ("\
853 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
854 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
855 ;; verilog-vcs-1
856 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
857 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
858 ;; verilog-vcs-2
859 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
860 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
861 ;; verilog-vcs-3
862 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
863 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
864 ;; verilog-vcs-4
865 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
866 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
867 ;; verilog-verilator
868 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
869 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
870 ;; verilog-leda
871 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 bold t)
872 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 2 bold t)
873 )
874 "Keywords to also highlight in Verilog *compilation* buffers.
875 Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
876
877 (defcustom verilog-library-flags '("")
878 "List of standard Verilog arguments to use for /*AUTOINST*/.
879 These arguments are used to find files for `verilog-auto', and match
880 the flags accepted by a standard Verilog-XL simulator.
881
882 -f filename Reads more `verilog-library-flags' from the filename.
883 +incdir+dir Adds the directory to `verilog-library-directories'.
884 -Idir Adds the directory to `verilog-library-directories'.
885 -y dir Adds the directory to `verilog-library-directories'.
886 +libext+.v Adds the extensions to `verilog-library-extensions'.
887 -v filename Adds the filename to `verilog-library-files'.
888
889 filename Adds the filename to `verilog-library-files'.
890 This is not recommended, -v is a better choice.
891
892 You might want these defined in each file; put at the *END* of your file
893 something like:
894
895 // Local Variables:
896 // verilog-library-flags:(\"-y dir -y otherdir\")
897 // End:
898
899 Verilog-mode attempts to detect changes to this local variable, but they
900 are only insured to be correct when the file is first visited. Thus if you
901 have problems, use \\[find-alternate-file] RET to have these take effect.
902
903 See also the variables mentioned above."
904 :group 'verilog-mode-auto
905 :type '(repeat string))
906 (put 'verilog-library-flags 'safe-local-variable 'listp)
907
908 (defcustom verilog-library-directories '(".")
909 "List of directories when looking for files for /*AUTOINST*/.
910 The directory may be relative to the current file, or absolute.
911 Environment variables are also expanded in the directory names.
912 Having at least the current directory is a good idea.
913
914 You might want these defined in each file; put at the *END* of your file
915 something like:
916
917 // Local Variables:
918 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
919 // End:
920
921 Verilog-mode attempts to detect changes to this local variable, but they
922 are only insured to be correct when the file is first visited. Thus if you
923 have problems, use \\[find-alternate-file] RET to have these take effect.
924
925 See also `verilog-library-flags', `verilog-library-files'
926 and `verilog-library-extensions'."
927 :group 'verilog-mode-auto
928 :type '(repeat file))
929 (put 'verilog-library-directories 'safe-local-variable 'listp)
930
931 (defcustom verilog-library-files '()
932 "List of files to search for modules.
933 AUTOINST will use this when it needs to resolve a module name.
934 This is a complete path, usually to a technology file with many standard
935 cells defined in it.
936
937 You might want these defined in each file; put at the *END* of your file
938 something like:
939
940 // Local Variables:
941 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
942 // End:
943
944 Verilog-mode attempts to detect changes to this local variable, but they
945 are only insured to be correct when the file is first visited. Thus if you
946 have problems, use \\[find-alternate-file] RET to have these take effect.
947
948 See also `verilog-library-flags', `verilog-library-directories'."
949 :group 'verilog-mode-auto
950 :type '(repeat directory))
951 (put 'verilog-library-files 'safe-local-variable 'listp)
952
953 (defcustom verilog-library-extensions '(".v" ".sv")
954 "List of extensions to use when looking for files for /*AUTOINST*/.
955 See also `verilog-library-flags', `verilog-library-directories'."
956 :type '(repeat string)
957 :group 'verilog-mode-auto)
958 (put 'verilog-library-extensions 'safe-local-variable 'listp)
959
960 (defcustom verilog-active-low-regexp nil
961 "If true, treat signals matching this regexp as active low.
962 This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
963 you will probably also need `verilog-auto-reset-widths' set."
964 :group 'verilog-mode-auto
965 :type '(choice (const nil) regexp))
966 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
967
968 (defcustom verilog-auto-sense-include-inputs nil
969 "Non-nil means AUTOSENSE should include all inputs.
970 If nil, only inputs that are NOT output signals in the same block are
971 included."
972 :group 'verilog-mode-auto
973 :type 'boolean)
974 (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
975
976 (defcustom verilog-auto-sense-defines-constant nil
977 "Non-nil means AUTOSENSE should assume all defines represent constants.
978 When true, the defines will not be included in sensitivity lists. To
979 maintain compatibility with other sites, this should be set at the bottom
980 of each Verilog file that requires it, rather than being set globally."
981 :group 'verilog-mode-auto
982 :type 'boolean)
983 (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
984
985 (defcustom verilog-auto-reset-blocking-in-non t
986 "Non-nil means AUTORESET will reset blocking statements.
987 When true, AUTORESET will reset in blocking statements those
988 signals which were assigned with blocking assignments (=) even in
989 a block with non-blocking assignments (<=).
990
991 If nil, all blocking assigned signals are ignored when any
992 non-blocking assignment is in the AUTORESET block. This allows
993 blocking assignments to be used for temporary values and not have
994 those temporaries reset. See example in `verilog-auto-reset'."
995 :version "24.1" ;; rev718
996 :type 'boolean
997 :group 'verilog-mode-auto)
998 (put 'verilog-auto-reset-blocking-in-non 'safe-local-variable 'verilog-booleanp)
999
1000 (defcustom verilog-auto-reset-widths t
1001 "True means AUTORESET should determine the width of signals.
1002 This is then used to set the width of the zero (32'h0 for example). This
1003 is required by some lint tools that aren't smart enough to ignore widths of
1004 the constant zero. This may result in ugly code when parameters determine
1005 the MSB or LSB of a signal inside an AUTORESET.
1006
1007 If nil, AUTORESET uses \"0\" as the constant.
1008
1009 If 'unbased', AUTORESET used the unbased unsized literal \"'0\"
1010 as the constant. This setting is strongly recommended for
1011 SystemVerilog designs."
1012 :type 'boolean
1013 :group 'verilog-mode-auto)
1014 (put 'verilog-auto-reset-widths 'safe-local-variable
1015 '(lambda (x) (memq x '(nil t unbased))))
1016
1017 (defcustom verilog-assignment-delay ""
1018 "Text used for delays in delayed assignments. Add a trailing space if set."
1019 :group 'verilog-mode-auto
1020 :type 'string)
1021 (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
1022
1023 (defcustom verilog-auto-arg-format 'packed
1024 "Formatting to use for AUTOARG signal names.
1025 If 'packed', then as many inputs and outputs that fit within
1026 `fill-column' will be put onto one line.
1027
1028 If 'single', then a single input or output will be put onto each
1029 line."
1030 :version "24.5"
1031 :type '(radio (const :tag "Line up Assignments and Declarations" packed)
1032 (const :tag "Line up Assignment statements" single))
1033 :group 'verilog-mode-auto)
1034 (put 'verilog-auto-arg-format 'safe-local-variable
1035 '(lambda (x) (memq x '(packed single))))
1036
1037 (defcustom verilog-auto-arg-sort nil
1038 "Non-nil means AUTOARG signal names will be sorted, not in declaration order.
1039 Declaration order is advantageous with order based instantiations
1040 and is the default for backward compatibility. Sorted order
1041 reduces changes when declarations are moved around in a file, and
1042 it's bad practice to rely on order based instantiations anyhow.
1043
1044 See also `verilog-auto-inst-sort'."
1045 :group 'verilog-mode-auto
1046 :type 'boolean)
1047 (put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
1048
1049 (defcustom verilog-auto-inst-dot-name nil
1050 "Non-nil means when creating ports with AUTOINST, use .name syntax.
1051 This will use \".port\" instead of \".port(port)\" when possible.
1052 This is only legal in SystemVerilog files, and will confuse older
1053 simulators. Setting `verilog-auto-inst-vector' to nil may also
1054 be desirable to increase how often .name will be used."
1055 :group 'verilog-mode-auto
1056 :type 'boolean)
1057 (put 'verilog-auto-inst-dot-name 'safe-local-variable 'verilog-booleanp)
1058
1059 (defcustom verilog-auto-inst-param-value nil
1060 "Non-nil means AUTOINST will replace parameters with the parameter value.
1061 If nil, leave parameters as symbolic names.
1062
1063 Parameters must be in Verilog 2001 format #(...), and if a parameter is not
1064 listed as such there (as when the default value is acceptable), it will not
1065 be replaced, and will remain symbolic.
1066
1067 For example, imagine a submodule uses parameters to declare the size of its
1068 inputs. This is then used by an upper module:
1069
1070 module InstModule (o,i);
1071 parameter WIDTH;
1072 input [WIDTH-1:0] i;
1073 endmodule
1074
1075 module ExampInst;
1076 InstModule
1077 #(.PARAM(10))
1078 instName
1079 (/*AUTOINST*/
1080 .i (i[PARAM-1:0]));
1081
1082 Note even though PARAM=10, the AUTOINST has left the parameter as a
1083 symbolic name. If `verilog-auto-inst-param-value' is set, this will
1084 instead expand to:
1085
1086 module ExampInst;
1087 InstModule
1088 #(.PARAM(10))
1089 instName
1090 (/*AUTOINST*/
1091 .i (i[9:0]));"
1092 :group 'verilog-mode-auto
1093 :type 'boolean)
1094 (put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
1095
1096 (defcustom verilog-auto-inst-sort nil
1097 "Non-nil means AUTOINST signals will be sorted, not in declaration order.
1098 Also affects AUTOINSTPARAM. Declaration order is the default for
1099 backward compatibility, and as some teams prefer signals that are
1100 declared together to remain together. Sorted order reduces
1101 changes when declarations are moved around in a file.
1102
1103 See also `verilog-auto-arg-sort'."
1104 :version "24.1" ;; rev688
1105 :group 'verilog-mode-auto
1106 :type 'boolean)
1107 (put 'verilog-auto-inst-sort 'safe-local-variable 'verilog-booleanp)
1108
1109 (defcustom verilog-auto-inst-vector t
1110 "Non-nil means when creating default ports with AUTOINST, use bus subscripts.
1111 If nil, skip the subscript when it matches the entire bus as declared in
1112 the module (AUTOWIRE signals always are subscripted, you must manually
1113 declare the wire to have the subscripts removed.) Setting this to nil may
1114 speed up some simulators, but is less general and harder to read, so avoid."
1115 :group 'verilog-mode-auto
1116 :type 'boolean)
1117 (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
1118
1119 (defcustom verilog-auto-inst-template-numbers nil
1120 "If true, when creating templated ports with AUTOINST, add a comment.
1121
1122 If t, the comment will add the line number of the template that
1123 was used for that port declaration. This setting is suggested
1124 only for debugging use, as regular use may cause a large numbers
1125 of merge conflicts.
1126
1127 If 'lhs', the comment will show the left hand side of the
1128 AUTO_TEMPLATE rule that is matched. This is less precise than
1129 numbering (t) when multiple rules have the same pin name, but
1130 won't merge conflict."
1131 :group 'verilog-mode-auto
1132 :type '(choice (const nil) (const t) (const lhs)))
1133 (put 'verilog-auto-inst-template-numbers 'safe-local-variable
1134 '(lambda (x) (memq x '(nil t lhs))))
1135
1136 (defcustom verilog-auto-inst-column 40
1137 "Indent-to column number for net name part of AUTOINST created pin."
1138 :group 'verilog-mode-indent
1139 :type 'integer)
1140 (put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
1141
1142 (defcustom verilog-auto-inst-interfaced-ports nil
1143 "Non-nil means include interfaced ports in AUTOINST expansions."
1144 :version "24.3" ;; rev773, default change rev815
1145 :group 'verilog-mode-auto
1146 :type 'boolean)
1147 (put 'verilog-auto-inst-interfaced-ports 'safe-local-variable 'verilog-booleanp)
1148
1149 (defcustom verilog-auto-input-ignore-regexp nil
1150 "If non-nil, when creating AUTOINPUT, ignore signals matching this regexp.
1151 See the \\[verilog-faq] for examples on using this."
1152 :group 'verilog-mode-auto
1153 :type '(choice (const nil) regexp))
1154 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
1155
1156 (defcustom verilog-auto-inout-ignore-regexp nil
1157 "If non-nil, when creating AUTOINOUT, ignore signals matching this regexp.
1158 See the \\[verilog-faq] for examples on using this."
1159 :group 'verilog-mode-auto
1160 :type '(choice (const nil) regexp))
1161 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
1162
1163 (defcustom verilog-auto-output-ignore-regexp nil
1164 "If non-nil, when creating AUTOOUTPUT, ignore signals matching this regexp.
1165 See the \\[verilog-faq] for examples on using this."
1166 :group 'verilog-mode-auto
1167 :type '(choice (const nil) regexp))
1168 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
1169
1170 (defcustom verilog-auto-template-warn-unused nil
1171 "Non-nil means report warning if an AUTO_TEMPLATE line is not used.
1172 This feature is not supported before Emacs 21.1 or XEmacs 21.4."
1173 :version "24.3" ;;rev787
1174 :group 'verilog-mode-auto
1175 :type 'boolean)
1176 (put 'verilog-auto-template-warn-unused 'safe-local-variable 'verilog-booleanp)
1177
1178 (defcustom verilog-auto-tieoff-declaration "wire"
1179 "Data type used for the declaration for AUTOTIEOFF.
1180 If \"wire\" then create a wire, if \"assign\" create an
1181 assignment, else the data type for variable creation."
1182 :version "24.1" ;; rev713
1183 :group 'verilog-mode-auto
1184 :type 'string)
1185 (put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
1186
1187 (defcustom verilog-auto-tieoff-ignore-regexp nil
1188 "If non-nil, when creating AUTOTIEOFF, ignore signals matching this regexp.
1189 See the \\[verilog-faq] for examples on using this."
1190 :group 'verilog-mode-auto
1191 :type '(choice (const nil) regexp))
1192 (put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
1193
1194 (defcustom verilog-auto-unused-ignore-regexp nil
1195 "If non-nil, when creating AUTOUNUSED, ignore signals matching this regexp.
1196 See the \\[verilog-faq] for examples on using this."
1197 :group 'verilog-mode-auto
1198 :type '(choice (const nil) regexp))
1199 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
1200
1201 (defcustom verilog-case-fold t
1202 "Non-nil means `verilog-mode' regexps should ignore case.
1203 This variable is t for backward compatibility; nil is suggested."
1204 :version "24.4"
1205 :group 'verilog-mode
1206 :type 'boolean)
1207 (put 'verilog-case-fold 'safe-local-variable 'verilog-booleanp)
1208
1209 (defcustom verilog-typedef-regexp nil
1210 "If non-nil, regular expression that matches Verilog-2001 typedef names.
1211 For example, \"_t$\" matches typedefs named with _t, as in the C language.
1212 See also `verilog-case-fold'."
1213 :group 'verilog-mode-auto
1214 :type '(choice (const nil) regexp))
1215 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
1216
1217 (defcustom verilog-mode-hook 'verilog-set-compile-command
1218 "Hook run after Verilog mode is loaded."
1219 :type 'hook
1220 :group 'verilog-mode)
1221
1222 (defcustom verilog-auto-hook nil
1223 "Hook run after `verilog-mode' updates AUTOs."
1224 :group 'verilog-mode-auto
1225 :type 'hook)
1226
1227 (defcustom verilog-before-auto-hook nil
1228 "Hook run before `verilog-mode' updates AUTOs."
1229 :group 'verilog-mode-auto
1230 :type 'hook)
1231
1232 (defcustom verilog-delete-auto-hook nil
1233 "Hook run after `verilog-mode' deletes AUTOs."
1234 :group 'verilog-mode-auto
1235 :type 'hook)
1236
1237 (defcustom verilog-before-delete-auto-hook nil
1238 "Hook run before `verilog-mode' deletes AUTOs."
1239 :group 'verilog-mode-auto
1240 :type 'hook)
1241
1242 (defcustom verilog-getopt-flags-hook nil
1243 "Hook run after `verilog-getopt-flags' determines the Verilog option lists."
1244 :group 'verilog-mode-auto
1245 :type 'hook)
1246
1247 (defcustom verilog-before-getopt-flags-hook nil
1248 "Hook run before `verilog-getopt-flags' determines the Verilog option lists."
1249 :group 'verilog-mode-auto
1250 :type 'hook)
1251
1252 (defcustom verilog-before-save-font-hook nil
1253 "Hook run before `verilog-save-font-mods' removes highlighting."
1254 :version "24.3" ;;rev735
1255 :group 'verilog-mode-auto
1256 :type 'hook)
1257
1258 (defcustom verilog-after-save-font-hook nil
1259 "Hook run after `verilog-save-font-mods' restores highlighting."
1260 :version "24.3" ;;rev735
1261 :group 'verilog-mode-auto
1262 :type 'hook)
1263
1264 (defvar verilog-imenu-generic-expression
1265 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
1266 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
1267 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
1268
1269 ;;
1270 ;; provide a verilog-header function.
1271 ;; Customization variables:
1272 ;;
1273 (defvar verilog-date-scientific-format nil
1274 "If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1275 If nil, in European format (e.g. 17.09.1997). The brain-dead American
1276 format (e.g. 09/17/1997) is not supported.")
1277
1278 (defvar verilog-company nil
1279 "Default name of Company for Verilog header.
1280 If set will become buffer local.")
1281 (make-variable-buffer-local 'verilog-company)
1282
1283 (defvar verilog-project nil
1284 "Default name of Project for Verilog header.
1285 If set will become buffer local.")
1286 (make-variable-buffer-local 'verilog-project)
1287
1288 (defvar verilog-mode-map
1289 (let ((map (make-sparse-keymap)))
1290 (define-key map ";" 'electric-verilog-semi)
1291 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1292 (define-key map ":" 'electric-verilog-colon)
1293 ;;(define-key map "=" 'electric-verilog-equal)
1294 (define-key map "\`" 'electric-verilog-tick)
1295 (define-key map "\t" 'electric-verilog-tab)
1296 (define-key map "\r" 'electric-verilog-terminate-line)
1297 ;; backspace/delete key bindings
1298 (define-key map [backspace] 'backward-delete-char-untabify)
1299 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1300 (define-key map [delete] 'delete-char)
1301 (define-key map [(meta delete)] 'kill-word))
1302 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1303 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1304 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1305 (define-key map "\M-\t" 'verilog-complete-word)
1306 (define-key map "\M-?" 'verilog-show-completions)
1307 ;; Note \C-c and letter are reserved for users
1308 (define-key map "\C-c\`" 'verilog-lint-off)
1309 (define-key map "\C-c\*" 'verilog-delete-auto-star-implicit)
1310 (define-key map "\C-c\?" 'verilog-diff-auto)
1311 (define-key map "\C-c\C-r" 'verilog-label-be)
1312 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1313 (define-key map "\C-c=" 'verilog-pretty-expr)
1314 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1315 (define-key map "\M-*" 'verilog-star-comment)
1316 (define-key map "\C-c\C-c" 'verilog-comment-region)
1317 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
1318 (when (featurep 'xemacs)
1319 (define-key map [(meta control h)] 'verilog-mark-defun)
1320 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1321 (define-key map "\M-\C-e" 'verilog-end-of-defun))
1322 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1323 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1324 (define-key map "\C-c\C-a" 'verilog-auto)
1325 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
1326 (define-key map "\C-c\C-p" 'verilog-preprocess)
1327 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1328 (define-key map "\C-c\C-e" 'verilog-expand-vector)
1329 (define-key map "\C-c\C-h" 'verilog-header)
1330 map)
1331 "Keymap used in Verilog mode.")
1332
1333 ;; menus
1334 (easy-menu-define
1335 verilog-menu verilog-mode-map "Menu for Verilog mode"
1336 (verilog-easy-menu-filter
1337 '("Verilog"
1338 ("Choose Compilation Action"
1339 ["None"
1340 (progn
1341 (setq verilog-tool nil)
1342 (verilog-set-compile-command))
1343 :style radio
1344 :selected (equal verilog-tool nil)
1345 :help "When invoking compilation, use compile-command"]
1346 ["Lint"
1347 (progn
1348 (setq verilog-tool 'verilog-linter)
1349 (verilog-set-compile-command))
1350 :style radio
1351 :selected (equal verilog-tool `verilog-linter)
1352 :help "When invoking compilation, use lint checker"]
1353 ["Coverage"
1354 (progn
1355 (setq verilog-tool 'verilog-coverage)
1356 (verilog-set-compile-command))
1357 :style radio
1358 :selected (equal verilog-tool `verilog-coverage)
1359 :help "When invoking compilation, annotate for coverage"]
1360 ["Simulator"
1361 (progn
1362 (setq verilog-tool 'verilog-simulator)
1363 (verilog-set-compile-command))
1364 :style radio
1365 :selected (equal verilog-tool `verilog-simulator)
1366 :help "When invoking compilation, interpret Verilog source"]
1367 ["Compiler"
1368 (progn
1369 (setq verilog-tool 'verilog-compiler)
1370 (verilog-set-compile-command))
1371 :style radio
1372 :selected (equal verilog-tool `verilog-compiler)
1373 :help "When invoking compilation, compile Verilog source"]
1374 ["Preprocessor"
1375 (progn
1376 (setq verilog-tool 'verilog-preprocessor)
1377 (verilog-set-compile-command))
1378 :style radio
1379 :selected (equal verilog-tool `verilog-preprocessor)
1380 :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
1381 )
1382 ("Move"
1383 ["Beginning of function" verilog-beg-of-defun
1384 :keys "C-M-a"
1385 :help "Move backward to the beginning of the current function or procedure"]
1386 ["End of function" verilog-end-of-defun
1387 :keys "C-M-e"
1388 :help "Move forward to the end of the current function or procedure"]
1389 ["Mark function" verilog-mark-defun
1390 :keys "C-M-h"
1391 :help "Mark the current Verilog function or procedure"]
1392 ["Goto function/module" verilog-goto-defun
1393 :help "Move to specified Verilog module/task/function"]
1394 ["Move to beginning of block" electric-verilog-backward-sexp
1395 :help "Move backward over one balanced expression"]
1396 ["Move to end of block" electric-verilog-forward-sexp
1397 :help "Move forward over one balanced expression"]
1398 )
1399 ("Comments"
1400 ["Comment Region" verilog-comment-region
1401 :help "Put marked area into a comment"]
1402 ["UnComment Region" verilog-uncomment-region
1403 :help "Uncomment an area commented with Comment Region"]
1404 ["Multi-line comment insert" verilog-star-comment
1405 :help "Insert Verilog /* */ comment at point"]
1406 ["Lint error to comment" verilog-lint-off
1407 :help "Convert a Verilog linter warning line into a disable statement"]
1408 )
1409 "----"
1410 ["Compile" compile
1411 :help "Perform compilation-action (above) on the current buffer"]
1412 ["AUTO, Save, Compile" verilog-auto-save-compile
1413 :help "Recompute AUTOs, save buffer, and compile"]
1414 ["Next Compile Error" next-error
1415 :help "Visit next compilation error message and corresponding source code"]
1416 ["Ignore Lint Warning at point" verilog-lint-off
1417 :help "Convert a Verilog linter warning line into a disable statement"]
1418 "----"
1419 ["Line up declarations around point" verilog-pretty-declarations
1420 :help "Line up declarations around point"]
1421 ["Line up equations around point" verilog-pretty-expr
1422 :help "Line up expressions around point"]
1423 ["Redo/insert comments on every end" verilog-label-be
1424 :help "Label matching begin ... end statements"]
1425 ["Expand [x:y] vector line" verilog-expand-vector
1426 :help "Take a signal vector on the current line and expand it to multiple lines"]
1427 ["Insert begin-end block" verilog-insert-block
1428 :help "Insert begin ... end"]
1429 ["Complete word" verilog-complete-word
1430 :help "Complete word at point"]
1431 "----"
1432 ["Recompute AUTOs" verilog-auto
1433 :help "Expand AUTO meta-comment statements"]
1434 ["Kill AUTOs" verilog-delete-auto
1435 :help "Remove AUTO expansions"]
1436 ["Diff AUTOs" verilog-diff-auto
1437 :help "Show differences in AUTO expansions"]
1438 ["Inject AUTOs" verilog-inject-auto
1439 :help "Inject AUTOs into legacy non-AUTO buffer"]
1440 ("AUTO Help..."
1441 ["AUTO General" (describe-function 'verilog-auto)
1442 :help "Help introduction on AUTOs"]
1443 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1444 :help "Help on verilog-library-flags"]
1445 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1446 :help "Help on verilog-library-directories"]
1447 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1448 :help "Help on verilog-library-files"]
1449 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1450 :help "Help on verilog-library-extensions"]
1451 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1452 :help "Help on reading `defines"]
1453 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1454 :help "Help on parsing `includes"]
1455 ["AUTOARG" (describe-function 'verilog-auto-arg)
1456 :help "Help on AUTOARG - declaring module port list"]
1457 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1458 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1459 ["AUTOASSIGNMODPORT" (describe-function 'verilog-auto-assign-modport)
1460 :help "Help on AUTOASSIGNMODPORT - creating assignments to/from modports"]
1461 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1462 :help "Help on AUTOINOUT - adding inouts from cells"]
1463 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
1464 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
1465 ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
1466 :help "Help on AUTOINOUTIN - copying i/o from another file as all inputs"]
1467 ["AUTOINOUTMODPORT" (describe-function 'verilog-auto-inout-modport)
1468 :help "Help on AUTOINOUTMODPORT - copying i/o from an interface modport"]
1469 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1470 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1471 ["AUTOINOUTPARAM" (describe-function 'verilog-auto-inout-param)
1472 :help "Help on AUTOINOUTPARAM - copying parameters from another file"]
1473 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1474 :help "Help on AUTOINPUT - adding inputs from cells"]
1475 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1476 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
1477 ["AUTOINST" (describe-function 'verilog-auto-inst)
1478 :help "Help on AUTOINST - adding pins for cells"]
1479 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1480 :help "Help on expanding Verilog-2001 .* pins"]
1481 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1482 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1483 ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
1484 :help "Help on AUTOLOGIC - declaring logic signals"]
1485 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1486 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1487 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1488 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1489 ["AUTOREG" (describe-function 'verilog-auto-reg)
1490 :help "Help on AUTOREG - declaring registers for non-wires"]
1491 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1492 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1493 ["AUTORESET" (describe-function 'verilog-auto-reset)
1494 :help "Help on AUTORESET - resetting always blocks"]
1495 ["AUTOSENSE or AS" (describe-function 'verilog-auto-sense)
1496 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1497 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1498 :help "Help on AUTOTIEOFF - tying off unused outputs"]
1499 ["AUTOUNDEF" (describe-function 'verilog-auto-undef)
1500 :help "Help on AUTOUNDEF - undefine all local defines"]
1501 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1502 :help "Help on AUTOUNUSED - terminating unused inputs"]
1503 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1504 :help "Help on AUTOWIRE - declaring wires for cells"]
1505 )
1506 "----"
1507 ["Submit bug report" verilog-submit-bug-report
1508 :help "Submit via mail a bug report on verilog-mode.el"]
1509 ["Version and FAQ" verilog-faq
1510 :help "Show the current version, and where to get the FAQ etc"]
1511 ["Customize Verilog Mode..." verilog-customize
1512 :help "Customize variables and other settings used by Verilog-Mode"]
1513 ["Customize Verilog Fonts & Colors" verilog-font-customize
1514 :help "Customize fonts used by Verilog-Mode."])))
1515
1516 (easy-menu-define
1517 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1518 (verilog-easy-menu-filter
1519 '("Statements"
1520 ["Header" verilog-sk-header
1521 :help "Insert a header block at the top of file"]
1522 ["Comment" verilog-sk-comment
1523 :help "Insert a comment block"]
1524 "----"
1525 ["Module" verilog-sk-module
1526 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1527 ["OVM Class" verilog-sk-ovm-class
1528 :help "Insert an OVM class block"]
1529 ["UVM Object" verilog-sk-uvm-object
1530 :help "Insert an UVM object block"]
1531 ["UVM Component" verilog-sk-uvm-component
1532 :help "Insert an UVM component block"]
1533 ["Primitive" verilog-sk-primitive
1534 :help "Insert a primitive .. (.. );.. endprimitive block"]
1535 "----"
1536 ["Input" verilog-sk-input
1537 :help "Insert an input declaration"]
1538 ["Output" verilog-sk-output
1539 :help "Insert an output declaration"]
1540 ["Inout" verilog-sk-inout
1541 :help "Insert an inout declaration"]
1542 ["Wire" verilog-sk-wire
1543 :help "Insert a wire declaration"]
1544 ["Reg" verilog-sk-reg
1545 :help "Insert a register declaration"]
1546 ["Define thing under point as a register" verilog-sk-define-signal
1547 :help "Define signal under point as a register at the top of the module"]
1548 "----"
1549 ["Initial" verilog-sk-initial
1550 :help "Insert an initial begin .. end block"]
1551 ["Always" verilog-sk-always
1552 :help "Insert an always @(AS) begin .. end block"]
1553 ["Function" verilog-sk-function
1554 :help "Insert a function .. begin .. end endfunction block"]
1555 ["Task" verilog-sk-task
1556 :help "Insert a task .. begin .. end endtask block"]
1557 ["Specify" verilog-sk-specify
1558 :help "Insert a specify .. endspecify block"]
1559 ["Generate" verilog-sk-generate
1560 :help "Insert a generate .. endgenerate block"]
1561 "----"
1562 ["Begin" verilog-sk-begin
1563 :help "Insert a begin .. end block"]
1564 ["If" verilog-sk-if
1565 :help "Insert an if (..) begin .. end block"]
1566 ["(if) else" verilog-sk-else-if
1567 :help "Insert an else if (..) begin .. end block"]
1568 ["For" verilog-sk-for
1569 :help "Insert a for (...) begin .. end block"]
1570 ["While" verilog-sk-while
1571 :help "Insert a while (...) begin .. end block"]
1572 ["Fork" verilog-sk-fork
1573 :help "Insert a fork begin .. end .. join block"]
1574 ["Repeat" verilog-sk-repeat
1575 :help "Insert a repeat (..) begin .. end block"]
1576 ["Case" verilog-sk-case
1577 :help "Insert a case block, prompting for details"]
1578 ["Casex" verilog-sk-casex
1579 :help "Insert a casex (...) item: begin.. end endcase block"]
1580 ["Casez" verilog-sk-casez
1581 :help "Insert a casez (...) item: begin.. end endcase block"])))
1582
1583 (defvar verilog-mode-abbrev-table nil
1584 "Abbrev table in use in Verilog-mode buffers.")
1585
1586 (define-abbrev-table 'verilog-mode-abbrev-table ())
1587 (verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
1588 (verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
1589 (verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
1590 (verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
1591 (verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
1592 (verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
1593 (verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
1594 (verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
1595 (verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
1596 (verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
1597 (verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
1598 (verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
1599 (verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
1600 (verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
1601 (verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
1602 (verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
1603 (verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
1604 (verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
1605 (verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
1606 (verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
1607 (verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
1608 (verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
1609 (verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
1610 (verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
1611 (verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
1612
1613 ;;
1614 ;; Macros
1615 ;;
1616
1617 (defsubst verilog-within-string ()
1618 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
1619
1620 (defsubst verilog-string-match-fold (regexp string &optional start)
1621 "Like `string-match', but use `verilog-case-fold'.
1622 Return index of start of first match for REGEXP in STRING, or nil.
1623 Matching ignores case if `verilog-case-fold' is non-nil.
1624 If third arg START is non-nil, start search at that index in STRING."
1625 (let ((case-fold-search verilog-case-fold))
1626 (string-match regexp string start)))
1627
1628 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1629 "Replace occurrences of FROM-STRING with TO-STRING.
1630 FIXEDCASE and LITERAL as in `replace-match`. STRING is what to replace.
1631 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1632 will break, as the o's continuously replace. xa -> x works ok though."
1633 ;; Hopefully soon to an Emacs built-in
1634 ;; Also note \ in the replacement prevent multiple replacements; IE
1635 ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
1636 ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
1637 (let ((start 0))
1638 (while (string-match from-string string start)
1639 (setq string (replace-match to-string fixedcase literal string)
1640 start (min (length string) (+ (match-beginning 0) (length to-string)))))
1641 string))
1642
1643 (defsubst verilog-string-remove-spaces (string)
1644 "Remove spaces surrounding STRING."
1645 (save-match-data
1646 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1647 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1648 string))
1649
1650 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1651 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1652 "Like `re-search-forward', but skips over match in comments or strings."
1653 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1654 (while (and
1655 (re-search-forward REGEXP BOUND NOERROR)
1656 (setq mdata (match-data))
1657 (and (verilog-skip-forward-comment-or-string)
1658 (progn
1659 (setq mdata '(nil nil))
1660 (if BOUND
1661 (< (point) BOUND)
1662 t)))))
1663 (store-match-data mdata)
1664 (match-end 0)))
1665
1666 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1667 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1668 "Like `re-search-backward', but skips over match in comments or strings."
1669 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1670 (while (and
1671 (re-search-backward REGEXP BOUND NOERROR)
1672 (setq mdata (match-data))
1673 (and (verilog-skip-backward-comment-or-string)
1674 (progn
1675 (setq mdata '(nil nil))
1676 (if BOUND
1677 (> (point) BOUND)
1678 t)))))
1679 (store-match-data mdata)
1680 (match-end 0)))
1681
1682 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1683 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1684 but trashes match data and is faster for REGEXP that doesn't match often.
1685 This uses `verilog-scan' and text properties to ignore comments,
1686 so there may be a large up front penalty for the first search."
1687 (let (pt)
1688 (while (and (not pt)
1689 (re-search-forward regexp bound noerror))
1690 (if (verilog-inside-comment-or-string-p)
1691 (re-search-forward "[/\"\n]" nil t) ;; Only way a comment or quote can end
1692 (setq pt (match-end 0))))
1693 pt))
1694
1695 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1696 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1697 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1698 but trashes match data and is faster for REGEXP that doesn't match often.
1699 This uses `verilog-scan' and text properties to ignore comments,
1700 so there may be a large up front penalty for the first search."
1701 (let (pt)
1702 (while (and (not pt)
1703 (re-search-backward regexp bound noerror))
1704 (if (verilog-inside-comment-or-string-p)
1705 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
1706 (setq pt (match-beginning 0))))
1707 pt))
1708
1709 (defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
1710 "Like `re-search-forward', but first search for SUBSTR constant.
1711 Then searched for the normal REGEXP (which contains SUBSTR), with given
1712 BOUND and NOERROR. The REGEXP must fit within a single line.
1713 This speeds up complicated regexp matches."
1714 ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
1715 ;; thus require matches to be on one line, and use beginning-of-line.
1716 (let (done)
1717 (while (and (not done)
1718 (search-forward substr bound noerror))
1719 (save-excursion
1720 (beginning-of-line)
1721 (setq done (re-search-forward regexp (point-at-eol) noerror)))
1722 (unless (and (<= (match-beginning 0) (point))
1723 (>= (match-end 0) (point)))
1724 (setq done nil)))
1725 (when done (goto-char done))
1726 done))
1727 ;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1728
1729 (defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
1730 "Like `re-search-backward', but first search for SUBSTR constant.
1731 Then searched for the normal REGEXP (which contains SUBSTR), with given
1732 BOUND and NOERROR. The REGEXP must fit within a single line.
1733 This speeds up complicated regexp matches."
1734 ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
1735 ;; thus require matches to be on one line, and use beginning-of-line.
1736 (let (done)
1737 (while (and (not done)
1738 (search-backward substr bound noerror))
1739 (save-excursion
1740 (end-of-line)
1741 (setq done (re-search-backward regexp (point-at-bol) noerror)))
1742 (unless (and (<= (match-beginning 0) (point))
1743 (>= (match-end 0) (point)))
1744 (setq done nil)))
1745 (when done (goto-char done))
1746 done))
1747 ;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1748
1749 (defun verilog-delete-trailing-whitespace ()
1750 "Delete trailing spaces or tabs, but not newlines nor linefeeds.
1751 Also add missing final newline.
1752
1753 To call this from the command line, see \\[verilog-batch-diff-auto].
1754
1755 To call on \\[verilog-auto], set `verilog-auto-delete-trailing-whitespace'."
1756 ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
1757 (save-excursion
1758 (goto-char (point-min))
1759 (while (re-search-forward "[ \t]+$" nil t) ;; Not syntactic WS as no formfeed
1760 (replace-match "" nil nil))
1761 (goto-char (point-max))
1762 (unless (bolp) (insert "\n"))))
1763
1764 (defvar compile-command)
1765 (defvar create-lockfiles) ;; Emacs 24
1766
1767 ;; compilation program
1768 (defun verilog-set-compile-command ()
1769 "Function to compute shell command to compile Verilog.
1770
1771 This reads `verilog-tool' and sets `compile-command'. This specifies the
1772 program that executes when you type \\[compile] or
1773 \\[verilog-auto-save-compile].
1774
1775 By default `verilog-tool' uses a Makefile if one exists in the
1776 current directory. If not, it is set to the `verilog-linter',
1777 `verilog-compiler', `verilog-coverage', `verilog-preprocessor',
1778 or `verilog-simulator' variables, as selected with the Verilog ->
1779 \"Choose Compilation Action\" menu.
1780
1781 You should set `verilog-tool' or the other variables to the path and
1782 arguments for your Verilog simulator. For example:
1783 \"vcs -p123 -O\"
1784 or a string like:
1785 \"(cd /tmp; surecov %s)\".
1786
1787 In the former case, the path to the current buffer is concat'ed to the
1788 value of `verilog-tool'; in the later, the path to the current buffer is
1789 substituted for the %s.
1790
1791 Where __FLAGS__ appears in the string `verilog-current-flags'
1792 will be substituted.
1793
1794 Where __FILE__ appears in the string, the variable
1795 `buffer-file-name' of the current buffer, without the directory
1796 portion, will be substituted."
1797 (interactive)
1798 (cond
1799 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1800 (file-exists-p "Makefile"))
1801 (set (make-local-variable 'compile-command) "make "))
1802 (t
1803 (set (make-local-variable 'compile-command)
1804 (if verilog-tool
1805 (if (string-match "%s" (eval verilog-tool))
1806 (format (eval verilog-tool) (or buffer-file-name ""))
1807 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1808 ""))))
1809 (verilog-modify-compile-command))
1810
1811 (defun verilog-expand-command (command)
1812 "Replace meta-information in COMMAND and return it.
1813 Where __FLAGS__ appears in the string `verilog-current-flags'
1814 will be substituted. Where __FILE__ appears in the string, the
1815 current buffer's file-name, without the directory portion, will
1816 be substituted."
1817 (setq command (verilog-string-replace-matches
1818 ;; Note \\b only works if under verilog syntax table
1819 "\\b__FLAGS__\\b" (verilog-current-flags)
1820 t t command))
1821 (setq command (verilog-string-replace-matches
1822 "\\b__FILE__\\b" (file-name-nondirectory
1823 (or (buffer-file-name) ""))
1824 t t command))
1825 command)
1826
1827 (defun verilog-modify-compile-command ()
1828 "Update `compile-command' using `verilog-expand-command'."
1829 (when (and
1830 (stringp compile-command)
1831 (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
1832 (set (make-local-variable 'compile-command)
1833 (verilog-expand-command compile-command))))
1834
1835 (if (featurep 'xemacs)
1836 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1837 (defun verilog-error-regexp-add-xemacs ()
1838 "Teach XEmacs about verilog errors.
1839 Called by `compilation-mode-hook'. This allows \\[next-error] to
1840 find the errors."
1841 (interactive)
1842 (if (boundp 'compilation-error-regexp-systems-alist)
1843 (if (and
1844 (not (equal compilation-error-regexp-systems-list 'all))
1845 (not (member compilation-error-regexp-systems-list 'verilog)))
1846 (push 'verilog compilation-error-regexp-systems-list)))
1847 (if (boundp 'compilation-error-regexp-alist-alist)
1848 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1849 (setcdr compilation-error-regexp-alist-alist
1850 (cons verilog-error-regexp-xemacs-alist
1851 (cdr compilation-error-regexp-alist-alist)))))
1852 (if (boundp 'compilation-font-lock-keywords)
1853 (progn
1854 (set (make-local-variable 'compilation-font-lock-keywords)
1855 verilog-error-font-lock-keywords)
1856 (font-lock-set-defaults)))
1857 ;; Need to re-run compilation-error-regexp builder
1858 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1859 (compilation-build-compilation-error-regexp-alist))
1860 ))
1861
1862 ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
1863 (defun verilog-error-regexp-add-emacs ()
1864 "Tell Emacs compile that we are Verilog.
1865 Called by `compilation-mode-hook'. This allows \\[next-error] to
1866 find the errors."
1867 (interactive)
1868 (if (boundp 'compilation-error-regexp-alist-alist)
1869 (progn
1870 (if (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1871 (mapcar
1872 (lambda (item)
1873 (push (car item) compilation-error-regexp-alist)
1874 (push item compilation-error-regexp-alist-alist)
1875 )
1876 verilog-error-regexp-emacs-alist)))))
1877
1878 (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1879 (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
1880
1881 (defconst verilog-directive-re
1882 (eval-when-compile
1883 (verilog-regexp-words
1884 '(
1885 "`case" "`default" "`define" "`else" "`elsif" "`endfor" "`endif"
1886 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1887 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1888 "`time_scale" "`undef" "`while" ))))
1889
1890 (defconst verilog-directive-re-1
1891 (concat "[ \t]*" verilog-directive-re))
1892
1893 (defconst verilog-directive-begin
1894 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1895
1896 (defconst verilog-directive-middle
1897 "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
1898
1899 (defconst verilog-directive-end
1900 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1901
1902 (defconst verilog-ovm-begin-re
1903 (eval-when-compile
1904 (verilog-regexp-opt
1905 '(
1906 "`ovm_component_utils_begin"
1907 "`ovm_component_param_utils_begin"
1908 "`ovm_field_utils_begin"
1909 "`ovm_object_utils_begin"
1910 "`ovm_object_param_utils_begin"
1911 "`ovm_sequence_utils_begin"
1912 "`ovm_sequencer_utils_begin"
1913 ) nil )))
1914
1915 (defconst verilog-ovm-end-re
1916 (eval-when-compile
1917 (verilog-regexp-opt
1918 '(
1919 "`ovm_component_utils_end"
1920 "`ovm_field_utils_end"
1921 "`ovm_object_utils_end"
1922 "`ovm_sequence_utils_end"
1923 "`ovm_sequencer_utils_end"
1924 ) nil )))
1925
1926 (defconst verilog-uvm-begin-re
1927 (eval-when-compile
1928 (verilog-regexp-opt
1929 '(
1930 "`uvm_component_utils_begin"
1931 "`uvm_component_param_utils_begin"
1932 "`uvm_field_utils_begin"
1933 "`uvm_object_utils_begin"
1934 "`uvm_object_param_utils_begin"
1935 "`uvm_sequence_utils_begin"
1936 "`uvm_sequencer_utils_begin"
1937 ) nil )))
1938
1939 (defconst verilog-uvm-end-re
1940 (eval-when-compile
1941 (verilog-regexp-opt
1942 '(
1943 "`uvm_component_utils_end"
1944 "`uvm_field_utils_end"
1945 "`uvm_object_utils_end"
1946 "`uvm_sequence_utils_end"
1947 "`uvm_sequencer_utils_end"
1948 ) nil )))
1949
1950 (defconst verilog-vmm-begin-re
1951 (eval-when-compile
1952 (verilog-regexp-opt
1953 '(
1954 "`vmm_data_member_begin"
1955 "`vmm_env_member_begin"
1956 "`vmm_scenario_member_begin"
1957 "`vmm_subenv_member_begin"
1958 "`vmm_xactor_member_begin"
1959 ) nil ) ) )
1960
1961 (defconst verilog-vmm-end-re
1962 (eval-when-compile
1963 (verilog-regexp-opt
1964 '(
1965 "`vmm_data_member_end"
1966 "`vmm_env_member_end"
1967 "`vmm_scenario_member_end"
1968 "`vmm_subenv_member_end"
1969 "`vmm_xactor_member_end"
1970 ) nil ) ) )
1971
1972 (defconst verilog-vmm-statement-re
1973 (eval-when-compile
1974 (verilog-regexp-opt
1975 '(
1976 ;; "`vmm_xactor_member_enum_array"
1977 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
1978 ;; "`vmm_xactor_member_scalar_array"
1979 ;; "`vmm_xactor_member_scalar"
1980 ) nil )))
1981
1982 (defconst verilog-ovm-statement-re
1983 (eval-when-compile
1984 (verilog-regexp-opt
1985 '(
1986 ;; Statements
1987 "`DUT_ERROR"
1988 "`MESSAGE"
1989 "`dut_error"
1990 "`message"
1991 "`ovm_analysis_imp_decl"
1992 "`ovm_blocking_get_imp_decl"
1993 "`ovm_blocking_get_peek_imp_decl"
1994 "`ovm_blocking_master_imp_decl"
1995 "`ovm_blocking_peek_imp_decl"
1996 "`ovm_blocking_put_imp_decl"
1997 "`ovm_blocking_slave_imp_decl"
1998 "`ovm_blocking_transport_imp_decl"
1999 "`ovm_component_registry"
2000 "`ovm_component_registry_param"
2001 "`ovm_component_utils"
2002 "`ovm_create"
2003 "`ovm_create_seq"
2004 "`ovm_declare_sequence_lib"
2005 "`ovm_do"
2006 "`ovm_do_seq"
2007 "`ovm_do_seq_with"
2008 "`ovm_do_with"
2009 "`ovm_error"
2010 "`ovm_fatal"
2011 "`ovm_field_aa_int_byte"
2012 "`ovm_field_aa_int_byte_unsigned"
2013 "`ovm_field_aa_int_int"
2014 "`ovm_field_aa_int_int_unsigned"
2015 "`ovm_field_aa_int_integer"
2016 "`ovm_field_aa_int_integer_unsigned"
2017 "`ovm_field_aa_int_key"
2018 "`ovm_field_aa_int_longint"
2019 "`ovm_field_aa_int_longint_unsigned"
2020 "`ovm_field_aa_int_shortint"
2021 "`ovm_field_aa_int_shortint_unsigned"
2022 "`ovm_field_aa_int_string"
2023 "`ovm_field_aa_object_int"
2024 "`ovm_field_aa_object_string"
2025 "`ovm_field_aa_string_int"
2026 "`ovm_field_aa_string_string"
2027 "`ovm_field_array_int"
2028 "`ovm_field_array_object"
2029 "`ovm_field_array_string"
2030 "`ovm_field_enum"
2031 "`ovm_field_event"
2032 "`ovm_field_int"
2033 "`ovm_field_object"
2034 "`ovm_field_queue_int"
2035 "`ovm_field_queue_object"
2036 "`ovm_field_queue_string"
2037 "`ovm_field_sarray_int"
2038 "`ovm_field_string"
2039 "`ovm_field_utils"
2040 "`ovm_file"
2041 "`ovm_get_imp_decl"
2042 "`ovm_get_peek_imp_decl"
2043 "`ovm_info"
2044 "`ovm_info1"
2045 "`ovm_info2"
2046 "`ovm_info3"
2047 "`ovm_info4"
2048 "`ovm_line"
2049 "`ovm_master_imp_decl"
2050 "`ovm_msg_detail"
2051 "`ovm_non_blocking_transport_imp_decl"
2052 "`ovm_nonblocking_get_imp_decl"
2053 "`ovm_nonblocking_get_peek_imp_decl"
2054 "`ovm_nonblocking_master_imp_decl"
2055 "`ovm_nonblocking_peek_imp_decl"
2056 "`ovm_nonblocking_put_imp_decl"
2057 "`ovm_nonblocking_slave_imp_decl"
2058 "`ovm_object_registry"
2059 "`ovm_object_registry_param"
2060 "`ovm_object_utils"
2061 "`ovm_peek_imp_decl"
2062 "`ovm_phase_func_decl"
2063 "`ovm_phase_task_decl"
2064 "`ovm_print_aa_int_object"
2065 "`ovm_print_aa_string_int"
2066 "`ovm_print_aa_string_object"
2067 "`ovm_print_aa_string_string"
2068 "`ovm_print_array_int"
2069 "`ovm_print_array_object"
2070 "`ovm_print_array_string"
2071 "`ovm_print_object_queue"
2072 "`ovm_print_queue_int"
2073 "`ovm_print_string_queue"
2074 "`ovm_put_imp_decl"
2075 "`ovm_rand_send"
2076 "`ovm_rand_send_with"
2077 "`ovm_send"
2078 "`ovm_sequence_utils"
2079 "`ovm_slave_imp_decl"
2080 "`ovm_transport_imp_decl"
2081 "`ovm_update_sequence_lib"
2082 "`ovm_update_sequence_lib_and_item"
2083 "`ovm_warning"
2084 "`static_dut_error"
2085 "`static_message") nil )))
2086
2087 (defconst verilog-uvm-statement-re
2088 (eval-when-compile
2089 (verilog-regexp-opt
2090 '(
2091 ;; Statements
2092 "`uvm_analysis_imp_decl"
2093 "`uvm_blocking_get_imp_decl"
2094 "`uvm_blocking_get_peek_imp_decl"
2095 "`uvm_blocking_master_imp_decl"
2096 "`uvm_blocking_peek_imp_decl"
2097 "`uvm_blocking_put_imp_decl"
2098 "`uvm_blocking_slave_imp_decl"
2099 "`uvm_blocking_transport_imp_decl"
2100 "`uvm_component_param_utils"
2101 "`uvm_component_registry"
2102 "`uvm_component_registry_param"
2103 "`uvm_component_utils"
2104 "`uvm_create"
2105 "`uvm_create_on"
2106 "`uvm_create_seq" ;; Undocumented in 1.1
2107 "`uvm_declare_p_sequencer"
2108 "`uvm_declare_sequence_lib" ;; Deprecated in 1.1
2109 "`uvm_do"
2110 "`uvm_do_callbacks"
2111 "`uvm_do_callbacks_exit_on"
2112 "`uvm_do_obj_callbacks"
2113 "`uvm_do_obj_callbacks_exit_on"
2114 "`uvm_do_on"
2115 "`uvm_do_on_pri"
2116 "`uvm_do_on_pri_with"
2117 "`uvm_do_on_with"
2118 "`uvm_do_pri"
2119 "`uvm_do_pri_with"
2120 "`uvm_do_seq" ;; Undocumented in 1.1
2121 "`uvm_do_seq_with" ;; Undocumented in 1.1
2122 "`uvm_do_with"
2123 "`uvm_error"
2124 "`uvm_error_context"
2125 "`uvm_fatal"
2126 "`uvm_fatal_context"
2127 "`uvm_field_aa_int_byte"
2128 "`uvm_field_aa_int_byte_unsigned"
2129 "`uvm_field_aa_int_enum"
2130 "`uvm_field_aa_int_int"
2131 "`uvm_field_aa_int_int_unsigned"
2132 "`uvm_field_aa_int_integer"
2133 "`uvm_field_aa_int_integer_unsigned"
2134 "`uvm_field_aa_int_key"
2135 "`uvm_field_aa_int_longint"
2136 "`uvm_field_aa_int_longint_unsigned"
2137 "`uvm_field_aa_int_shortint"
2138 "`uvm_field_aa_int_shortint_unsigned"
2139 "`uvm_field_aa_int_string"
2140 "`uvm_field_aa_object_int"
2141 "`uvm_field_aa_object_string"
2142 "`uvm_field_aa_string_int"
2143 "`uvm_field_aa_string_string"
2144 "`uvm_field_array_enum"
2145 "`uvm_field_array_int"
2146 "`uvm_field_array_object"
2147 "`uvm_field_array_string"
2148 "`uvm_field_enum"
2149 "`uvm_field_event"
2150 "`uvm_field_int"
2151 "`uvm_field_object"
2152 "`uvm_field_queue_enum"
2153 "`uvm_field_queue_int"
2154 "`uvm_field_queue_object"
2155 "`uvm_field_queue_string"
2156 "`uvm_field_real"
2157 "`uvm_field_sarray_enum"
2158 "`uvm_field_sarray_int"
2159 "`uvm_field_sarray_object"
2160 "`uvm_field_sarray_string"
2161 "`uvm_field_string"
2162 "`uvm_field_utils"
2163 "`uvm_file" ;; Undocumented in 1.1, use `__FILE__
2164 "`uvm_get_imp_decl"
2165 "`uvm_get_peek_imp_decl"
2166 "`uvm_info"
2167 "`uvm_info_context"
2168 "`uvm_line" ;; Undocumented in 1.1, use `__LINE__
2169 "`uvm_master_imp_decl"
2170 "`uvm_non_blocking_transport_imp_decl" ;; Deprecated in 1.1
2171 "`uvm_nonblocking_get_imp_decl"
2172 "`uvm_nonblocking_get_peek_imp_decl"
2173 "`uvm_nonblocking_master_imp_decl"
2174 "`uvm_nonblocking_peek_imp_decl"
2175 "`uvm_nonblocking_put_imp_decl"
2176 "`uvm_nonblocking_slave_imp_decl"
2177 "`uvm_nonblocking_transport_imp_decl"
2178 "`uvm_object_param_utils"
2179 "`uvm_object_registry"
2180 "`uvm_object_registry_param" ;; Undocumented in 1.1
2181 "`uvm_object_utils"
2182 "`uvm_pack_array"
2183 "`uvm_pack_arrayN"
2184 "`uvm_pack_enum"
2185 "`uvm_pack_enumN"
2186 "`uvm_pack_int"
2187 "`uvm_pack_intN"
2188 "`uvm_pack_queue"
2189 "`uvm_pack_queueN"
2190 "`uvm_pack_real"
2191 "`uvm_pack_sarray"
2192 "`uvm_pack_sarrayN"
2193 "`uvm_pack_string"
2194 "`uvm_peek_imp_decl"
2195 "`uvm_put_imp_decl"
2196 "`uvm_rand_send"
2197 "`uvm_rand_send_pri"
2198 "`uvm_rand_send_pri_with"
2199 "`uvm_rand_send_with"
2200 "`uvm_record_attribute"
2201 "`uvm_record_field"
2202 "`uvm_register_cb"
2203 "`uvm_send"
2204 "`uvm_send_pri"
2205 "`uvm_sequence_utils" ;; Deprecated in 1.1
2206 "`uvm_set_super_type"
2207 "`uvm_slave_imp_decl"
2208 "`uvm_transport_imp_decl"
2209 "`uvm_unpack_array"
2210 "`uvm_unpack_arrayN"
2211 "`uvm_unpack_enum"
2212 "`uvm_unpack_enumN"
2213 "`uvm_unpack_int"
2214 "`uvm_unpack_intN"
2215 "`uvm_unpack_queue"
2216 "`uvm_unpack_queueN"
2217 "`uvm_unpack_real"
2218 "`uvm_unpack_sarray"
2219 "`uvm_unpack_sarrayN"
2220 "`uvm_unpack_string"
2221 "`uvm_update_sequence_lib" ;; Deprecated in 1.1
2222 "`uvm_update_sequence_lib_and_item" ;; Deprecated in 1.1
2223 "`uvm_warning"
2224 "`uvm_warning_context") nil )))
2225
2226
2227 ;;
2228 ;; Regular expressions used to calculate indent, etc.
2229 ;;
2230 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
2231 ;; Want to match
2232 ;; aa :
2233 ;; aa,bb :
2234 ;; a[34:32] :
2235 ;; a,
2236 ;; b :
2237 (defconst verilog-assignment-operator-re
2238 (eval-when-compile
2239 (verilog-regexp-opt
2240 `(
2241 ;; blocking assignment_operator
2242 "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
2243 ;; non blocking assignment operator
2244 "<="
2245 ;; comparison
2246 "==" "!=" "===" "!===" "<=" ">=" "==\?" "!=\?"
2247 ;; event_trigger
2248 "->" "->>"
2249 ;; property_expr
2250 "|->" "|=>"
2251 ;; Is this a legal verilog operator?
2252 ":="
2253 ) 't
2254 )))
2255 (defconst verilog-assignment-operation-re
2256 (concat
2257 ; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
2258 ; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
2259 "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
2260
2261 (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
2262 (defconst verilog-property-re
2263 (concat "\\(" verilog-label-re "\\)?"
2264 "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
2265 ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
2266
2267 (defconst verilog-no-indent-begin-re
2268 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\|always_comb\\|always_ff\\|always_latch\\)\\>")
2269
2270 (defconst verilog-ends-re
2271 ;; Parenthesis indicate type of keyword found
2272 (concat
2273 "\\(\\<else\\>\\)\\|" ; 1
2274 "\\(\\<if\\>\\)\\|" ; 2
2275 "\\(\\<assert\\>\\)\\|" ; 3
2276 "\\(\\<end\\>\\)\\|" ; 3.1
2277 "\\(\\<endcase\\>\\)\\|" ; 4
2278 "\\(\\<endfunction\\>\\)\\|" ; 5
2279 "\\(\\<endtask\\>\\)\\|" ; 6
2280 "\\(\\<endspecify\\>\\)\\|" ; 7
2281 "\\(\\<endtable\\>\\)\\|" ; 8
2282 "\\(\\<endgenerate\\>\\)\\|" ; 9
2283 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
2284 "\\(\\<endclass\\>\\)\\|" ; 11
2285 "\\(\\<endgroup\\>\\)\\|" ; 12
2286 ;; VMM
2287 "\\(\\<`vmm_data_member_end\\>\\)\\|"
2288 "\\(\\<`vmm_env_member_end\\>\\)\\|"
2289 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
2290 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
2291 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
2292 ;; OVM
2293 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
2294 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
2295 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
2296 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
2297 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
2298 ;; UVM
2299 "\\(\\<`uvm_component_utils_end\\>\\)\\|"
2300 "\\(\\<`uvm_field_utils_end\\>\\)\\|"
2301 "\\(\\<`uvm_object_utils_end\\>\\)\\|"
2302 "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
2303 "\\(\\<`uvm_sequencer_utils_end\\>\\)"
2304 ))
2305
2306 (defconst verilog-auto-end-comment-lines-re
2307 ;; Matches to names in this list cause auto-end-commenting
2308 (concat "\\("
2309 verilog-directive-re "\\)\\|\\("
2310 (eval-when-compile
2311 (verilog-regexp-words
2312 `( "begin"
2313 "else"
2314 "end"
2315 "endcase"
2316 "endclass"
2317 "endclocking"
2318 "endgroup"
2319 "endfunction"
2320 "endmodule"
2321 "endprogram"
2322 "endprimitive"
2323 "endinterface"
2324 "endpackage"
2325 "endsequence"
2326 "endspecify"
2327 "endtable"
2328 "endtask"
2329 "join"
2330 "join_any"
2331 "join_none"
2332 "module"
2333 "macromodule"
2334 "primitive"
2335 "interface"
2336 "package")))
2337 "\\)"))
2338
2339 ;;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
2340 ;;; verilog-end-block-ordered-re matches exactly the same strings.
2341 (defconst verilog-end-block-ordered-re
2342 ;; Parenthesis indicate type of keyword found
2343 (concat "\\(\\<endcase\\>\\)\\|" ; 1
2344 "\\(\\<end\\>\\)\\|" ; 2
2345 "\\(\\<end" ; 3, but not used
2346 "\\(" ; 4, but not used
2347 "\\(function\\)\\|" ; 5
2348 "\\(task\\)\\|" ; 6
2349 "\\(module\\)\\|" ; 7
2350 "\\(primitive\\)\\|" ; 8
2351 "\\(interface\\)\\|" ; 9
2352 "\\(package\\)\\|" ; 10
2353 "\\(class\\)\\|" ; 11
2354 "\\(group\\)\\|" ; 12
2355 "\\(program\\)\\|" ; 13
2356 "\\(sequence\\)\\|" ; 14
2357 "\\(clocking\\)\\|" ; 15
2358 "\\)\\>\\)"))
2359 (defconst verilog-end-block-re
2360 (eval-when-compile
2361 (verilog-regexp-words
2362
2363 `("end" ;; closes begin
2364 "endcase" ;; closes any of case, casex casez or randcase
2365 "join" "join_any" "join_none" ;; closes fork
2366 "endclass"
2367 "endtable"
2368 "endspecify"
2369 "endfunction"
2370 "endgenerate"
2371 "endtask"
2372 "endgroup"
2373 "endproperty"
2374 "endinterface"
2375 "endpackage"
2376 "endprogram"
2377 "endsequence"
2378 "endclocking"
2379 ;; OVM
2380 "`ovm_component_utils_end"
2381 "`ovm_field_utils_end"
2382 "`ovm_object_utils_end"
2383 "`ovm_sequence_utils_end"
2384 "`ovm_sequencer_utils_end"
2385 ;; UVM
2386 "`uvm_component_utils_end"
2387 "`uvm_field_utils_end"
2388 "`uvm_object_utils_end"
2389 "`uvm_sequence_utils_end"
2390 "`uvm_sequencer_utils_end"
2391 ;; VMM
2392 "`vmm_data_member_end"
2393 "`vmm_env_member_end"
2394 "`vmm_scenario_member_end"
2395 "`vmm_subenv_member_end"
2396 "`vmm_xactor_member_end"
2397 ))))
2398
2399
2400 (defconst verilog-endcomment-reason-re
2401 ;; Parenthesis indicate type of keyword found
2402 (concat
2403 "\\(\\<begin\\>\\)\\|" ; 1
2404 "\\(\\<else\\>\\)\\|" ; 2
2405 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
2406 "\\(\\<always_comb\\>\\(\[ \t\]*@\\)?\\)\\|" ; 4
2407 "\\(\\<always_ff\\>\\(\[ \t\]*@\\)?\\)\\|" ; 5
2408 "\\(\\<always_latch\\>\\(\[ \t\]*@\\)?\\)\\|" ; 6
2409 "\\(\\<fork\\>\\)\\|" ; 7
2410 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
2411 "\\(\\<if\\>\\)\\|"
2412 verilog-property-re "\\|"
2413 "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
2414 "\\(\\<clocking\\>\\)\\|"
2415 "\\(\\<task\\>\\)\\|"
2416 "\\(\\<function\\>\\)\\|"
2417 "\\(\\<initial\\>\\)\\|"
2418 "\\(\\<interface\\>\\)\\|"
2419 "\\(\\<package\\>\\)\\|"
2420 "\\(\\<final\\>\\)\\|"
2421 "\\(@\\)\\|"
2422 "\\(\\<while\\>\\)\\|"
2423 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
2424 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
2425 "#"))
2426
2427 (defconst verilog-named-block-re "begin[ \t]*:")
2428
2429 ;; These words begin a block which can occur inside a module which should be indented,
2430 ;; and closed with the respective word from the end-block list
2431
2432 (defconst verilog-beg-block-re
2433 (eval-when-compile
2434 (verilog-regexp-words
2435 `("begin"
2436 "case" "casex" "casez" "randcase"
2437 "clocking"
2438 "generate"
2439 "fork"
2440 "function"
2441 "property"
2442 "specify"
2443 "table"
2444 "task"
2445 ;; OVM
2446 "`ovm_component_utils_begin"
2447 "`ovm_component_param_utils_begin"
2448 "`ovm_field_utils_begin"
2449 "`ovm_object_utils_begin"
2450 "`ovm_object_param_utils_begin"
2451 "`ovm_sequence_utils_begin"
2452 "`ovm_sequencer_utils_begin"
2453 ;; UVM
2454 "`uvm_component_utils_begin"
2455 "`uvm_component_param_utils_begin"
2456 "`uvm_field_utils_begin"
2457 "`uvm_object_utils_begin"
2458 "`uvm_object_param_utils_begin"
2459 "`uvm_sequence_utils_begin"
2460 "`uvm_sequencer_utils_begin"
2461 ;; VMM
2462 "`vmm_data_member_begin"
2463 "`vmm_env_member_begin"
2464 "`vmm_scenario_member_begin"
2465 "`vmm_subenv_member_begin"
2466 "`vmm_xactor_member_begin"
2467 ))))
2468 ;; These are the same words, in a specific order in the regular
2469 ;; expression so that matching will work nicely for
2470 ;; verilog-forward-sexp and verilog-calc-indent
2471 (defconst verilog-beg-block-re-ordered
2472 ( concat "\\(\\<begin\\>\\)" ;1
2473 "\\|\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
2474 "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
2475 "\\|\\(\\<class\\>\\)" ;6
2476 "\\|\\(\\<table\\>\\)" ;7
2477 "\\|\\(\\<specify\\>\\)" ;8
2478 "\\|\\(\\<function\\>\\)" ;9
2479 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
2480 "\\|\\(\\<task\\>\\)" ;14
2481 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
2482 "\\|\\(\\<generate\\>\\)" ;18
2483 "\\|\\(\\<covergroup\\>\\)" ;16 20
2484 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
2485 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
2486 "\\|\\(\\<clocking\\>\\)" ;22 27
2487 "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;28
2488 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
2489 ;;
2490 ))
2491
2492 (defconst verilog-end-block-ordered-rry
2493 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2494 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
2495 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2496 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
2497 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
2498 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
2499 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
2500 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
2501 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
2502 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
2503 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
2504 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
2505 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
2506 ] )
2507
2508 (defconst verilog-nameable-item-re
2509 (eval-when-compile
2510 (verilog-regexp-words
2511 `("begin"
2512 "fork"
2513 "join" "join_any" "join_none"
2514 "end"
2515 "endcase"
2516 "endconfig"
2517 "endclass"
2518 "endclocking"
2519 "endfunction"
2520 "endgenerate"
2521 "endmodule"
2522 "endprimitive"
2523 "endinterface"
2524 "endpackage"
2525 "endspecify"
2526 "endtable"
2527 "endtask" )
2528 )))
2529
2530 (defconst verilog-declaration-opener
2531 (eval-when-compile
2532 (verilog-regexp-words
2533 `("module" "begin" "task" "function"))))
2534
2535 (defconst verilog-declaration-prefix-re
2536 (eval-when-compile
2537 (verilog-regexp-words
2538 `(
2539 ;; port direction
2540 "inout" "input" "output" "ref"
2541 ;; changeableness
2542 "const" "static" "protected" "local"
2543 ;; parameters
2544 "localparam" "parameter" "var"
2545 ;; type creation
2546 "typedef"
2547 ))))
2548 (defconst verilog-declaration-core-re
2549 (eval-when-compile
2550 (verilog-regexp-words
2551 `(
2552 ;; port direction (by themselves)
2553 "inout" "input" "output"
2554 ;; integer_atom_type
2555 "byte" "shortint" "int" "longint" "integer" "time"
2556 ;; integer_vector_type
2557 "bit" "logic" "reg"
2558 ;; non_integer_type
2559 "shortreal" "real" "realtime"
2560 ;; net_type
2561 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
2562 ;; misc
2563 "string" "event" "chandle" "virtual" "enum" "genvar"
2564 "struct" "union"
2565 ;; builtin classes
2566 "mailbox" "semaphore"
2567 ))))
2568 (defconst verilog-declaration-re
2569 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
2570 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
2571 (defconst verilog-optional-signed-re "\\s-*\\(signed\\)?")
2572 (defconst verilog-optional-signed-range-re
2573 (concat
2574 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
2575 (defconst verilog-macroexp-re "`\\sw+")
2576
2577 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
2578 (defconst verilog-declaration-re-2-no-macro
2579 (concat "\\s-*" verilog-declaration-re
2580 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2581 "\\)?"))
2582 (defconst verilog-declaration-re-2-macro
2583 (concat "\\s-*" verilog-declaration-re
2584 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2585 "\\|\\(" verilog-macroexp-re "\\)"
2586 "\\)?"))
2587 (defconst verilog-declaration-re-1-macro
2588 (concat "^" verilog-declaration-re-2-macro))
2589
2590 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2591
2592 (defconst verilog-defun-re
2593 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2594 (defconst verilog-end-defun-re
2595 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2596 (defconst verilog-zero-indent-re
2597 (concat verilog-defun-re "\\|" verilog-end-defun-re))
2598 (defconst verilog-inst-comment-re
2599 (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
2600
2601 (defconst verilog-behavioral-block-beg-re
2602 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2603 "function" "task"))))
2604 (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
2605 (defconst verilog-in-constraint-re ;; keywords legal in constraint blocks starting a statement/block
2606 (eval-when-compile (verilog-regexp-words `("if" "else" "solve" "foreach"))))
2607
2608 (defconst verilog-indent-re
2609 (eval-when-compile
2610 (verilog-regexp-words
2611 `(
2612 "{"
2613 "always" "always_latch" "always_ff" "always_comb"
2614 "begin" "end"
2615 ; "unique" "priority"
2616 "case" "casex" "casez" "randcase" "endcase"
2617 "class" "endclass"
2618 "clocking" "endclocking"
2619 "config" "endconfig"
2620 "covergroup" "endgroup"
2621 "fork" "join" "join_any" "join_none"
2622 "function" "endfunction"
2623 "final"
2624 "generate" "endgenerate"
2625 "initial"
2626 "interface" "endinterface"
2627 "module" "macromodule" "endmodule"
2628 "package" "endpackage"
2629 "primitive" "endprimitive"
2630 "program" "endprogram"
2631 "property" "endproperty"
2632 "sequence" "randsequence" "endsequence"
2633 "specify" "endspecify"
2634 "table" "endtable"
2635 "task" "endtask"
2636 "virtual"
2637 "`case"
2638 "`default"
2639 "`define" "`undef"
2640 "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
2641 "`while" "`endwhile"
2642 "`for" "`endfor"
2643 "`format"
2644 "`include"
2645 "`let"
2646 "`protect" "`endprotect"
2647 "`switch" "`endswitch"
2648 "`timescale"
2649 "`time_scale"
2650 ;; OVM Begin tokens
2651 "`ovm_component_utils_begin"
2652 "`ovm_component_param_utils_begin"
2653 "`ovm_field_utils_begin"
2654 "`ovm_object_utils_begin"
2655 "`ovm_object_param_utils_begin"
2656 "`ovm_sequence_utils_begin"
2657 "`ovm_sequencer_utils_begin"
2658 ;; OVM End tokens
2659 "`ovm_component_utils_end"
2660 "`ovm_field_utils_end"
2661 "`ovm_object_utils_end"
2662 "`ovm_sequence_utils_end"
2663 "`ovm_sequencer_utils_end"
2664 ;; UVM Begin tokens
2665 "`uvm_component_utils_begin"
2666 "`uvm_component_param_utils_begin"
2667 "`uvm_field_utils_begin"
2668 "`uvm_object_utils_begin"
2669 "`uvm_object_param_utils_begin"
2670 "`uvm_sequence_utils_begin"
2671 "`uvm_sequencer_utils_begin"
2672 ;; UVM End tokens
2673 "`uvm_component_utils_end" ;; Typo in spec, it's not uvm_component_end
2674 "`uvm_field_utils_end"
2675 "`uvm_object_utils_end"
2676 "`uvm_sequence_utils_end"
2677 "`uvm_sequencer_utils_end"
2678 ;; VMM Begin tokens
2679 "`vmm_data_member_begin"
2680 "`vmm_env_member_begin"
2681 "`vmm_scenario_member_begin"
2682 "`vmm_subenv_member_begin"
2683 "`vmm_xactor_member_begin"
2684 ;; VMM End tokens
2685 "`vmm_data_member_end"
2686 "`vmm_env_member_end"
2687 "`vmm_scenario_member_end"
2688 "`vmm_subenv_member_end"
2689 "`vmm_xactor_member_end"
2690 ))))
2691
2692 (defconst verilog-defun-level-not-generate-re
2693 (eval-when-compile
2694 (verilog-regexp-words
2695 `( "module" "macromodule" "primitive" "class" "program"
2696 "interface" "package" "config"))))
2697
2698 (defconst verilog-defun-level-re
2699 (eval-when-compile
2700 (verilog-regexp-words
2701 (append
2702 `( "module" "macromodule" "primitive" "class" "program"
2703 "interface" "package" "config")
2704 `( "initial" "final" "always" "always_comb" "always_ff"
2705 "always_latch" "endtask" "endfunction" )))))
2706
2707 (defconst verilog-defun-level-generate-only-re
2708 (eval-when-compile
2709 (verilog-regexp-words
2710 `( "initial" "final" "always" "always_comb" "always_ff"
2711 "always_latch" "endtask" "endfunction" ))))
2712
2713 (defconst verilog-cpp-level-re
2714 (eval-when-compile
2715 (verilog-regexp-words
2716 `(
2717 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2718 ))))
2719 (defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
2720 (defconst verilog-extended-case-re "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
2721 (defconst verilog-extended-complete-re
2722 (concat "\\(\\(\\<extern\\s-+\\|\\<\\(\\<pure\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)\\)"
2723 "\\|\\(\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)\\)"
2724 "\\|\\(\\(\\<import\\>\\s-+\\)?\\(\"DPI-C\"\\s-+\\)?\\(\\<pure\\>\\s-+\\)?\\(function\\>\\|task\\>\\)\\)"
2725 "\\|" verilog-extended-case-re ))
2726 (defconst verilog-basic-complete-re
2727 (eval-when-compile
2728 (verilog-regexp-words
2729 `(
2730 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2731 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
2732 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
2733 ))))
2734 (defconst verilog-complete-reg
2735 (concat
2736 verilog-extended-complete-re "\\|\\(" verilog-basic-complete-re "\\)"))
2737
2738 (defconst verilog-end-statement-re
2739 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2740 verilog-end-block-re "\\)"))
2741
2742 (defconst verilog-endcase-re
2743 (concat verilog-extended-case-re "\\|"
2744 "\\(endcase\\)\\|"
2745 verilog-defun-re
2746 ))
2747
2748 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2749 "String used to mark beginning of excluded text.")
2750 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2751 "String used to mark end of excluded text.")
2752 (defconst verilog-preprocessor-re
2753 (eval-when-compile
2754 (verilog-regexp-words
2755 `(
2756 "`define" "`include" "`ifdef" "`ifndef" "`if" "`endif" "`else"
2757 ))))
2758
2759 (defconst verilog-keywords
2760 '( "`case" "`default" "`define" "`else" "`endfor" "`endif"
2761 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
2762 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
2763 "`time_scale" "`undef" "`while"
2764
2765 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2766 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2767 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2768 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2769 "config" "const" "constraint" "context" "continue" "cover"
2770 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2771 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2772 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2773 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2774 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2775 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2776 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2777 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2778 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2779 "include" "initial" "inout" "input" "inside" "instance" "int"
2780 "integer" "interface" "intersect" "join" "join_any" "join_none"
2781 "large" "liblist" "library" "local" "localparam" "logic"
2782 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2783 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2784 "notif0" "notif1" "null" "or" "output" "package" "packed"
2785 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2786 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2787 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2788 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2789 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2790 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2791 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2792 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2793 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2794 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2795 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2796 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2797 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2798 "wire" "with" "within" "wor" "xnor" "xor"
2799 ;; 1800-2009
2800 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2801 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2802 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2803 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
2804 ;; 1800-2012
2805 "implements" "interconnect" "nettype" "soft"
2806 )
2807 "List of Verilog keywords.")
2808
2809 (defconst verilog-comment-start-regexp "//\\|/\\*"
2810 "Dual comment value for `comment-start-regexp'.")
2811
2812 (defvar verilog-mode-syntax-table
2813 (let ((table (make-syntax-table)))
2814 ;; Populate the syntax TABLE.
2815 (modify-syntax-entry ?\\ "\\" table)
2816 (modify-syntax-entry ?+ "." table)
2817 (modify-syntax-entry ?- "." table)
2818 (modify-syntax-entry ?= "." table)
2819 (modify-syntax-entry ?% "." table)
2820 (modify-syntax-entry ?< "." table)
2821 (modify-syntax-entry ?> "." table)
2822 (modify-syntax-entry ?& "." table)
2823 (modify-syntax-entry ?| "." table)
2824 ;; FIXME: This goes against Emacs conventions. Use "_" syntax instead and
2825 ;; then use regexps with things like "\\_<...\\_>".
2826 (modify-syntax-entry ?` "w" table) ;; ` is part of definition symbols in Verilog
2827 (modify-syntax-entry ?_ "w" table)
2828 (modify-syntax-entry ?\' "." table)
2829
2830 ;; Set up TABLE to handle block and line style comments.
2831 (if (featurep 'xemacs)
2832 (progn
2833 ;; XEmacs (formerly Lucid) has the best implementation
2834 (modify-syntax-entry ?/ ". 1456" table)
2835 (modify-syntax-entry ?* ". 23" table)
2836 (modify-syntax-entry ?\n "> b" table))
2837 ;; Emacs does things differently, but we can work with it
2838 (modify-syntax-entry ?/ ". 124b" table)
2839 (modify-syntax-entry ?* ". 23" table)
2840 (modify-syntax-entry ?\n "> b" table))
2841 table)
2842 "Syntax table used in Verilog mode buffers.")
2843
2844 (defvar verilog-font-lock-keywords nil
2845 "Default highlighting for Verilog mode.")
2846
2847 (defvar verilog-font-lock-keywords-1 nil
2848 "Subdued level highlighting for Verilog mode.")
2849
2850 (defvar verilog-font-lock-keywords-2 nil
2851 "Medium level highlighting for Verilog mode.
2852 See also `verilog-font-lock-extra-types'.")
2853
2854 (defvar verilog-font-lock-keywords-3 nil
2855 "Gaudy level highlighting for Verilog mode.
2856 See also `verilog-font-lock-extra-types'.")
2857
2858 (defvar verilog-font-lock-translate-off-face
2859 'verilog-font-lock-translate-off-face
2860 "Font to use for translated off regions.")
2861 (defface verilog-font-lock-translate-off-face
2862 '((((class color)
2863 (background light))
2864 (:background "gray90" :italic t ))
2865 (((class color)
2866 (background dark))
2867 (:background "gray10" :italic t ))
2868 (((class grayscale) (background light))
2869 (:foreground "DimGray" :italic t))
2870 (((class grayscale) (background dark))
2871 (:foreground "LightGray" :italic t))
2872 (t (:italis t)))
2873 "Font lock mode face used to background highlight translate-off regions."
2874 :group 'font-lock-highlighting-faces)
2875
2876 (defvar verilog-font-lock-p1800-face
2877 'verilog-font-lock-p1800-face
2878 "Font to use for p1800 keywords.")
2879 (defface verilog-font-lock-p1800-face
2880 '((((class color)
2881 (background light))
2882 (:foreground "DarkOrange3" :bold t ))
2883 (((class color)
2884 (background dark))
2885 (:foreground "orange1" :bold t ))
2886 (t (:italic t)))
2887 "Font lock mode face used to highlight P1800 keywords."
2888 :group 'font-lock-highlighting-faces)
2889
2890 (defvar verilog-font-lock-ams-face
2891 'verilog-font-lock-ams-face
2892 "Font to use for Analog/Mixed Signal keywords.")
2893 (defface verilog-font-lock-ams-face
2894 '((((class color)
2895 (background light))
2896 (:foreground "Purple" :bold t ))
2897 (((class color)
2898 (background dark))
2899 (:foreground "orange1" :bold t ))
2900 (t (:italic t)))
2901 "Font lock mode face used to highlight AMS keywords."
2902 :group 'font-lock-highlighting-faces)
2903
2904 (defvar verilog-font-grouping-keywords-face
2905 'verilog-font-lock-grouping-keywords-face
2906 "Font to use for Verilog Grouping Keywords (such as begin..end).")
2907 (defface verilog-font-lock-grouping-keywords-face
2908 '((((class color)
2909 (background light))
2910 (:foreground "red4" :bold t ))
2911 (((class color)
2912 (background dark))
2913 (:foreground "red4" :bold t ))
2914 (t (:italic t)))
2915 "Font lock mode face used to highlight verilog grouping keywords."
2916 :group 'font-lock-highlighting-faces)
2917
2918 (let* ((verilog-type-font-keywords
2919 (eval-when-compile
2920 (verilog-regexp-opt
2921 '(
2922 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
2923 "event" "genvar" "inout" "input" "integer" "localparam"
2924 "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0" "notif1" "or"
2925 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
2926 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
2927 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
2928 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
2929 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
2930 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
2931 ) nil )))
2932
2933 (verilog-pragma-keywords
2934 (eval-when-compile
2935 (verilog-regexp-opt
2936 '("surefire" "auto" "synopsys" "rtl_synthesis" "verilint" "leda" "0in"
2937 ) nil )))
2938
2939 (verilog-1800-2005-keywords
2940 (eval-when-compile
2941 (verilog-regexp-opt
2942 '("alias" "assert" "assume" "automatic" "before" "bind"
2943 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
2944 "clocking" "config" "const" "constraint" "context" "continue"
2945 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
2946 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
2947 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
2948 "expect" "export" "extends" "extern" "first_match" "foreach"
2949 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
2950 "illegal_bins" "import" "incdir" "include" "inside" "instance"
2951 "int" "intersect" "large" "liblist" "library" "local" "longint"
2952 "matches" "medium" "modport" "new" "noshowcancelled" "null"
2953 "packed" "program" "property" "protected" "pull0" "pull1"
2954 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2955 "randcase" "randsequence" "ref" "release" "return" "scalared"
2956 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
2957 "specparam" "static" "string" "strong0" "strong1" "struct"
2958 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
2959 "type" "union" "unsigned" "use" "var" "virtual" "void"
2960 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
2961 ) nil )))
2962
2963 (verilog-1800-2009-keywords
2964 (eval-when-compile
2965 (verilog-regexp-opt
2966 '("accept_on" "checker" "endchecker" "eventually" "global"
2967 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
2968 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
2969 "sync_accept_on" "sync_reject_on" "unique0" "until"
2970 "until_with" "untyped" "weak" ) nil )))
2971
2972 (verilog-1800-2012-keywords
2973 (eval-when-compile
2974 (verilog-regexp-opt
2975 '("implements" "interconnect" "nettype" "soft" ) nil )))
2976
2977 (verilog-ams-keywords
2978 (eval-when-compile
2979 (verilog-regexp-opt
2980 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
2981 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
2982 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
2983 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
2984 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
2985 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
2986 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
2987 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
2988 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
2989 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
2990 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
2991
2992 (verilog-font-keywords
2993 (eval-when-compile
2994 (verilog-regexp-opt
2995 '(
2996 "assign" "case" "casex" "casez" "randcase" "deassign"
2997 "default" "disable" "else" "endcase" "endfunction"
2998 "endgenerate" "endinterface" "endmodule" "endprimitive"
2999 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
3000 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
3001 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
3002 "package" "endpackage" "always" "always_comb" "always_ff"
3003 "always_latch" "posedge" "primitive" "priority" "release"
3004 "repeat" "specify" "table" "task" "unique" "wait" "while"
3005 "class" "program" "endclass" "endprogram"
3006 ) nil )))
3007
3008 (verilog-font-grouping-keywords
3009 (eval-when-compile
3010 (verilog-regexp-opt
3011 '( "begin" "end" ) nil ))))
3012
3013 (setq verilog-font-lock-keywords
3014 (list
3015 ;; Fontify all builtin keywords
3016 (concat "\\<\\(" verilog-font-keywords "\\|"
3017 ;; And user/system tasks and functions
3018 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
3019 "\\)\\>")
3020 ;; Fontify all types
3021 (if verilog-highlight-grouping-keywords
3022 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
3023 'verilog-font-lock-ams-face)
3024 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
3025 'font-lock-type-face))
3026 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
3027 'font-lock-type-face)
3028 ;; Fontify IEEE-1800-2005 keywords appropriately
3029 (if verilog-highlight-p1800-keywords
3030 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
3031 'verilog-font-lock-p1800-face)
3032 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
3033 'font-lock-type-face))
3034 ;; Fontify IEEE-1800-2009 keywords appropriately
3035 (if verilog-highlight-p1800-keywords
3036 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
3037 'verilog-font-lock-p1800-face)
3038 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
3039 'font-lock-type-face))
3040 ;; Fontify IEEE-1800-2012 keywords appropriately
3041 (if verilog-highlight-p1800-keywords
3042 (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
3043 'verilog-font-lock-p1800-face)
3044 (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
3045 'font-lock-type-face))
3046 ;; Fontify Verilog-AMS keywords
3047 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
3048 'verilog-font-lock-ams-face)))
3049
3050 (setq verilog-font-lock-keywords-1
3051 (append verilog-font-lock-keywords
3052 (list
3053 ;; Fontify module definitions
3054 (list
3055 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
3056 '(1 font-lock-keyword-face)
3057 '(3 font-lock-function-name-face 'prepend))
3058 ;; Fontify function definitions
3059 (list
3060 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
3061 '(1 font-lock-keyword-face)
3062 '(3 font-lock-constant-face prepend))
3063 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
3064 (1 font-lock-keyword-face)
3065 (2 font-lock-constant-face append))
3066 '("\\<function\\>\\s-+\\(\\sw+\\)"
3067 1 'font-lock-constant-face append))))
3068
3069 (setq verilog-font-lock-keywords-2
3070 (append verilog-font-lock-keywords-1
3071 (list
3072 ;; Fontify pragmas
3073 (concat "\\(//\\s-*\\(" verilog-pragma-keywords "\\)\\s-.*\\)")
3074 ;; Fontify escaped names
3075 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
3076 ;; Fontify macro definitions/ uses
3077 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
3078 'font-lock-preprocessor-face
3079 'font-lock-type-face))
3080 ;; Fontify delays/numbers
3081 '("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9_.\]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
3082 0 font-lock-type-face append)
3083 ;; Fontify instantiation names
3084 '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
3085 )))
3086
3087 (setq verilog-font-lock-keywords-3
3088 (append verilog-font-lock-keywords-2
3089 (when verilog-highlight-translate-off
3090 (list
3091 ;; Fontify things in translate off regions
3092 '(verilog-match-translate-off
3093 (0 'verilog-font-lock-translate-off-face prepend))
3094 )))))
3095
3096 ;;
3097 ;; Buffer state preservation
3098
3099 (defmacro verilog-save-buffer-state (&rest body)
3100 "Execute BODY forms, saving state around insignificant change.
3101 Changes in text properties like `face' or `syntax-table' are
3102 considered insignificant. This macro allows text properties to
3103 be changed, even in a read-only buffer.
3104
3105 A change is considered significant if it affects the buffer text
3106 in any way that isn't completely restored again. Any
3107 user-visible changes to the buffer must not be within a
3108 `verilog-save-buffer-state'."
3109 ;; From c-save-buffer-state
3110 `(let* ((modified (buffer-modified-p))
3111 (buffer-undo-list t)
3112 (inhibit-read-only t)
3113 (inhibit-point-motion-hooks t)
3114 (verilog-no-change-functions t)
3115 before-change-functions
3116 after-change-functions
3117 deactivate-mark
3118 buffer-file-name ; Prevent primitives checking
3119 buffer-file-truename) ; for file modification
3120 (unwind-protect
3121 (progn ,@body)
3122 (and (not modified)
3123 (buffer-modified-p)
3124 (set-buffer-modified-p nil)))))
3125
3126 (defmacro verilog-save-no-change-functions (&rest body)
3127 "Execute BODY forms, disabling all change hooks in BODY.
3128 For insignificant changes, see instead `verilog-save-buffer-state'."
3129 `(let* ((inhibit-point-motion-hooks t)
3130 (verilog-no-change-functions t)
3131 before-change-functions
3132 after-change-functions)
3133 (progn ,@body)))
3134
3135 (defvar verilog-save-font-mod-hooked nil
3136 "Local variable when inside a `verilog-save-font-mods' block.")
3137 (make-variable-buffer-local 'verilog-save-font-mod-hooked)
3138
3139 (defmacro verilog-save-font-mods (&rest body)
3140 "Execute BODY forms, disabling text modifications to allow performing BODY.
3141 Includes temporary disabling of `font-lock' to restore the buffer
3142 to full text form for parsing. Additional actions may be specified with
3143 `verilog-before-save-font-hook' and `verilog-after-save-font-hook'."
3144 ;; Before version 20, match-string with font-lock returns a
3145 ;; vector that is not equal to the string. IE if on "input"
3146 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
3147 `(let* ((hooked (unless verilog-save-font-mod-hooked
3148 (verilog-run-hooks 'verilog-before-save-font-hook)
3149 t))
3150 (verilog-save-font-mod-hooked t)
3151 (fontlocked (when (and (boundp 'font-lock-mode) font-lock-mode)
3152 (font-lock-mode 0)
3153 t)))
3154 (unwind-protect
3155 (progn ,@body)
3156 ;; Unwind forms
3157 (when fontlocked (font-lock-mode t))
3158 (when hooked (verilog-run-hooks 'verilog-after-save-font-hook)))))
3159
3160 ;;
3161 ;; Comment detection and caching
3162
3163 (defvar verilog-scan-cache-preserving nil
3164 "If true, the specified buffer's comment properties are static.
3165 Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
3166 and `verilog-scan'.")
3167
3168 (defvar verilog-scan-cache-tick nil
3169 "Modification tick at which `verilog-scan' was last completed.")
3170 (make-variable-buffer-local 'verilog-scan-cache-tick)
3171
3172 (defun verilog-scan-cache-flush ()
3173 "Flush the `verilog-scan' cache."
3174 (setq verilog-scan-cache-tick nil))
3175
3176 (defun verilog-scan-cache-ok-p ()
3177 "Return t if the scan cache is up to date."
3178 (or (and verilog-scan-cache-preserving
3179 (eq verilog-scan-cache-preserving (current-buffer))
3180 verilog-scan-cache-tick)
3181 (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
3182
3183 (defmacro verilog-save-scan-cache (&rest body)
3184 "Execute the BODY forms, allowing scan cache preservation within BODY.
3185 This requires that insertions must use `verilog-insert'."
3186 ;; If the buffer is out of date, trash it, as we'll not check later the tick
3187 ;; Note this must work properly if there's multiple layers of calls
3188 ;; to verilog-save-scan-cache even with differing ticks.
3189 `(progn
3190 (unless (verilog-scan-cache-ok-p) ;; Must be before let
3191 (setq verilog-scan-cache-tick nil))
3192 (let* ((verilog-scan-cache-preserving (current-buffer)))
3193 (progn ,@body))))
3194
3195 (defun verilog-scan-region (beg end)
3196 "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
3197 This creates v-cmts properties where comments are in force."
3198 ;; Why properties and not overlays? Overlays have much slower non O(1)
3199 ;; lookup times.
3200 ;; This function is warm - called on every verilog-insert
3201 (save-excursion
3202 (save-match-data
3203 (verilog-save-buffer-state
3204 (let (pt)
3205 (goto-char beg)
3206 (while (< (point) end)
3207 (cond ((looking-at "//")
3208 (setq pt (point))
3209 (or (search-forward "\n" end t)
3210 (goto-char end))
3211 ;; "1+": The leading // or /* itself isn't considered as
3212 ;; being "inside" the comment, so that a (search-backward)
3213 ;; that lands at the start of the // won't mis-indicate
3214 ;; it's inside a comment. Also otherwise it would be
3215 ;; hard to find a commented out /*AS*/ vs one that isn't
3216 (put-text-property (1+ pt) (point) 'v-cmts t))
3217 ((looking-at "/\\*")
3218 (setq pt (point))
3219 (or (search-forward "*/" end t)
3220 ;; No error - let later code indicate it so we can
3221 ;; use inside functions on-the-fly
3222 ;;(error "%s: Unmatched /* */, at char %d"
3223 ;; (verilog-point-text) (point))
3224 (goto-char end))
3225 (put-text-property (1+ pt) (point) 'v-cmts t))
3226 ((looking-at "\"")
3227 (setq pt (point))
3228 (or (re-search-forward "[^\\]\"" end t) ;; don't forward-char first, since we look for a non backslash first
3229 ;; No error - let later code indicate it so we can
3230 (goto-char end))
3231 (put-text-property (1+ pt) (point) 'v-cmts t))
3232 (t
3233 (forward-char 1)
3234 (if (re-search-forward "[/\"]" end t)
3235 (backward-char 1)
3236 (goto-char end))))))))))
3237
3238 (defun verilog-scan ()
3239 "Parse the buffer, marking all comments with properties.
3240 Also assumes any text inserted since `verilog-scan-cache-tick'
3241 either is ok to parse as a non-comment, or `verilog-insert' was used."
3242 ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
3243 (unless (verilog-scan-cache-ok-p)
3244 (save-excursion
3245 (verilog-save-buffer-state
3246 (when verilog-debug
3247 (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
3248 verilog-scan-cache-preserving verilog-scan-cache-tick
3249 (buffer-chars-modified-tick)))
3250 (remove-text-properties (point-min) (point-max) '(v-cmts nil))
3251 (verilog-scan-region (point-min) (point-max))
3252 (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
3253 (when verilog-debug (message "Scanning... done"))))))
3254
3255 (defun verilog-scan-debug ()
3256 "For debugging, show with display face results of `verilog-scan'."
3257 (font-lock-mode 0)
3258 ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
3259 (save-excursion
3260 (goto-char (point-min))
3261 (remove-text-properties (point-min) (point-max) '(face nil))
3262 (while (not (eobp))
3263 (cond ((get-text-property (point) 'v-cmts)
3264 (put-text-property (point) (1+ (point)) `face 'underline)
3265 ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
3266 (forward-char 1))
3267 (t
3268 (goto-char (or (next-property-change (point)) (point-max))))))))
3269
3270 (defun verilog-scan-and-debug ()
3271 "For debugging, run `verilog-scan' and `verilog-scan-debug'."
3272 (let (verilog-scan-cache-preserving
3273 verilog-scan-cache-tick)
3274 (goto-char (point-min))
3275 (verilog-scan)
3276 (verilog-scan-debug)))
3277
3278 (defun verilog-inside-comment-or-string-p (&optional pos)
3279 "Check if optional point POS is inside a comment.
3280 This may require a slow pre-parse of the buffer with `verilog-scan'
3281 to establish comment properties on all text."
3282 ;; This function is very hot
3283 (verilog-scan)
3284 (if pos
3285 (and (>= pos (point-min))
3286 (get-text-property pos 'v-cmts))
3287 (get-text-property (point) 'v-cmts)))
3288
3289 (defun verilog-insert (&rest stuff)
3290 "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
3291 Any insert that includes a comment must have the entire comment
3292 inserted using a single call to `verilog-insert'."
3293 (let ((pt (point)))
3294 (while stuff
3295 (insert (car stuff))
3296 (setq stuff (cdr stuff)))
3297 (verilog-scan-region pt (point))))
3298
3299 ;; More searching
3300
3301 (defun verilog-declaration-end ()
3302 (search-forward ";"))
3303
3304 (defun verilog-point-text (&optional pointnum)
3305 "Return text describing where POINTNUM or current point is (for errors).
3306 Use filename, if current buffer being edited shorten to just buffer name."
3307 (concat (or (and (equal (window-buffer) (current-buffer))
3308 (buffer-name))
3309 buffer-file-name
3310 (buffer-name))
3311 ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
3312
3313 (defun electric-verilog-backward-sexp ()
3314 "Move backward over one balanced expression."
3315 (interactive)
3316 ;; before that see if we are in a comment
3317 (verilog-backward-sexp))
3318
3319 (defun electric-verilog-forward-sexp ()
3320 "Move forward over one balanced expression."
3321 (interactive)
3322 ;; before that see if we are in a comment
3323 (verilog-forward-sexp))
3324
3325 ;;;used by hs-minor-mode
3326 (defun verilog-forward-sexp-function (arg)
3327 (if (< arg 0)
3328 (verilog-backward-sexp)
3329 (verilog-forward-sexp)))
3330
3331
3332 (defun verilog-backward-sexp ()
3333 (let ((reg)
3334 (elsec 1)
3335 (found nil)
3336 (st (point)))
3337 (if (not (looking-at "\\<"))
3338 (forward-word -1))
3339 (cond
3340 ((verilog-skip-backward-comment-or-string))
3341 ((looking-at "\\<else\\>")
3342 (setq reg (concat
3343 verilog-end-block-re
3344 "\\|\\(\\<else\\>\\)"
3345 "\\|\\(\\<if\\>\\)"))
3346 (while (and (not found)
3347 (verilog-re-search-backward reg nil 'move))
3348 (cond
3349 ((match-end 1) ; matched verilog-end-block-re
3350 ;; try to leap back to matching outward block by striding across
3351 ;; indent level changing tokens then immediately
3352 ;; previous line governs indentation.
3353 (verilog-leap-to-head))
3354 ((match-end 2) ; else, we're in deep
3355 (setq elsec (1+ elsec)))
3356 ((match-end 3) ; found it
3357 (setq elsec (1- elsec))
3358 (if (= 0 elsec)
3359 ;; Now previous line describes syntax
3360 (setq found 't))))))
3361 ((looking-at verilog-end-block-re)
3362 (verilog-leap-to-head))
3363 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
3364 (cond
3365 ((match-end 1)
3366 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
3367 ((match-end 2)
3368 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
3369 ((match-end 3)
3370 (verilog-re-search-backward "\\<class\\>" nil 'move))
3371 ((match-end 4)
3372 (verilog-re-search-backward "\\<program\\>" nil 'move))
3373 ((match-end 5)
3374 (verilog-re-search-backward "\\<interface\\>" nil 'move))
3375 ((match-end 6)
3376 (verilog-re-search-backward "\\<package\\>" nil 'move))
3377 (t
3378 (goto-char st)
3379 (backward-sexp 1))))
3380 (t
3381 (goto-char st)
3382 (backward-sexp)))))
3383
3384 (defun verilog-forward-sexp ()
3385 (let ((reg)
3386 (md 2)
3387 (st (point))
3388 (nest 'yes))
3389 (if (not (looking-at "\\<"))
3390 (forward-word -1))
3391 (cond
3392 ((verilog-skip-forward-comment-or-string)
3393 (verilog-forward-syntactic-ws))
3394 ((looking-at verilog-beg-block-re-ordered)
3395 (cond
3396 ((match-end 1);
3397 ;; Search forward for matching end
3398 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
3399 ((match-end 2)
3400 ;; Search forward for matching endcase
3401 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique0?\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
3402 (setq md 3) ;; ender is third item in regexp
3403 )
3404 ((match-end 4)
3405 ;; might be "disable fork" or "wait fork"
3406 (let
3407 (here)
3408 (if (or
3409 (looking-at verilog-disable-fork-re)
3410 (and (looking-at "fork")
3411 (progn
3412 (setq here (point)) ;; sometimes a fork is just a fork
3413 (forward-word -1)
3414 (looking-at verilog-disable-fork-re))))
3415 (progn ;; it is a disable fork; ignore it
3416 (goto-char (match-end 0))
3417 (forward-word 1)
3418 (setq reg nil))
3419 (progn ;; it is a nice simple fork
3420 (goto-char here) ;; return from looking for "disable fork"
3421 ;; Search forward for matching join
3422 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
3423 ((match-end 6)
3424 ;; Search forward for matching endclass
3425 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
3426
3427 ((match-end 7)
3428 ;; Search forward for matching endtable
3429 (setq reg "\\<endtable\\>" )
3430 (setq nest 'no))
3431 ((match-end 8)
3432 ;; Search forward for matching endspecify
3433 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
3434 ((match-end 9)
3435 ;; Search forward for matching endfunction
3436 (setq reg "\\<endfunction\\>" )
3437 (setq nest 'no))
3438 ((match-end 10)
3439 ;; Search forward for matching endfunction
3440 (setq reg "\\<endfunction\\>" )
3441 (setq nest 'no))
3442 ((match-end 14)
3443 ;; Search forward for matching endtask
3444 (setq reg "\\<endtask\\>" )
3445 (setq nest 'no))
3446 ((match-end 15)
3447 ;; Search forward for matching endtask
3448 (setq reg "\\<endtask\\>" )
3449 (setq nest 'no))
3450 ((match-end 19)
3451 ;; Search forward for matching endgenerate
3452 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
3453 ((match-end 20)
3454 ;; Search forward for matching endgroup
3455 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
3456 ((match-end 21)
3457 ;; Search forward for matching endproperty
3458 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
3459 ((match-end 25)
3460 ;; Search forward for matching endsequence
3461 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
3462 (setq md 3)) ; 3 to get to endsequence in the reg above
3463 ((match-end 27)
3464 ;; Search forward for matching endclocking
3465 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
3466 (if (and reg
3467 (forward-word 1))
3468 (catch 'skip
3469 (if (eq nest 'yes)
3470 (let ((depth 1)
3471 here)
3472 (while (verilog-re-search-forward reg nil 'move)
3473 (cond
3474 ((match-end md) ; a closer in regular expression, so we are climbing out
3475 (setq depth (1- depth))
3476 (if (= 0 depth) ; we are out!
3477 (throw 'skip 1)))
3478 ((match-end 1) ; an opener in the r-e, so we are in deeper now
3479 (setq here (point)) ; remember where we started
3480 (goto-char (match-beginning 1))
3481 (cond
3482 ((if (or
3483 (looking-at verilog-disable-fork-re)
3484 (and (looking-at "fork")
3485 (progn
3486 (forward-word -1)
3487 (looking-at verilog-disable-fork-re))))
3488 (progn ;; it is a disable fork; another false alarm
3489 (goto-char (match-end 0)))
3490 (progn ;; it is a simple fork (or has nothing to do with fork)
3491 (goto-char here)
3492 (setq depth (1+ depth))))))))))
3493 (if (verilog-re-search-forward reg nil 'move)
3494 (throw 'skip 1))))))
3495
3496 ((looking-at (concat
3497 "\\(\\<\\(macro\\)?module\\>\\)\\|"
3498 "\\(\\<primitive\\>\\)\\|"
3499 "\\(\\<class\\>\\)\\|"
3500 "\\(\\<program\\>\\)\\|"
3501 "\\(\\<interface\\>\\)\\|"
3502 "\\(\\<package\\>\\)"))
3503 (cond
3504 ((match-end 1)
3505 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
3506 ((match-end 2)
3507 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
3508 ((match-end 3)
3509 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
3510 ((match-end 4)
3511 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
3512 ((match-end 5)
3513 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
3514 ((match-end 6)
3515 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
3516 (t
3517 (goto-char st)
3518 (if (= (following-char) ?\) )
3519 (forward-char 1)
3520 (forward-sexp 1)))))
3521 (t
3522 (goto-char st)
3523 (if (= (following-char) ?\) )
3524 (forward-char 1)
3525 (forward-sexp 1))))))
3526
3527 (defun verilog-declaration-beg ()
3528 (verilog-re-search-backward verilog-declaration-re (bobp) t))
3529
3530 ;;
3531 ;;
3532 ;; Mode
3533 ;;
3534 (defvar verilog-which-tool 1)
3535 ;;;###autoload
3536 (define-derived-mode verilog-mode prog-mode "Verilog"
3537 "Major mode for editing Verilog code.
3538 \\<verilog-mode-map>
3539 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
3540 AUTOs can improve coding efficiency.
3541
3542 Use \\[verilog-faq] for a pointer to frequently asked questions.
3543
3544 NEWLINE, TAB indents for Verilog code.
3545 Delete converts tabs to spaces as it moves back.
3546
3547 Supports highlighting.
3548
3549 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
3550 with no args, if that value is non-nil.
3551
3552 Variables controlling indentation/edit style:
3553
3554 variable `verilog-indent-level' (default 3)
3555 Indentation of Verilog statements with respect to containing block.
3556 `verilog-indent-level-module' (default 3)
3557 Absolute indentation of Module level Verilog statements.
3558 Set to 0 to get initial and always statements lined up
3559 on the left side of your screen.
3560 `verilog-indent-level-declaration' (default 3)
3561 Indentation of declarations with respect to containing block.
3562 Set to 0 to get them list right under containing block.
3563 `verilog-indent-level-behavioral' (default 3)
3564 Indentation of first begin in a task or function block
3565 Set to 0 to get such code to lined up underneath the task or
3566 function keyword.
3567 `verilog-indent-level-directive' (default 1)
3568 Indentation of `ifdef/`endif blocks.
3569 `verilog-cexp-indent' (default 1)
3570 Indentation of Verilog statements broken across lines i.e.:
3571 if (a)
3572 begin
3573 `verilog-case-indent' (default 2)
3574 Indentation for case statements.
3575 `verilog-auto-newline' (default nil)
3576 Non-nil means automatically newline after semicolons and the punctuation
3577 mark after an end.
3578 `verilog-auto-indent-on-newline' (default t)
3579 Non-nil means automatically indent line after newline.
3580 `verilog-tab-always-indent' (default t)
3581 Non-nil means TAB in Verilog mode should always reindent the current line,
3582 regardless of where in the line point is when the TAB command is used.
3583 `verilog-indent-begin-after-if' (default t)
3584 Non-nil means to indent begin statements following a preceding
3585 if, else, while, for and repeat statements, if any. Otherwise,
3586 the begin is lined up with the preceding token. If t, you get:
3587 if (a)
3588 begin // amount of indent based on `verilog-cexp-indent'
3589 otherwise you get:
3590 if (a)
3591 begin
3592 `verilog-auto-endcomments' (default t)
3593 Non-nil means a comment /* ... */ is set after the ends which ends
3594 cases, tasks, functions and modules.
3595 The type and name of the object will be set between the braces.
3596 `verilog-minimum-comment-distance' (default 10)
3597 Minimum distance (in lines) between begin and end required before a comment
3598 will be inserted. Setting this variable to zero results in every
3599 end acquiring a comment; the default avoids too many redundant
3600 comments in tight quarters.
3601 `verilog-auto-lineup' (default 'declarations)
3602 List of contexts where auto lineup of code should be done.
3603
3604 Variables controlling other actions:
3605
3606 `verilog-linter' (default surelint)
3607 Unix program to call to run the lint checker. This is the default
3608 command for \\[compile-command] and \\[verilog-auto-save-compile].
3609
3610 See \\[customize] for the complete list of variables.
3611
3612 AUTO expansion functions are, in part:
3613
3614 \\[verilog-auto] Expand AUTO statements.
3615 \\[verilog-delete-auto] Remove the AUTOs.
3616 \\[verilog-inject-auto] Insert AUTOs for the first time.
3617
3618 Some other functions are:
3619
3620 \\[verilog-complete-word] Complete word with appropriate possibilities.
3621 \\[verilog-mark-defun] Mark function.
3622 \\[verilog-beg-of-defun] Move to beginning of current function.
3623 \\[verilog-end-of-defun] Move to end of current function.
3624 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
3625
3626 \\[verilog-comment-region] Put marked area in a comment.
3627 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
3628 \\[verilog-insert-block] Insert begin ... end.
3629 \\[verilog-star-comment] Insert /* ... */.
3630
3631 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
3632 \\[verilog-sk-begin] Insert a begin .. end block.
3633 \\[verilog-sk-case] Insert a case block, prompting for details.
3634 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
3635 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
3636 \\[verilog-sk-header] Insert a header block at the top of file.
3637 \\[verilog-sk-initial] Insert an initial begin .. end block.
3638 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
3639 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
3640 \\[verilog-sk-ovm-class] Insert an OVM Class block.
3641 \\[verilog-sk-uvm-object] Insert an UVM Object block.
3642 \\[verilog-sk-uvm-component] Insert an UVM Component block.
3643 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
3644 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
3645 \\[verilog-sk-specify] Insert a specify .. endspecify block.
3646 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
3647 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
3648 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
3649 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
3650 \\[verilog-sk-if] Insert an if (..) begin .. end block.
3651 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
3652 \\[verilog-sk-comment] Insert a comment block.
3653 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
3654 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
3655 \\[verilog-sk-input] Insert an input declaration, prompting for details.
3656 \\[verilog-sk-output] Insert an output declaration, prompting for details.
3657 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
3658 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
3659 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
3660 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
3661 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
3662
3663 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
3664 Key bindings specific to `verilog-mode-map' are:
3665
3666 \\{verilog-mode-map}"
3667 :abbrev-table verilog-mode-abbrev-table
3668 (set (make-local-variable 'beginning-of-defun-function)
3669 'verilog-beg-of-defun)
3670 (set (make-local-variable 'end-of-defun-function)
3671 'verilog-end-of-defun)
3672 (set-syntax-table verilog-mode-syntax-table)
3673 (set (make-local-variable 'indent-line-function)
3674 #'verilog-indent-line-relative)
3675 (set (make-local-variable 'comment-indent-function) 'verilog-comment-indent)
3676 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
3677 (set (make-local-variable 'comment-start) "// ")
3678 (set (make-local-variable 'comment-end) "")
3679 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
3680 (set (make-local-variable 'comment-multi-line) nil)
3681 ;; Set up for compilation
3682 (setq verilog-which-tool 1)
3683 (setq verilog-tool 'verilog-linter)
3684 (verilog-set-compile-command)
3685 (when (boundp 'hack-local-variables-hook) ;; Also modify any file-local-variables
3686 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
3687
3688 ;; Setting up menus
3689 (when (featurep 'xemacs)
3690 (easy-menu-add verilog-stmt-menu)
3691 (easy-menu-add verilog-menu)
3692 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
3693
3694 ;; Stuff for GNU Emacs
3695 (set (make-local-variable 'font-lock-defaults)
3696 `((verilog-font-lock-keywords
3697 verilog-font-lock-keywords-1
3698 verilog-font-lock-keywords-2
3699 verilog-font-lock-keywords-3)
3700 nil nil nil
3701 ,(if (functionp 'syntax-ppss)
3702 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
3703 ;; font-lock-beginning-of-syntax-function, so
3704 ;; font-lock-beginning-of-syntax-function, can't use
3705 ;; verilog-beg-of-defun.
3706 nil
3707 'verilog-beg-of-defun)))
3708 ;;------------------------------------------------------------
3709 ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
3710 ;; all buffer local:
3711 (unless noninteractive ;; Else can't see the result, and change hooks are slow
3712 (when (featurep 'xemacs)
3713 (make-local-hook 'font-lock-mode-hook)
3714 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
3715 (make-local-hook 'after-change-functions))
3716 (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
3717 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
3718 (add-hook 'after-change-functions 'verilog-highlight-region t t))
3719
3720 ;; Tell imenu how to handle Verilog.
3721 (set (make-local-variable 'imenu-generic-expression)
3722 verilog-imenu-generic-expression)
3723 ;; Tell which-func-modes that imenu knows about verilog
3724 (when (and (boundp 'which-func-modes) (listp which-func-modes))
3725 (add-to-list 'which-func-modes 'verilog-mode))
3726 ;; hideshow support
3727 (when (boundp 'hs-special-modes-alist)
3728 (unless (assq 'verilog-mode hs-special-modes-alist)
3729 (setq hs-special-modes-alist
3730 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
3731 verilog-forward-sexp-function)
3732 hs-special-modes-alist))))
3733
3734 ;; Stuff for autos
3735 (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
3736 ;; verilog-mode-hook call added by define-derived-mode
3737 )
3738 \f
3739
3740 ;;
3741 ;; Electric functions
3742 ;;
3743 (defun electric-verilog-terminate-line (&optional arg)
3744 "Terminate line and indent next line.
3745 With optional ARG, remove existing end of line comments."
3746 (interactive)
3747 ;; before that see if we are in a comment
3748 (let ((state (save-excursion (verilog-syntax-ppss))))
3749 (cond
3750 ((nth 7 state) ; Inside // comment
3751 (if (eolp)
3752 (progn
3753 (delete-horizontal-space)
3754 (newline))
3755 (progn
3756 (newline)
3757 (insert "// ")
3758 (beginning-of-line)))
3759 (verilog-indent-line))
3760 ((nth 4 state) ; Inside any comment (hence /**/)
3761 (newline)
3762 (verilog-more-comment))
3763 ((eolp)
3764 ;; First, check if current line should be indented
3765 (if (save-excursion
3766 (delete-horizontal-space)
3767 (beginning-of-line)
3768 (skip-chars-forward " \t")
3769 (if (looking-at verilog-auto-end-comment-lines-re)
3770 (let ((indent-str (verilog-indent-line)))
3771 ;; Maybe we should set some endcomments
3772 (if verilog-auto-endcomments
3773 (verilog-set-auto-endcomments indent-str arg))
3774 (end-of-line)
3775 (delete-horizontal-space)
3776 (if arg
3777 ()
3778 (newline))
3779 nil)
3780 (progn
3781 (end-of-line)
3782 (delete-horizontal-space)
3783 't)))
3784 ;; see if we should line up assignments
3785 (progn
3786 (if (or (eq 'all verilog-auto-lineup)
3787 (eq 'assignments verilog-auto-lineup))
3788 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
3789 (newline))
3790 (forward-line 1))
3791 ;; Indent next line
3792 (if verilog-auto-indent-on-newline
3793 (verilog-indent-line)))
3794 (t
3795 (newline)))))
3796
3797 (defun electric-verilog-terminate-and-indent ()
3798 "Insert a newline and indent for the next statement."
3799 (interactive)
3800 (electric-verilog-terminate-line 1))
3801
3802 (defun electric-verilog-semi ()
3803 "Insert `;' character and reindent the line."
3804 (interactive)
3805 (verilog-insert-last-command-event)
3806
3807 (if (or (verilog-in-comment-or-string-p)
3808 (verilog-in-escaped-name-p))
3809 ()
3810 (save-excursion
3811 (beginning-of-line)
3812 (verilog-forward-ws&directives)
3813 (verilog-indent-line))
3814 (if (and verilog-auto-newline
3815 (not (verilog-parenthesis-depth)))
3816 (electric-verilog-terminate-line))))
3817
3818 (defun electric-verilog-semi-with-comment ()
3819 "Insert `;' character, reindent the line and indent for comment."
3820 (interactive)
3821 (insert "\;")
3822 (save-excursion
3823 (beginning-of-line)
3824 (verilog-indent-line))
3825 (indent-for-comment))
3826
3827 (defun electric-verilog-colon ()
3828 "Insert `:' and do all indentations except line indent on this line."
3829 (interactive)
3830 (verilog-insert-last-command-event)
3831 ;; Do nothing if within string.
3832 (if (or
3833 (verilog-within-string)
3834 (not (verilog-in-case-region-p)))
3835 ()
3836 (save-excursion
3837 (let ((p (point))
3838 (lim (progn (verilog-beg-of-statement) (point))))
3839 (goto-char p)
3840 (verilog-backward-case-item lim)
3841 (verilog-indent-line)))
3842 ;; (let ((verilog-tab-always-indent nil))
3843 ;; (verilog-indent-line))
3844 ))
3845
3846 ;;(defun electric-verilog-equal ()
3847 ;; "Insert `=', and do indentation if within block."
3848 ;; (interactive)
3849 ;; (verilog-insert-last-command-event)
3850 ;; Could auto line up expressions, but not yet
3851 ;; (if (eq (car (verilog-calculate-indent)) 'block)
3852 ;; (let ((verilog-tab-always-indent nil))
3853 ;; (verilog-indent-command)))
3854 ;; )
3855
3856 (defun electric-verilog-tick ()
3857 "Insert back-tick, and indent to column 0 if this is a CPP directive."
3858 (interactive)
3859 (verilog-insert-last-command-event)
3860 (save-excursion
3861 (if (verilog-in-directive-p)
3862 (verilog-indent-line))))
3863
3864 (defun electric-verilog-tab ()
3865 "Function called when TAB is pressed in Verilog mode."
3866 (interactive)
3867 ;; If verilog-tab-always-indent, indent the beginning of the line.
3868 (cond
3869 ;; The region is active, indent it.
3870 ((and (region-active-p)
3871 (not (eq (region-beginning) (region-end))))
3872 (indent-region (region-beginning) (region-end) nil))
3873 ((or verilog-tab-always-indent
3874 (save-excursion
3875 (skip-chars-backward " \t")
3876 (bolp)))
3877 (let* ((oldpnt (point))
3878 (boi-point
3879 (save-excursion
3880 (beginning-of-line)
3881 (skip-chars-forward " \t")
3882 (verilog-indent-line)
3883 (back-to-indentation)
3884 (point))))
3885 (if (< (point) boi-point)
3886 (back-to-indentation)
3887 (cond ((not verilog-tab-to-comment))
3888 ((not (eolp))
3889 (end-of-line))
3890 (t
3891 (indent-for-comment)
3892 (when (and (eolp) (= oldpnt (point)))
3893 ; kill existing comment
3894 (beginning-of-line)
3895 (re-search-forward comment-start-skip oldpnt 'move)
3896 (goto-char (match-beginning 0))
3897 (skip-chars-backward " \t")
3898 (kill-region (point) oldpnt)))))))
3899 (t (progn (insert "\t")))))
3900
3901 \f
3902
3903 ;;
3904 ;; Interactive functions
3905 ;;
3906
3907 (defun verilog-indent-buffer ()
3908 "Indent-region the entire buffer as Verilog code.
3909 To call this from the command line, see \\[verilog-batch-indent]."
3910 (interactive)
3911 (verilog-mode)
3912 (indent-region (point-min) (point-max) nil))
3913
3914 (defun verilog-insert-block ()
3915 "Insert Verilog begin ... end; block in the code with right indentation."
3916 (interactive)
3917 (verilog-indent-line)
3918 (insert "begin")
3919 (electric-verilog-terminate-line)
3920 (save-excursion
3921 (electric-verilog-terminate-line)
3922 (insert "end")
3923 (beginning-of-line)
3924 (verilog-indent-line)))
3925
3926 (defun verilog-star-comment ()
3927 "Insert Verilog star comment at point."
3928 (interactive)
3929 (verilog-indent-line)
3930 (insert "/*")
3931 (save-excursion
3932 (newline)
3933 (insert " */"))
3934 (newline)
3935 (insert " * "))
3936
3937 (defun verilog-insert-1 (fmt max)
3938 "Use format string FMT to insert integers 0 to MAX - 1.
3939 Inserts one integer per line, at the current column. Stops early
3940 if it reaches the end of the buffer."
3941 (let ((col (current-column))
3942 (n 0))
3943 (save-excursion
3944 (while (< n max)
3945 (insert (format fmt n))
3946 (forward-line 1)
3947 ;; Note that this function does not bother to check for lines
3948 ;; shorter than col.
3949 (if (eobp)
3950 (setq n max)
3951 (setq n (1+ n))
3952 (move-to-column col))))))
3953
3954 (defun verilog-insert-indices (max)
3955 "Insert a set of indices into a rectangle.
3956 The upper left corner is defined by point. Indices begin with 0
3957 and extend to the MAX - 1. If no prefix arg is given, the user
3958 is prompted for a value. The indices are surrounded by square
3959 brackets \[]. For example, the following code with the point
3960 located after the first 'a' gives:
3961
3962 a = b a[ 0] = b
3963 a = b a[ 1] = b
3964 a = b a[ 2] = b
3965 a = b a[ 3] = b
3966 a = b ==> insert-indices ==> a[ 4] = b
3967 a = b a[ 5] = b
3968 a = b a[ 6] = b
3969 a = b a[ 7] = b
3970 a = b a[ 8] = b"
3971
3972 (interactive "NMAX: ")
3973 (verilog-insert-1 "[%3d]" max))
3974
3975 (defun verilog-generate-numbers (max)
3976 "Insert a set of generated numbers into a rectangle.
3977 The upper left corner is defined by point. The numbers are padded to three
3978 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
3979 is supplied, then the user is prompted for the MAX number. Consider the
3980 following code fragment:
3981
3982 buf buf buf buf000
3983 buf buf buf buf001
3984 buf buf buf buf002
3985 buf buf buf buf003
3986 buf buf ==> generate-numbers ==> buf buf004
3987 buf buf buf buf005
3988 buf buf buf buf006
3989 buf buf buf buf007
3990 buf buf buf buf008"
3991
3992 (interactive "NMAX: ")
3993 (verilog-insert-1 "%3.3d" max))
3994
3995 (defun verilog-mark-defun ()
3996 "Mark the current Verilog function (or procedure).
3997 This puts the mark at the end, and point at the beginning."
3998 (interactive)
3999 (if (featurep 'xemacs)
4000 (progn
4001 (push-mark (point))
4002 (verilog-end-of-defun)
4003 (push-mark (point))
4004 (verilog-beg-of-defun)
4005 (if (fboundp 'zmacs-activate-region)
4006 (zmacs-activate-region)))
4007 (mark-defun)))
4008
4009 (defun verilog-comment-region (start end)
4010 ;; checkdoc-params: (start end)
4011 "Put the region into a Verilog comment.
4012 The comments that are in this area are \"deformed\":
4013 `*)' becomes `!(*' and `}' becomes `!{'.
4014 These deformed comments are returned to normal if you use
4015 \\[verilog-uncomment-region] to undo the commenting.
4016
4017 The commented area starts with `verilog-exclude-str-start', and ends with
4018 `verilog-exclude-str-end'. But if you change these variables,
4019 \\[verilog-uncomment-region] won't recognize the comments."
4020 (interactive "r")
4021 (save-excursion
4022 ;; Insert start and endcomments
4023 (goto-char end)
4024 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
4025 (not (save-excursion (skip-chars-backward " \t") (bolp))))
4026 (forward-line 1)
4027 (beginning-of-line))
4028 (insert verilog-exclude-str-end)
4029 (setq end (point))
4030 (newline)
4031 (goto-char start)
4032 (beginning-of-line)
4033 (insert verilog-exclude-str-start)
4034 (newline)
4035 ;; Replace end-comments within commented area
4036 (goto-char end)
4037 (save-excursion
4038 (while (re-search-backward "\\*/" start t)
4039 (replace-match "*-/" t t)))
4040 (save-excursion
4041 (let ((s+1 (1+ start)))
4042 (while (re-search-backward "/\\*" s+1 t)
4043 (replace-match "/-*" t t))))))
4044
4045 (defun verilog-uncomment-region ()
4046 "Uncomment a commented area; change deformed comments back to normal.
4047 This command does nothing if the pointer is not in a commented
4048 area. See also `verilog-comment-region'."
4049 (interactive)
4050 (save-excursion
4051 (let ((start (point))
4052 (end (point)))
4053 ;; Find the boundaries of the comment
4054 (save-excursion
4055 (setq start (progn (search-backward verilog-exclude-str-start nil t)
4056 (point)))
4057 (setq end (progn (search-forward verilog-exclude-str-end nil t)
4058 (point))))
4059 ;; Check if we're really inside a comment
4060 (if (or (equal start (point)) (<= end (point)))
4061 (message "Not standing within commented area.")
4062 (progn
4063 ;; Remove endcomment
4064 (goto-char end)
4065 (beginning-of-line)
4066 (let ((pos (point)))
4067 (end-of-line)
4068 (delete-region pos (1+ (point))))
4069 ;; Change comments back to normal
4070 (save-excursion
4071 (while (re-search-backward "\\*-/" start t)
4072 (replace-match "*/" t t)))
4073 (save-excursion
4074 (while (re-search-backward "/-\\*" start t)
4075 (replace-match "/*" t t)))
4076 ;; Remove start comment
4077 (goto-char start)
4078 (beginning-of-line)
4079 (let ((pos (point)))
4080 (end-of-line)
4081 (delete-region pos (1+ (point)))))))))
4082
4083 (defun verilog-beg-of-defun ()
4084 "Move backward to the beginning of the current function or procedure."
4085 (interactive)
4086 (verilog-re-search-backward verilog-defun-re nil 'move))
4087
4088 (defun verilog-beg-of-defun-quick ()
4089 "Move backward to the beginning of the current function or procedure.
4090 Uses `verilog-scan' cache."
4091 (interactive)
4092 (verilog-re-search-backward-quick verilog-defun-re nil 'move))
4093
4094 (defun verilog-end-of-defun ()
4095 "Move forward to the end of the current function or procedure."
4096 (interactive)
4097 (verilog-re-search-forward verilog-end-defun-re nil 'move))
4098
4099 (defun verilog-get-end-of-defun ()
4100 (save-excursion
4101 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
4102 (point))
4103 (t
4104 (error "%s: Can't find endmodule" (verilog-point-text))
4105 (point-max)))))
4106
4107 (defun verilog-label-be ()
4108 "Label matching begin ... end, fork ... join and case ... endcase statements."
4109 (interactive)
4110 (let ((cnt 0)
4111 (oldpos (point))
4112 (b (progn
4113 (verilog-beg-of-defun)
4114 (point-marker)))
4115 (e (progn
4116 (verilog-end-of-defun)
4117 (point-marker))))
4118 (goto-char (marker-position b))
4119 (if (> (- e b) 200)
4120 (message "Relabeling module..."))
4121 (while (and
4122 (> (marker-position e) (point))
4123 (verilog-re-search-forward
4124 (concat
4125 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|\\(primitive\\)\\|\\(interface\\)\\|\\(package\\)\\|\\(case\\)\\)?\\>"
4126 "\\|\\(`endif\\)\\|\\(`else\\)")
4127 nil 'move))
4128 (goto-char (match-beginning 0))
4129 (let ((indent-str (verilog-indent-line)))
4130 (verilog-set-auto-endcomments indent-str 't)
4131 (end-of-line)
4132 (delete-horizontal-space))
4133 (setq cnt (1+ cnt))
4134 (if (= 9 (% cnt 10))
4135 (message "%d..." cnt)))
4136 (goto-char oldpos)
4137 (if (or
4138 (> (- e b) 200)
4139 (> cnt 20))
4140 (message "%d lines auto commented" cnt))))
4141
4142 (defun verilog-beg-of-statement ()
4143 "Move backward to beginning of statement."
4144 (interactive)
4145 ;; Move back token by token until we see the end
4146 ;; of some earlier line.
4147 (let (h)
4148 (while
4149 ;; If the current point does not begin a new
4150 ;; statement, as in the character ahead of us is a ';', or SOF
4151 ;; or the string after us unambiguously starts a statement,
4152 ;; or the token before us unambiguously ends a statement,
4153 ;; then move back a token and test again.
4154 (not (or
4155 ;; stop if beginning of buffer
4156 (bolp)
4157 ;; stop if we find a ;
4158 (= (preceding-char) ?\;)
4159 ;; stop if we see a named coverpoint
4160 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
4161 ;; keep going if we are in the middle of a word
4162 (not (or (looking-at "\\<") (forward-word -1)))
4163 ;; stop if we see an assertion (perhaps labeled)
4164 (and
4165 (looking-at "\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
4166 (progn
4167 (setq h (point))
4168 (save-excursion
4169 (verilog-backward-token)
4170 (if (looking-at verilog-label-re)
4171 (setq h (point))))
4172 (goto-char h)))
4173 ;; stop if we see an extended complete reg, perhaps a complete one
4174 (and
4175 (looking-at verilog-complete-reg)
4176 (let* ((p (point)))
4177 (while (and (looking-at verilog-extended-complete-re)
4178 (progn (setq p (point))
4179 (verilog-backward-token)
4180 (/= p (point)))))
4181 (goto-char p)))
4182 ;; stop if we see a complete reg (previous found extended ones)
4183 (looking-at verilog-basic-complete-re)
4184 ;; stop if previous token is an ender
4185 (save-excursion
4186 (verilog-backward-token)
4187 (or
4188 (looking-at verilog-end-block-re)
4189 (looking-at verilog-preprocessor-re))))) ;; end of test
4190 (verilog-backward-syntactic-ws)
4191 (verilog-backward-token))
4192 ;; Now point is where the previous line ended.
4193 (verilog-forward-syntactic-ws)))
4194
4195 (defun verilog-beg-of-statement-1 ()
4196 "Move backward to beginning of statement."
4197 (interactive)
4198 (if (verilog-in-comment-p)
4199 (verilog-backward-syntactic-ws))
4200 (let ((pt (point)))
4201 (catch 'done
4202 (while (not (looking-at verilog-complete-reg))
4203 (setq pt (point))
4204 (verilog-backward-syntactic-ws)
4205 (if (or (bolp)
4206 (= (preceding-char) ?\;)
4207 (save-excursion
4208 (verilog-backward-token)
4209 (looking-at verilog-ends-re)))
4210 (progn
4211 (goto-char pt)
4212 (throw 'done t))
4213 (verilog-backward-token))))
4214 (verilog-forward-syntactic-ws)))
4215 ;
4216 ; (while (and
4217 ; (not (looking-at verilog-complete-reg))
4218 ; (not (bolp))
4219 ; (not (= (preceding-char) ?\;)))
4220 ; (verilog-backward-token)
4221 ; (verilog-backward-syntactic-ws)
4222 ; (setq pt (point)))
4223 ; (goto-char pt)
4224 ; ;(verilog-forward-syntactic-ws)
4225
4226 (defun verilog-end-of-statement ()
4227 "Move forward to end of current statement."
4228 (interactive)
4229 (let ((nest 0) pos)
4230 (cond
4231 ((verilog-in-directive-p)
4232 (forward-line 1)
4233 (backward-char 1))
4234
4235 ((looking-at verilog-beg-block-re)
4236 (verilog-forward-sexp))
4237
4238 ((equal (char-after) ?\})
4239 (forward-char))
4240
4241 ;; Skip to end of statement
4242 ((condition-case nil
4243 (setq pos
4244 (catch 'found
4245 (while t
4246 (forward-sexp 1)
4247 (verilog-skip-forward-comment-or-string)
4248 (if (eolp)
4249 (forward-line 1))
4250 (cond ((looking-at "[ \t]*;")
4251 (skip-chars-forward "^;")
4252 (forward-char 1)
4253 (throw 'found (point)))
4254 ((save-excursion
4255 (forward-sexp -1)
4256 (looking-at verilog-beg-block-re))
4257 (goto-char (match-beginning 0))
4258 (throw 'found nil))
4259 ((looking-at "[ \t]*)")
4260 (throw 'found (point)))
4261 ((eobp)
4262 (throw 'found (point)))
4263 )))
4264
4265 )
4266 (error nil))
4267 (if (not pos)
4268 ;; Skip a whole block
4269 (catch 'found
4270 (while t
4271 (verilog-re-search-forward verilog-end-statement-re nil 'move)
4272 (setq nest (if (match-end 1)
4273 (1+ nest)
4274 (1- nest)))
4275 (cond ((eobp)
4276 (throw 'found (point)))
4277 ((= 0 nest)
4278 (throw 'found (verilog-end-of-statement))))))
4279 pos)))))
4280
4281 (defun verilog-in-case-region-p ()
4282 "Return true if in a case region.
4283 More specifically, point @ in the line foo : @ begin"
4284 (interactive)
4285 (save-excursion
4286 (if (and
4287 (progn (verilog-forward-syntactic-ws)
4288 (looking-at "\\<begin\\>"))
4289 (progn (verilog-backward-syntactic-ws)
4290 (= (preceding-char) ?\:)))
4291 (catch 'found
4292 (let ((nest 1))
4293 (while t
4294 (verilog-re-search-backward
4295 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
4296 "\\(\\<endcase\\>\\)\\>")
4297 nil 'move)
4298 (cond
4299 ((match-end 3)
4300 (setq nest (1+ nest)))
4301 ((match-end 2)
4302 (if (= nest 1)
4303 (throw 'found 1))
4304 (setq nest (1- nest)))
4305 (t
4306 (throw 'found (= nest 0)))))))
4307 nil)))
4308
4309 (defun verilog-backward-up-list (arg)
4310 "Call `backward-up-list' ARG, ignoring comments."
4311 (let ((parse-sexp-ignore-comments t))
4312 (backward-up-list arg)))
4313
4314 (defun verilog-forward-sexp-cmt (arg)
4315 "Call `forward-sexp' ARG, inside comments."
4316 (let ((parse-sexp-ignore-comments nil))
4317 (forward-sexp arg)))
4318
4319 (defun verilog-forward-sexp-ign-cmt (arg)
4320 "Call `forward-sexp' ARG, ignoring comments."
4321 (let ((parse-sexp-ignore-comments t))
4322 (forward-sexp arg)))
4323
4324 (defun verilog-in-generate-region-p ()
4325 "Return true if in a generate region.
4326 More specifically, after a generate and before an endgenerate."
4327 (interactive)
4328 (let ((nest 1))
4329 (save-excursion
4330 (catch 'done
4331 (while (and
4332 (/= nest 0)
4333 (verilog-re-search-backward
4334 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
4335 (cond
4336 ((match-end 1) ; module - we have crawled out
4337 (throw 'done 1))
4338 ((match-end 2) ; generate
4339 (setq nest (1- nest)))
4340 ((match-end 3) ; endgenerate
4341 (setq nest (1+ nest))))))))
4342 (= nest 0) )) ; return nest
4343
4344 (defun verilog-in-fork-region-p ()
4345 "Return true if between a fork and join."
4346 (interactive)
4347 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
4348 (nest 1))
4349 (save-excursion
4350 (while (and
4351 (/= nest 0)
4352 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
4353 (cond
4354 ((match-end 1) ; fork
4355 (setq nest (1- nest)))
4356 ((match-end 2) ; join
4357 (setq nest (1+ nest)))))))
4358 (= nest 0) )) ; return nest
4359
4360 (defun verilog-backward-case-item (lim)
4361 "Skip backward to nearest enclosing case item.
4362 Limit search to point LIM."
4363 (interactive)
4364 (let ((str 'nil)
4365 (lim1
4366 (progn
4367 (save-excursion
4368 (verilog-re-search-backward verilog-endcomment-reason-re
4369 lim 'move)
4370 (point)))))
4371 ;; Try to find the real :
4372 (if (save-excursion (search-backward ":" lim1 t))
4373 (let ((colon 0)
4374 b e )
4375 (while
4376 (and
4377 (< colon 1)
4378 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
4379 lim1 'move))
4380 (cond
4381 ((match-end 1) ;; [
4382 (setq colon (1+ colon))
4383 (if (>= colon 0)
4384 (error "%s: unbalanced [" (verilog-point-text))))
4385 ((match-end 2) ;; ]
4386 (setq colon (1- colon)))
4387
4388 ((match-end 3) ;; :
4389 (setq colon (1+ colon)))))
4390 ;; Skip back to beginning of case item
4391 (skip-chars-backward "\t ")
4392 (verilog-skip-backward-comment-or-string)
4393 (setq e (point))
4394 (setq b
4395 (progn
4396 (if
4397 (verilog-re-search-backward
4398 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
4399 (progn
4400 (cond
4401 ((match-end 1)
4402 (goto-char (match-end 1))
4403 (verilog-forward-ws&directives)
4404 (if (looking-at "(")
4405 (progn
4406 (forward-sexp)
4407 (verilog-forward-ws&directives)))
4408 (point))
4409 (t
4410 (goto-char (match-end 0))
4411 (verilog-forward-ws&directives)
4412 (point))))
4413 (error "Malformed case item"))))
4414 (setq str (buffer-substring b e))
4415 (if
4416 (setq e
4417 (string-match
4418 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4419 (setq str (concat (substring str 0 e) "...")))
4420 str)
4421 'nil)))
4422 \f
4423
4424 ;;
4425 ;; Other functions
4426 ;;
4427
4428 (defun verilog-kill-existing-comment ()
4429 "Kill auto comment on this line."
4430 (save-excursion
4431 (let* (
4432 (e (progn
4433 (end-of-line)
4434 (point)))
4435 (b (progn
4436 (beginning-of-line)
4437 (search-forward "//" e t))))
4438 (if b
4439 (delete-region (- b 2) e)))))
4440
4441 (defconst verilog-directive-nest-re
4442 (concat "\\(`else\\>\\)\\|"
4443 "\\(`endif\\>\\)\\|"
4444 "\\(`if\\>\\)\\|"
4445 "\\(`ifdef\\>\\)\\|"
4446 "\\(`ifndef\\>\\)\\|"
4447 "\\(`elsif\\>\\)"))
4448
4449 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
4450 "Add ending comment with given INDENT-STR.
4451 With KILL-EXISTING-COMMENT, remove what was there before.
4452 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
4453 Insert `// case expr ' if this line ends a case block.
4454 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
4455 Insert `// NAME ' if this line ends a function, task, module,
4456 primitive or interface named NAME."
4457 (save-excursion
4458 (cond
4459 (; Comment close preprocessor directives
4460 (and
4461 (looking-at "\\(`endif\\)\\|\\(`else\\)")
4462 (or kill-existing-comment
4463 (not (save-excursion
4464 (end-of-line)
4465 (search-backward "//" (point-at-bol) t)))))
4466 (let ((nest 1) b e
4467 m
4468 (else (if (match-end 2) "!" " ")))
4469 (end-of-line)
4470 (if kill-existing-comment
4471 (verilog-kill-existing-comment))
4472 (delete-horizontal-space)
4473 (save-excursion
4474 (backward-sexp 1)
4475 (while (and (/= nest 0)
4476 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
4477 (cond
4478 ((match-end 1) ; `else
4479 (if (= nest 1)
4480 (setq else "!")))
4481 ((match-end 2) ; `endif
4482 (setq nest (1+ nest)))
4483 ((match-end 3) ; `if
4484 (setq nest (1- nest)))
4485 ((match-end 4) ; `ifdef
4486 (setq nest (1- nest)))
4487 ((match-end 5) ; `ifndef
4488 (setq nest (1- nest)))
4489 ((match-end 6) ; `elsif
4490 (if (= nest 1)
4491 (progn
4492 (setq else "!")
4493 (setq nest 0))))))
4494 (if (match-end 0)
4495 (setq
4496 m (buffer-substring
4497 (match-beginning 0)
4498 (match-end 0))
4499 b (progn
4500 (skip-chars-forward "^ \t")
4501 (verilog-forward-syntactic-ws)
4502 (point))
4503 e (progn
4504 (skip-chars-forward "a-zA-Z0-9_")
4505 (point)))))
4506 (if b
4507 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
4508 (insert (concat " // " else m " " (buffer-substring b e))))
4509 (progn
4510 (insert " // unmatched `else, `elsif or `endif")
4511 (ding 't)))))
4512
4513 (; Comment close case/class/function/task/module and named block
4514 (and (looking-at "\\<end")
4515 (or kill-existing-comment
4516 (not (save-excursion
4517 (end-of-line)
4518 (search-backward "//" (point-at-bol) t)))))
4519 (let ((type (car indent-str)))
4520 (unless (eq type 'declaration)
4521 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ;; ignore named ends
4522 (if (looking-at verilog-end-block-ordered-re)
4523 (cond
4524 (;- This is a case block; search back for the start of this case
4525 (match-end 1) ;; of verilog-end-block-ordered-re
4526
4527 (let ((err 't)
4528 (str "UNMATCHED!!"))
4529 (save-excursion
4530 (verilog-leap-to-head)
4531 (cond
4532 ((looking-at "\\<randcase\\>")
4533 (setq str "randcase")
4534 (setq err nil))
4535 ((looking-at "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
4536 (goto-char (match-end 0))
4537 (setq str (concat (match-string 0) " " (verilog-get-expr)))
4538 (setq err nil))
4539 ))
4540 (end-of-line)
4541 (if kill-existing-comment
4542 (verilog-kill-existing-comment))
4543 (delete-horizontal-space)
4544 (insert (concat " // " str ))
4545 (if err (ding 't))))
4546
4547 (;- This is a begin..end block
4548 (match-end 2) ;; of verilog-end-block-ordered-re
4549 (let ((str " // UNMATCHED !!")
4550 (err 't)
4551 (here (point))
4552 there
4553 cntx)
4554 (save-excursion
4555 (verilog-leap-to-head)
4556 (setq there (point))
4557 (if (not (match-end 0))
4558 (progn
4559 (goto-char here)
4560 (end-of-line)
4561 (if kill-existing-comment
4562 (verilog-kill-existing-comment))
4563 (delete-horizontal-space)
4564 (insert str)
4565 (ding 't))
4566 (let ((lim
4567 (save-excursion (verilog-beg-of-defun) (point)))
4568 (here (point)))
4569 (cond
4570 (;-- handle named block differently
4571 (looking-at verilog-named-block-re)
4572 (search-forward ":")
4573 (setq there (point))
4574 (setq str (verilog-get-expr))
4575 (setq err nil)
4576 (setq str (concat " // block: " str )))
4577
4578 ((verilog-in-case-region-p) ;-- handle case item differently
4579 (goto-char here)
4580 (setq str (verilog-backward-case-item lim))
4581 (setq there (point))
4582 (setq err nil)
4583 (setq str (concat " // case: " str )))
4584
4585 (;- try to find "reason" for this begin
4586 (cond
4587 (;
4588 (eq here (progn
4589 ;; (verilog-backward-token)
4590 (verilog-beg-of-statement)
4591 (point)))
4592 (setq err nil)
4593 (setq str ""))
4594 ((looking-at verilog-endcomment-reason-re)
4595 (setq there (match-end 0))
4596 (setq cntx (concat (match-string 0) " "))
4597 (cond
4598 (;- begin
4599 (match-end 1)
4600 (setq err nil)
4601 (save-excursion
4602 (if (and (verilog-continued-line)
4603 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
4604 (progn
4605 (goto-char (match-end 0))
4606 (setq there (point))
4607 (setq str
4608 (concat " // " (match-string 0) " " (verilog-get-expr))))
4609 (setq str ""))))
4610
4611 (;- else
4612 (match-end 2)
4613 (let ((nest 0)
4614 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4615 (catch 'skip
4616 (while (verilog-re-search-backward reg nil 'move)
4617 (cond
4618 ((match-end 1) ; begin
4619 (setq nest (1- nest)))
4620 ((match-end 2) ; end
4621 (setq nest (1+ nest)))
4622 ((match-end 3)
4623 (if (= 0 nest)
4624 (progn
4625 (goto-char (match-end 0))
4626 (setq there (point))
4627 (setq err nil)
4628 (setq str (verilog-get-expr))
4629 (setq str (concat " // else: !if" str ))
4630 (throw 'skip 1))))
4631 ((match-end 4)
4632 (if (= 0 nest)
4633 (progn
4634 (goto-char (match-end 0))
4635 (setq there (point))
4636 (setq err nil)
4637 (setq str (verilog-get-expr))
4638 (setq str (concat " // else: !assert " str ))
4639 (throw 'skip 1)))))))))
4640 (;- end else
4641 (match-end 3)
4642 (goto-char there)
4643 (let ((nest 0)
4644 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4645 (catch 'skip
4646 (while (verilog-re-search-backward reg nil 'move)
4647 (cond
4648 ((match-end 1) ; begin
4649 (setq nest (1- nest)))
4650 ((match-end 2) ; end
4651 (setq nest (1+ nest)))
4652 ((match-end 3)
4653 (if (= 0 nest)
4654 (progn
4655 (goto-char (match-end 0))
4656 (setq there (point))
4657 (setq err nil)
4658 (setq str (verilog-get-expr))
4659 (setq str (concat " // else: !if" str ))
4660 (throw 'skip 1))))
4661 ((match-end 4)
4662 (if (= 0 nest)
4663 (progn
4664 (goto-char (match-end 0))
4665 (setq there (point))
4666 (setq err nil)
4667 (setq str (verilog-get-expr))
4668 (setq str (concat " // else: !assert " str ))
4669 (throw 'skip 1)))))))))
4670
4671 (; always_comb, always_ff, always_latch
4672 (or (match-end 4) (match-end 5) (match-end 6))
4673 (goto-char (match-end 0))
4674 (setq there (point))
4675 (setq err nil)
4676 (setq str (concat " // " cntx )))
4677
4678 (;- task/function/initial et cetera
4679 t
4680 (match-end 0)
4681 (goto-char (match-end 0))
4682 (setq there (point))
4683 (setq err nil)
4684 (setq str (concat " // " cntx (verilog-get-expr))))
4685
4686 (;-- otherwise...
4687 (setq str " // auto-endcomment confused "))))
4688
4689 ((and
4690 (verilog-in-case-region-p) ;-- handle case item differently
4691 (progn
4692 (setq there (point))
4693 (goto-char here)
4694 (setq str (verilog-backward-case-item lim))))
4695 (setq err nil)
4696 (setq str (concat " // case: " str )))
4697
4698 ((verilog-in-fork-region-p)
4699 (setq err nil)
4700 (setq str " // fork branch" ))
4701
4702 ((looking-at "\\<end\\>")
4703 ;; HERE
4704 (forward-word 1)
4705 (verilog-forward-syntactic-ws)
4706 (setq err nil)
4707 (setq str (verilog-get-expr))
4708 (setq str (concat " // " cntx str )))
4709
4710 ))))
4711 (goto-char here)
4712 (end-of-line)
4713 (if kill-existing-comment
4714 (verilog-kill-existing-comment))
4715 (delete-horizontal-space)
4716 (if (or err
4717 (> (count-lines here there) verilog-minimum-comment-distance))
4718 (insert str))
4719 (if err (ding 't))
4720 ))))
4721 (;- this is endclass, which can be nested
4722 (match-end 11) ;; of verilog-end-block-ordered-re
4723 ;;(goto-char there)
4724 (let ((nest 0)
4725 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
4726 string)
4727 (save-excursion
4728 (catch 'skip
4729 (while (verilog-re-search-backward reg nil 'move)
4730 (cond
4731 ((match-end 3) ; endclass
4732 (ding 't)
4733 (setq string "unmatched endclass")
4734 (throw 'skip 1))
4735
4736 ((match-end 2) ; endclass
4737 (setq nest (1+ nest)))
4738
4739 ((match-end 1) ; class
4740 (setq nest (1- nest))
4741 (if (< nest 0)
4742 (progn
4743 (goto-char (match-end 0))
4744 (let (b e)
4745 (setq b (progn
4746 (skip-chars-forward "^ \t")
4747 (verilog-forward-ws&directives)
4748 (point))
4749 e (progn
4750 (skip-chars-forward "a-zA-Z0-9_")
4751 (point)))
4752 (setq string (buffer-substring b e)))
4753 (throw 'skip 1))))
4754 ))))
4755 (end-of-line)
4756 (insert (concat " // " string ))))
4757
4758 (;- this is end{function,generate,task,module,primitive,table,generate}
4759 ;- which can not be nested.
4760 t
4761 (let (string reg (name-re nil))
4762 (end-of-line)
4763 (if kill-existing-comment
4764 (save-match-data
4765 (verilog-kill-existing-comment)))
4766 (delete-horizontal-space)
4767 (backward-sexp)
4768 (cond
4769 ((match-end 5) ;; of verilog-end-block-ordered-re
4770 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4771 (setq name-re "\\w+\\s-*("))
4772 ((match-end 6) ;; of verilog-end-block-ordered-re
4773 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4774 (setq name-re "\\w+\\s-*("))
4775 ((match-end 7) ;; of verilog-end-block-ordered-re
4776 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
4777 ((match-end 8) ;; of verilog-end-block-ordered-re
4778 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4779 ((match-end 9) ;; of verilog-end-block-ordered-re
4780 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
4781 ((match-end 10) ;; of verilog-end-block-ordered-re
4782 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4783 ((match-end 11) ;; of verilog-end-block-ordered-re
4784 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4785 ((match-end 12) ;; of verilog-end-block-ordered-re
4786 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4787 ((match-end 13) ;; of verilog-end-block-ordered-re
4788 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4789 ((match-end 14) ;; of verilog-end-block-ordered-re
4790 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4791 ((match-end 15) ;; of verilog-end-block-ordered-re
4792 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
4793
4794 (t (error "Problem in verilog-set-auto-endcomments")))
4795 (let (b e)
4796 (save-excursion
4797 (verilog-re-search-backward reg nil 'move)
4798 (cond
4799 ((match-end 1)
4800 (setq b (progn
4801 (skip-chars-forward "^ \t")
4802 (verilog-forward-ws&directives)
4803 (if (looking-at "static\\|automatic")
4804 (progn
4805 (goto-char (match-end 0))
4806 (verilog-forward-ws&directives)))
4807 (if (and name-re (verilog-re-search-forward name-re nil 'move))
4808 (progn
4809 (goto-char (match-beginning 0))
4810 (verilog-forward-ws&directives)))
4811 (point))
4812 e (progn
4813 (skip-chars-forward "a-zA-Z0-9_")
4814 (point)))
4815 (setq string (buffer-substring b e)))
4816 (t
4817 (ding 't)
4818 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
4819 (end-of-line)
4820 (insert (concat " // " string )))
4821 ))))))))))
4822
4823 (defun verilog-get-expr()
4824 "Grab expression at point, e.g., case ( a | b & (c ^d))."
4825 (let* ((b (progn
4826 (verilog-forward-syntactic-ws)
4827 (skip-chars-forward " \t")
4828 (point)))
4829 (e (let ((par 1))
4830 (cond
4831 ((looking-at "@")
4832 (forward-char 1)
4833 (verilog-forward-syntactic-ws)
4834 (if (looking-at "(")
4835 (progn
4836 (forward-char 1)
4837 (while (and (/= par 0)
4838 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4839 (cond
4840 ((match-end 1)
4841 (setq par (1+ par)))
4842 ((match-end 2)
4843 (setq par (1- par)))))))
4844 (point))
4845 ((looking-at "(")
4846 (forward-char 1)
4847 (while (and (/= par 0)
4848 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4849 (cond
4850 ((match-end 1)
4851 (setq par (1+ par)))
4852 ((match-end 2)
4853 (setq par (1- par)))))
4854 (point))
4855 ((looking-at "\\[")
4856 (forward-char 1)
4857 (while (and (/= par 0)
4858 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
4859 (cond
4860 ((match-end 1)
4861 (setq par (1+ par)))
4862 ((match-end 2)
4863 (setq par (1- par)))))
4864 (verilog-forward-syntactic-ws)
4865 (skip-chars-forward "^ \t\n\f")
4866 (point))
4867 ((looking-at "/[/\\*]")
4868 b)
4869 ('t
4870 (skip-chars-forward "^: \t\n\f")
4871 (point)))))
4872 (str (buffer-substring b e)))
4873 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4874 (setq str (concat (substring str 0 e) "...")))
4875 str))
4876
4877 (defun verilog-expand-vector ()
4878 "Take a signal vector on the current line and expand it to multiple lines.
4879 Useful for creating tri's and other expanded fields."
4880 (interactive)
4881 (verilog-expand-vector-internal "[" "]"))
4882
4883 (defun verilog-expand-vector-internal (bra ket)
4884 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
4885 (save-excursion
4886 (forward-line 0)
4887 (let ((signal-string (buffer-substring (point)
4888 (progn
4889 (end-of-line) (point)))))
4890 (if (string-match
4891 (concat "\\(.*\\)"
4892 (regexp-quote bra)
4893 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
4894 (regexp-quote ket)
4895 "\\(.*\\)$") signal-string)
4896 (let* ((sig-head (match-string 1 signal-string))
4897 (vec-start (string-to-number (match-string 2 signal-string)))
4898 (vec-end (if (= (match-beginning 3) (match-end 3))
4899 vec-start
4900 (string-to-number
4901 (substring signal-string (1+ (match-beginning 3))
4902 (match-end 3)))))
4903 (vec-range
4904 (if (= (match-beginning 4) (match-end 4))
4905 1
4906 (string-to-number
4907 (substring signal-string (+ 2 (match-beginning 4))
4908 (match-end 4)))))
4909 (sig-tail (match-string 5 signal-string))
4910 vec)
4911 ;; Decode vectors
4912 (setq vec nil)
4913 (if (< vec-range 0)
4914 (let ((tmp vec-start))
4915 (setq vec-start vec-end
4916 vec-end tmp
4917 vec-range (- vec-range))))
4918 (if (< vec-end vec-start)
4919 (while (<= vec-end vec-start)
4920 (setq vec (append vec (list vec-start)))
4921 (setq vec-start (- vec-start vec-range)))
4922 (while (<= vec-start vec-end)
4923 (setq vec (append vec (list vec-start)))
4924 (setq vec-start (+ vec-start vec-range))))
4925 ;;
4926 ;; Delete current line
4927 (delete-region (point) (progn (forward-line 0) (point)))
4928 ;;
4929 ;; Expand vector
4930 (while vec
4931 (insert (concat sig-head bra
4932 (int-to-string (car vec)) ket sig-tail "\n"))
4933 (setq vec (cdr vec)))
4934 (delete-char -1)
4935 ;;
4936 )))))
4937
4938 (defun verilog-strip-comments ()
4939 "Strip all comments from the Verilog code."
4940 (interactive)
4941 (goto-char (point-min))
4942 (while (re-search-forward "//" nil t)
4943 (if (verilog-within-string)
4944 (re-search-forward "\"" nil t)
4945 (if (verilog-in-star-comment-p)
4946 (re-search-forward "\*/" nil t)
4947 (let ((bpt (- (point) 2)))
4948 (end-of-line)
4949 (delete-region bpt (point))))))
4950 ;;
4951 (goto-char (point-min))
4952 (while (re-search-forward "/\\*" nil t)
4953 (if (verilog-within-string)
4954 (re-search-forward "\"" nil t)
4955 (let ((bpt (- (point) 2)))
4956 (re-search-forward "\\*/")
4957 (delete-region bpt (point))))))
4958
4959 (defun verilog-one-line ()
4960 "Convert structural Verilog instances to occupy one line."
4961 (interactive)
4962 (goto-char (point-min))
4963 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
4964 (replace-match "\\1 " nil nil)))
4965
4966 (defun verilog-linter-name ()
4967 "Return name of linter, either surelint or verilint."
4968 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4969 compile-command))
4970 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4971 verilog-linter)))
4972 (cond ((equal compile-word1 "surelint") `surelint)
4973 ((equal compile-word1 "verilint") `verilint)
4974 ((equal lint-word1 "surelint") `surelint)
4975 ((equal lint-word1 "verilint") `verilint)
4976 (t `surelint)))) ;; back compatibility
4977
4978 (defun verilog-lint-off ()
4979 "Convert a Verilog linter warning line into a disable statement.
4980 For example:
4981 pci_bfm_null.v, line 46: Unused input: pci_rst_
4982 becomes a comment for the appropriate tool.
4983
4984 The first word of the `compile-command' or `verilog-linter'
4985 variables is used to determine which product is being used.
4986
4987 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
4988 (interactive)
4989 (let ((linter (verilog-linter-name)))
4990 (cond ((equal linter `surelint)
4991 (verilog-surelint-off))
4992 ((equal linter `verilint)
4993 (verilog-verilint-off))
4994 (t (error "Linter name not set")))))
4995
4996 (defvar compilation-last-buffer)
4997 (defvar next-error-last-buffer)
4998
4999 (defun verilog-surelint-off ()
5000 "Convert a SureLint warning line into a disable statement.
5001 Run from Verilog source window; assumes there is a *compile* buffer
5002 with point set appropriately.
5003
5004 For example:
5005 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
5006 becomes:
5007 // surefire lint_line_off UDDONX"
5008 (interactive)
5009 (let ((buff (if (boundp 'next-error-last-buffer)
5010 next-error-last-buffer
5011 compilation-last-buffer)))
5012 (when (buffer-live-p buff)
5013 (save-excursion
5014 (switch-to-buffer buff)
5015 (beginning-of-line)
5016 (when
5017 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
5018 (let* ((code (match-string 2))
5019 (file (match-string 3))
5020 (line (match-string 4))
5021 (buffer (get-file-buffer file))
5022 dir filename)
5023 (unless buffer
5024 (progn
5025 (setq buffer
5026 (and (file-exists-p file)
5027 (find-file-noselect file)))
5028 (or buffer
5029 (let* ((pop-up-windows t))
5030 (let ((name (expand-file-name
5031 (read-file-name
5032 (format "Find this error in: (default %s) "
5033 file)
5034 dir file t))))
5035 (if (file-directory-p name)
5036 (setq name (expand-file-name filename name)))
5037 (setq buffer
5038 (and (file-exists-p name)
5039 (find-file-noselect name))))))))
5040 (switch-to-buffer buffer)
5041 (goto-char (point-min))
5042 (forward-line (- (string-to-number line)))
5043 (end-of-line)
5044 (catch 'already
5045 (cond
5046 ((verilog-in-slash-comment-p)
5047 (re-search-backward "//")
5048 (cond
5049 ((looking-at "// surefire lint_off_line ")
5050 (goto-char (match-end 0))
5051 (let ((lim (point-at-eol)))
5052 (if (re-search-forward code lim 'move)
5053 (throw 'already t)
5054 (insert (concat " " code)))))
5055 (t
5056 )))
5057 ((verilog-in-star-comment-p)
5058 (re-search-backward "/\*")
5059 (insert (format " // surefire lint_off_line %6s" code )))
5060 (t
5061 (insert (format " // surefire lint_off_line %6s" code ))
5062 )))))))))
5063
5064 (defun verilog-verilint-off ()
5065 "Convert a Verilint warning line into a disable statement.
5066
5067 For example:
5068 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
5069 becomes:
5070 //Verilint 240 off // WARNING: Unused input"
5071 (interactive)
5072 (save-excursion
5073 (beginning-of-line)
5074 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
5075 (replace-match (format
5076 ;; %3s makes numbers 1-999 line up nicely
5077 "\\1//Verilint %3s off // WARNING: \\3"
5078 (match-string 2)))
5079 (beginning-of-line)
5080 (verilog-indent-line))))
5081
5082 (defun verilog-auto-save-compile ()
5083 "Update automatics with \\[verilog-auto], save the buffer, and compile."
5084 (interactive)
5085 (verilog-auto) ; Always do it for safety
5086 (save-buffer)
5087 (compile compile-command))
5088
5089 (defun verilog-preprocess (&optional command filename)
5090 "Preprocess the buffer, similar to `compile', but put output in Verilog-Mode.
5091 Takes optional COMMAND or defaults to `verilog-preprocessor', and
5092 FILENAME to find directory to run in, or defaults to `buffer-file-name`."
5093 (interactive
5094 (list
5095 (let ((default (verilog-expand-command verilog-preprocessor)))
5096 (set (make-local-variable `verilog-preprocessor)
5097 (read-from-minibuffer "Run Preprocessor (like this): "
5098 default nil nil
5099 'verilog-preprocess-history default)))))
5100 (unless command (setq command (verilog-expand-command verilog-preprocessor)))
5101 (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
5102 (dir (file-name-directory (or filename buffer-file-name)))
5103 (cmd (concat "cd " dir "; " command)))
5104 (with-output-to-temp-buffer "*Verilog-Preprocessed*"
5105 (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
5106 (insert (concat "// " cmd "\n"))
5107 (call-process shell-file-name nil t nil shell-command-switch cmd)
5108 (verilog-mode)
5109 ;; Without this force, it takes a few idle seconds
5110 ;; to get the color, which is very jarring
5111 (unless (fboundp 'font-lock-ensure)
5112 ;; We should use font-lock-ensure in preference to
5113 ;; font-lock-fontify-buffer, but IIUC the problem this is supposed to
5114 ;; solve only appears in Emacsen older than font-lock-ensure anyway.
5115 (when fontlocked (font-lock-fontify-buffer)))))))
5116 \f
5117
5118 ;;
5119 ;; Batch
5120 ;;
5121
5122 (defun verilog-warn (string &rest args)
5123 "Print a warning with `format' using STRING and optional ARGS."
5124 (apply 'message (concat "%%Warning: " string) args))
5125
5126 (defun verilog-warn-error (string &rest args)
5127 "Call `error' using STRING and optional ARGS.
5128 If `verilog-warn-fatal' is non-nil, call `verilog-warn' instead."
5129 (if verilog-warn-fatal
5130 (apply 'error string args)
5131 (apply 'verilog-warn string args)))
5132
5133 (defmacro verilog-batch-error-wrapper (&rest body)
5134 "Execute BODY and add error prefix to any errors found.
5135 This lets programs calling batch mode to easily extract error messages."
5136 `(let ((verilog-warn-fatal nil))
5137 (condition-case err
5138 (progn ,@body)
5139 (error
5140 (error "%%Error: %s%s" (error-message-string err)
5141 (if (featurep 'xemacs) "\n" "")))))) ;; XEmacs forgets to add a newline
5142
5143 (defun verilog-batch-execute-func (funref &optional no-save)
5144 "Internal processing of a batch command.
5145 Runs FUNREF on all command arguments.
5146 Save the result unless optional NO-SAVE is t."
5147 (verilog-batch-error-wrapper
5148 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
5149 ;; However, this function is called only when Emacs is being used as
5150 ;; a standalone language instead of as an editor, so we'll live.
5151 ;;
5152 ;; General globals needed
5153 (setq make-backup-files nil)
5154 (setq-default make-backup-files nil)
5155 (setq enable-local-variables t)
5156 (setq enable-local-eval t)
5157 (setq create-lockfiles nil)
5158 ;; Make sure any sub-files we read get proper mode
5159 (setq-default major-mode 'verilog-mode)
5160 ;; Ditto files already read in
5161 ;; Remember buffer list, so don't later pickup any verilog-getopt files
5162 (let ((orig-buffer-list (buffer-list)))
5163 (mapc (lambda (buf)
5164 (when (buffer-file-name buf)
5165 (with-current-buffer buf
5166 (verilog-mode)
5167 (verilog-auto-reeval-locals)
5168 (verilog-getopt-flags))))
5169 orig-buffer-list)
5170 ;; Process the files
5171 (mapcar (lambda (buf)
5172 (when (buffer-file-name buf)
5173 (save-excursion
5174 (if (not (file-exists-p (buffer-file-name buf)))
5175 (error
5176 (concat "File not found: " (buffer-file-name buf))))
5177 (message (concat "Processing " (buffer-file-name buf)))
5178 (set-buffer buf)
5179 (funcall funref)
5180 (when (and (not no-save)
5181 (buffer-modified-p)) ;; Avoid "no changes to be saved"
5182 (save-buffer)))))
5183 orig-buffer-list))))
5184
5185 (defun verilog-batch-auto ()
5186 "For use with --batch, perform automatic expansions as a stand-alone tool.
5187 This sets up the appropriate Verilog mode environment, updates automatics
5188 with \\[verilog-auto] on all command-line files, and saves the buffers.
5189 For proper results, multiple filenames need to be passed on the command
5190 line in bottom-up order."
5191 (unless noninteractive
5192 (error "Use verilog-batch-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5193 (verilog-batch-execute-func `verilog-auto))
5194
5195 (defun verilog-batch-delete-auto ()
5196 "For use with --batch, perform automatic deletion as a stand-alone tool.
5197 This sets up the appropriate Verilog mode environment, deletes automatics
5198 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
5199 (unless noninteractive
5200 (error "Use verilog-batch-delete-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5201 (verilog-batch-execute-func `verilog-delete-auto))
5202
5203 (defun verilog-batch-delete-trailing-whitespace ()
5204 "For use with --batch, perform whitespace deletion as a stand-alone tool.
5205 This sets up the appropriate Verilog mode environment, removes
5206 whitespace with \\[verilog-delete-trailing-whitespace] on all
5207 command-line files, and saves the buffers."
5208 (unless noninteractive
5209 (error "Use verilog-batch-delete-trailing-whitespace only with --batch")) ;; Otherwise we'd mess up buffer modes
5210 (verilog-batch-execute-func `verilog-delete-trailing-whitespace))
5211
5212 (defun verilog-batch-diff-auto ()
5213 "For use with --batch, perform automatic differences as a stand-alone tool.
5214 This sets up the appropriate Verilog mode environment, expand automatics
5215 with \\[verilog-diff-auto] on all command-line files, and reports an error
5216 if any differences are observed. This is appropriate for adding to regressions
5217 to insure automatics are always properly maintained."
5218 (unless noninteractive
5219 (error "Use verilog-batch-diff-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5220 (verilog-batch-execute-func `verilog-diff-auto t))
5221
5222 (defun verilog-batch-inject-auto ()
5223 "For use with --batch, perform automatic injection as a stand-alone tool.
5224 This sets up the appropriate Verilog mode environment, injects new automatics
5225 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
5226 For proper results, multiple filenames need to be passed on the command
5227 line in bottom-up order."
5228 (unless noninteractive
5229 (error "Use verilog-batch-inject-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5230 (verilog-batch-execute-func `verilog-inject-auto))
5231
5232 (defun verilog-batch-indent ()
5233 "For use with --batch, reindent an entire file as a stand-alone tool.
5234 This sets up the appropriate Verilog mode environment, calls
5235 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
5236 (unless noninteractive
5237 (error "Use verilog-batch-indent only with --batch")) ;; Otherwise we'd mess up buffer modes
5238 (verilog-batch-execute-func `verilog-indent-buffer))
5239 \f
5240
5241 ;;
5242 ;; Indentation
5243 ;;
5244 (defconst verilog-indent-alist
5245 '((block . (+ ind verilog-indent-level))
5246 (case . (+ ind verilog-case-indent))
5247 (cparenexp . (+ ind verilog-indent-level))
5248 (cexp . (+ ind verilog-cexp-indent))
5249 (defun . verilog-indent-level-module)
5250 (declaration . verilog-indent-level-declaration)
5251 (directive . (verilog-calculate-indent-directive))
5252 (tf . verilog-indent-level)
5253 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
5254 (statement . ind)
5255 (cpp . 0)
5256 (comment . (verilog-comment-indent))
5257 (unknown . 3)
5258 (string . 0)))
5259
5260 (defun verilog-continued-line-1 (lim)
5261 "Return true if this is a continued line.
5262 Set point to where line starts. Limit search to point LIM."
5263 (let ((continued 't))
5264 (if (eq 0 (forward-line -1))
5265 (progn
5266 (end-of-line)
5267 (verilog-backward-ws&directives lim)
5268 (if (bobp)
5269 (setq continued nil)
5270 (setq continued (verilog-backward-token))))
5271 (setq continued nil))
5272 continued))
5273
5274 (defun verilog-calculate-indent ()
5275 "Calculate the indent of the current Verilog line.
5276 Examine previous lines. Once a line is found that is definitive as to the
5277 type of the current line, return that lines' indent level and its type.
5278 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
5279 (save-excursion
5280 (let* ((starting_position (point))
5281 (par 0)
5282 (begin (looking-at "[ \t]*begin\\>"))
5283 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
5284 (structres nil)
5285 (type (catch 'nesting
5286 ;; Keep working backwards until we can figure out
5287 ;; what type of statement this is.
5288 ;; Basically we need to figure out
5289 ;; 1) if this is a continuation of the previous line;
5290 ;; 2) are we in a block scope (begin..end)
5291
5292 ;; if we are in a comment, done.
5293 (if (verilog-in-star-comment-p)
5294 (throw 'nesting 'comment))
5295
5296 ;; if we have a directive, done.
5297 (if (save-excursion (beginning-of-line)
5298 (and (looking-at verilog-directive-re-1)
5299 (not (or (looking-at "[ \t]*`[ou]vm_")
5300 (looking-at "[ \t]*`vmm_")))))
5301 (throw 'nesting 'directive))
5302 ;; indent structs as if there were module level
5303 (setq structres (verilog-in-struct-nested-p))
5304 (cond ((not structres) nil)
5305 ;;((and structres (equal (char-after) ?\})) (throw 'nesting 'struct-close))
5306 ((> structres 0) (throw 'nesting 'nested-struct))
5307 ((= structres 0) (throw 'nesting 'block))
5308 (t nil))
5309
5310 ;; if we are in a parenthesized list, and the user likes to indent these, return.
5311 ;; unless we are in the newfangled coverpoint or constraint blocks
5312 (if (and
5313 verilog-indent-lists
5314 (verilog-in-paren)
5315 (not (verilog-in-coverage-p))
5316 )
5317 (progn (setq par 1)
5318 (throw 'nesting 'block)))
5319
5320 ;; See if we are continuing a previous line
5321 (while t
5322 ;; trap out if we crawl off the top of the buffer
5323 (if (bobp) (throw 'nesting 'cpp))
5324
5325 (if (and (verilog-continued-line-1 lim)
5326 (or (not (verilog-in-coverage-p))
5327 (looking-at verilog-in-constraint-re) )) ;; may still get hosed if concat in constraint
5328 (let ((sp (point)))
5329 (if (and
5330 (not (looking-at verilog-complete-reg))
5331 (verilog-continued-line-1 lim))
5332 (progn (goto-char sp)
5333 (throw 'nesting 'cexp))
5334
5335 (goto-char sp))
5336 (if (and (verilog-in-coverage-p)
5337 (looking-at verilog-in-constraint-re))
5338 (progn
5339 (beginning-of-line)
5340 (skip-chars-forward " \t")
5341 (throw 'nesting 'constraint)))
5342 (if (and begin
5343 (not verilog-indent-begin-after-if)
5344 (looking-at verilog-no-indent-begin-re))
5345 (progn
5346 (beginning-of-line)
5347 (skip-chars-forward " \t")
5348 (throw 'nesting 'statement))
5349 (progn
5350 (throw 'nesting 'cexp))))
5351 ;; not a continued line
5352 (goto-char starting_position))
5353
5354 (if (looking-at "\\<else\\>")
5355 ;; search back for governing if, striding across begin..end pairs
5356 ;; appropriately
5357 (let ((elsec 1))
5358 (while (verilog-re-search-backward verilog-ends-re nil 'move)
5359 (cond
5360 ((match-end 1) ; else, we're in deep
5361 (setq elsec (1+ elsec)))
5362 ((match-end 2) ; if
5363 (setq elsec (1- elsec))
5364 (if (= 0 elsec)
5365 (if verilog-align-ifelse
5366 (throw 'nesting 'statement)
5367 (progn ;; back up to first word on this line
5368 (beginning-of-line)
5369 (verilog-forward-syntactic-ws)
5370 (throw 'nesting 'statement)))))
5371 ((match-end 3) ; assert block
5372 (setq elsec (1- elsec))
5373 (verilog-beg-of-statement) ;; doesn't get to beginning
5374 (if (looking-at verilog-property-re)
5375 (throw 'nesting 'statement) ; We don't need an endproperty for these
5376 (throw 'nesting 'block) ;We still need an endproperty
5377 ))
5378 (t ; endblock
5379 ; try to leap back to matching outward block by striding across
5380 ; indent level changing tokens then immediately
5381 ; previous line governs indentation.
5382 (let (( reg) (nest 1))
5383 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
5384 (cond
5385 ((match-end 4) ; end
5386 ;; Search back for matching begin
5387 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
5388 ((match-end 5) ; endcase
5389 ;; Search back for matching case
5390 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5391 ((match-end 6) ; endfunction
5392 ;; Search back for matching function
5393 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5394 ((match-end 7) ; endtask
5395 ;; Search back for matching task
5396 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
5397 ((match-end 8) ; endspecify
5398 ;; Search back for matching specify
5399 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5400 ((match-end 9) ; endtable
5401 ;; Search back for matching table
5402 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5403 ((match-end 10) ; endgenerate
5404 ;; Search back for matching generate
5405 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5406 ((match-end 11) ; joins
5407 ;; Search back for matching fork
5408 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
5409 ((match-end 12) ; class
5410 ;; Search back for matching class
5411 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5412 ((match-end 13) ; covergroup
5413 ;; Search back for matching covergroup
5414 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
5415 (catch 'skip
5416 (while (verilog-re-search-backward reg nil 'move)
5417 (cond
5418 ((match-end 1) ; begin
5419 (setq nest (1- nest))
5420 (if (= 0 nest)
5421 (throw 'skip 1)))
5422 ((match-end 2) ; end
5423 (setq nest (1+ nest)))))
5424 )))))))
5425 (throw 'nesting (verilog-calc-1)))
5426 );; catch nesting
5427 );; type
5428 )
5429 ;; Return type of block and indent level.
5430 (if (not type)
5431 (setq type 'cpp))
5432 (if (> par 0) ; Unclosed Parenthesis
5433 (list 'cparenexp par)
5434 (cond
5435 ((eq type 'case)
5436 (list type (verilog-case-indent-level)))
5437 ((eq type 'statement)
5438 (list type (current-column)))
5439 ((eq type 'defun)
5440 (list type 0))
5441 ((eq type 'constraint)
5442 (list 'block (current-column)))
5443 ((eq type 'nested-struct)
5444 (list 'block structres))
5445 (t
5446 (list type (verilog-current-indent-level))))))))
5447
5448 (defun verilog-wai ()
5449 "Show matching nesting block for debugging."
5450 (interactive)
5451 (save-excursion
5452 (let* ((type (verilog-calc-1))
5453 depth)
5454 ;; Return type of block and indent level.
5455 (if (not type)
5456 (setq type 'cpp))
5457 (if (and
5458 verilog-indent-lists
5459 (not(or (verilog-in-coverage-p)
5460 (verilog-in-struct-p)))
5461 (verilog-in-paren))
5462 (setq depth 1)
5463 (cond
5464 ((eq type 'case)
5465 (setq depth (verilog-case-indent-level)))
5466 ((eq type 'statement)
5467 (setq depth (current-column)))
5468 ((eq type 'defun)
5469 (setq depth 0))
5470 (t
5471 (setq depth (verilog-current-indent-level)))))
5472 (message "You are at nesting %s depth %d" type depth))))
5473
5474 (defun verilog-calc-1 ()
5475 (catch 'nesting
5476 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)"))
5477 (inconstraint (verilog-in-coverage-p)))
5478 (while (verilog-re-search-backward re nil 'move)
5479 (catch 'continue
5480 (cond
5481 ((equal (char-after) ?\{)
5482 ;; block type returned based on outer constraint { or inner
5483 (if (verilog-at-constraint-p)
5484 (cond (inconstraint (throw 'nesting 'constraint))
5485 (t (throw 'nesting 'statement)))))
5486 ((equal (char-after) ?\})
5487 (let (par-pos
5488 (there (verilog-at-close-constraint-p)))
5489 (if there ;; we are at the } that closes a constraint. Find the { that opens it
5490 (progn
5491 (if (> (verilog-in-paren-count) 0)
5492 (forward-char 1))
5493 (setq par-pos (verilog-parenthesis-depth))
5494 (cond (par-pos
5495 (goto-char par-pos)
5496 (forward-char 1))
5497 (t
5498 (backward-char 1)))))))
5499
5500 ((looking-at verilog-beg-block-re-ordered)
5501 (cond
5502 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
5503 (let ((here (point)))
5504 (verilog-beg-of-statement)
5505 (if (looking-at verilog-extended-case-re)
5506 (throw 'nesting 'case)
5507 (goto-char here)))
5508 (throw 'nesting 'case))
5509
5510 ((match-end 4) ; *sigh* could be "disable fork"
5511 (let ((here (point)))
5512 (verilog-beg-of-statement)
5513 (if (looking-at verilog-disable-fork-re)
5514 t ; this is a normal statement
5515 (progn ; or is fork, starts a new block
5516 (goto-char here)
5517 (throw 'nesting 'block)))))
5518
5519 ((match-end 27) ; *sigh* might be a clocking declaration
5520 (let ((here (point)))
5521 (if (verilog-in-paren)
5522 t ; this is a normal statement
5523 (progn ; or is fork, starts a new block
5524 (goto-char here)
5525 (throw 'nesting 'block)))))
5526
5527 ;; need to consider typedef struct here...
5528 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
5529 ; *sigh* These words have an optional prefix:
5530 ; extern {virtual|protected}? function a();
5531 ; typedef class foo;
5532 ; and we don't want to confuse this with
5533 ; function a();
5534 ; property
5535 ; ...
5536 ; endfunction
5537 (verilog-beg-of-statement)
5538 (if (looking-at verilog-beg-block-re-ordered)
5539 (throw 'nesting 'block)
5540 (throw 'nesting 'defun)))
5541
5542 ;;
5543 ((looking-at "\\<property\\>")
5544 ; *sigh*
5545 ; {assert|assume|cover} property (); are complete
5546 ; and could also be labeled: - foo: assert property
5547 ; but
5548 ; property ID () ... needs end_property
5549 (verilog-beg-of-statement)
5550 (if (looking-at verilog-property-re)
5551 (throw 'continue 'statement) ; We don't need an endproperty for these
5552 (throw 'nesting 'block) ;We still need an endproperty
5553 ))
5554
5555 (t (throw 'nesting 'block))))
5556
5557 ((looking-at verilog-end-block-re)
5558 (verilog-leap-to-head)
5559 (if (verilog-in-case-region-p)
5560 (progn
5561 (verilog-leap-to-case-head)
5562 (if (looking-at verilog-extended-case-re)
5563 (throw 'nesting 'case)))))
5564
5565 ((looking-at verilog-defun-level-re)
5566 (if (looking-at verilog-defun-level-generate-only-re)
5567 (if (verilog-in-generate-region-p)
5568 (throw 'continue 'foo) ; always block in a generate - keep looking
5569 (throw 'nesting 'defun))
5570 (throw 'nesting 'defun)))
5571
5572 ((looking-at verilog-cpp-level-re)
5573 (throw 'nesting 'cpp))
5574
5575 ((bobp)
5576 (throw 'nesting 'cpp)))))
5577
5578 (throw 'nesting 'cpp))))
5579
5580 (defun verilog-calculate-indent-directive ()
5581 "Return indentation level for directive.
5582 For speed, the searcher looks at the last directive, not the indent
5583 of the appropriate enclosing block."
5584 (let ((base -1) ;; Indent of the line that determines our indentation
5585 (ind 0)) ;; Relative offset caused by other directives (like `endif on same line as `else)
5586 ;; Start at current location, scan back for another directive
5587
5588 (save-excursion
5589 (beginning-of-line)
5590 (while (and (< base 0)
5591 (verilog-re-search-backward verilog-directive-re nil t))
5592 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
5593 (setq base (current-indentation))))
5594 (cond ((and (looking-at verilog-directive-end) (< base 0)) ;; Only matters when not at BOL
5595 (setq ind (- ind verilog-indent-level-directive)))
5596 ((and (looking-at verilog-directive-middle) (>= base 0)) ;; Only matters when at BOL
5597 (setq ind (+ ind verilog-indent-level-directive)))
5598 ((looking-at verilog-directive-begin)
5599 (setq ind (+ ind verilog-indent-level-directive)))))
5600 ;; Adjust indent to starting indent of critical line
5601 (setq ind (max 0 (+ ind base))))
5602
5603 (save-excursion
5604 (beginning-of-line)
5605 (skip-chars-forward " \t")
5606 (cond ((or (looking-at verilog-directive-middle)
5607 (looking-at verilog-directive-end))
5608 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
5609 ind))
5610
5611 (defun verilog-leap-to-case-head ()
5612 (let ((nest 1))
5613 (while (/= 0 nest)
5614 (verilog-re-search-backward
5615 (concat
5616 "\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
5617 "\\|\\(\\<endcase\\>\\)" )
5618 nil 'move)
5619 (cond
5620 ((match-end 1)
5621 (let ((here (point)))
5622 (verilog-beg-of-statement)
5623 (unless (looking-at verilog-extended-case-re)
5624 (goto-char here)))
5625 (setq nest (1- nest)))
5626 ((match-end 3)
5627 (setq nest (1+ nest)))
5628 ((bobp)
5629 (ding 't)
5630 (setq nest 0))))))
5631
5632 (defun verilog-leap-to-head ()
5633 "Move point to the head of this block.
5634 Jump from end to matching begin, from endcase to matching case, and so on."
5635 (let ((reg nil)
5636 snest
5637 (nesting 'yes)
5638 (nest 1))
5639 (cond
5640 ((looking-at "\\<end\\>")
5641 ;; 1: Search back for matching begin
5642 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
5643 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
5644 ((looking-at "\\<endtask\\>")
5645 ;; 2: Search back for matching task
5646 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
5647 (setq nesting 'no))
5648 ((looking-at "\\<endcase\\>")
5649 (catch 'nesting
5650 (verilog-leap-to-case-head) )
5651 (setq reg nil) ; to force skip
5652 )
5653
5654 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
5655 ;; 4: Search back for matching fork
5656 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5657 ((looking-at "\\<endclass\\>")
5658 ;; 5: Search back for matching class
5659 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5660 ((looking-at "\\<endtable\\>")
5661 ;; 6: Search back for matching table
5662 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5663 ((looking-at "\\<endspecify\\>")
5664 ;; 7: Search back for matching specify
5665 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5666 ((looking-at "\\<endfunction\\>")
5667 ;; 8: Search back for matching function
5668 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
5669 (setq nesting 'no))
5670 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5671 ((looking-at "\\<endgenerate\\>")
5672 ;; 8: Search back for matching generate
5673 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5674 ((looking-at "\\<endgroup\\>")
5675 ;; 10: Search back for matching covergroup
5676 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
5677 ((looking-at "\\<endproperty\\>")
5678 ;; 11: Search back for matching property
5679 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
5680 ((looking-at verilog-uvm-end-re)
5681 ;; 12: Search back for matching sequence
5682 (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
5683 ((looking-at verilog-ovm-end-re)
5684 ;; 12: Search back for matching sequence
5685 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
5686 ((looking-at verilog-vmm-end-re)
5687 ;; 12: Search back for matching sequence
5688 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
5689 ((looking-at "\\<endinterface\\>")
5690 ;; 12: Search back for matching interface
5691 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
5692 ((looking-at "\\<endsequence\\>")
5693 ;; 12: Search back for matching sequence
5694 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
5695 ((looking-at "\\<endclocking\\>")
5696 ;; 12: Search back for matching clocking
5697 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
5698 (if reg
5699 (catch 'skip
5700 (if (eq nesting 'yes)
5701 (let (sreg)
5702 (while (verilog-re-search-backward reg nil 'move)
5703 (cond
5704 ((match-end 1) ; begin
5705 (if (looking-at "fork")
5706 (let ((here (point)))
5707 (verilog-beg-of-statement)
5708 (unless (looking-at verilog-disable-fork-re)
5709 (goto-char here)
5710 (setq nest (1- nest))))
5711 (setq nest (1- nest)))
5712 (if (= 0 nest)
5713 ;; Now previous line describes syntax
5714 (throw 'skip 1))
5715 (if (and snest
5716 (= snest nest))
5717 (setq reg sreg)))
5718 ((match-end 2) ; end
5719 (setq nest (1+ nest)))
5720 ((match-end 3)
5721 ;; endcase, jump to case
5722 (setq snest nest)
5723 (setq nest (1+ nest))
5724 (setq sreg reg)
5725 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5726 ((match-end 4)
5727 ;; join, jump to fork
5728 (setq snest nest)
5729 (setq nest (1+ nest))
5730 (setq sreg reg)
5731 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5732 )))
5733 ;; no nesting
5734 (if (and
5735 (verilog-re-search-backward reg nil 'move)
5736 (match-end 1)) ; task -> could be virtual and/or protected
5737 (progn
5738 (verilog-beg-of-statement)
5739 (throw 'skip 1))
5740 (throw 'skip 1)))))))
5741
5742 (defun verilog-continued-line ()
5743 "Return true if this is a continued line.
5744 Set point to where line starts."
5745 (let ((continued 't))
5746 (if (eq 0 (forward-line -1))
5747 (progn
5748 (end-of-line)
5749 (verilog-backward-ws&directives)
5750 (if (bobp)
5751 (setq continued nil)
5752 (while (and continued
5753 (save-excursion
5754 (skip-chars-backward " \t")
5755 (not (bolp))))
5756 (setq continued (verilog-backward-token)))))
5757 (setq continued nil))
5758 continued))
5759
5760 (defun verilog-backward-token ()
5761 "Step backward token, returning true if this is a continued line."
5762 (interactive)
5763 (verilog-backward-syntactic-ws)
5764 (cond
5765 ((bolp)
5766 nil)
5767 (;-- Anything ending in a ; is complete
5768 (= (preceding-char) ?\;)
5769 nil)
5770 (; If a "}" is prefixed by a ";", then this is a complete statement
5771 ; i.e.: constraint foo { a = b; }
5772 (= (preceding-char) ?\})
5773 (progn
5774 (backward-char)
5775 (not(verilog-at-close-constraint-p))))
5776 (;-- constraint foo { a = b }
5777 ; is a complete statement. *sigh*
5778 (= (preceding-char) ?\{)
5779 (progn
5780 (backward-char)
5781 (not (verilog-at-constraint-p))))
5782 (;" string "
5783 (= (preceding-char) ?\")
5784 (backward-char)
5785 (verilog-skip-backward-comment-or-string)
5786 nil)
5787
5788 (; [3:4]
5789 (= (preceding-char) ?\])
5790 (backward-char)
5791 (verilog-backward-open-bracket)
5792 t)
5793
5794 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
5795 ; also could be simply '@(foo)'
5796 ; or foo u1 #(a=8)
5797 ; (b, ... which ISN'T complete
5798 ;;;; Do we need this???
5799 (= (preceding-char) ?\))
5800 (progn
5801 (backward-char)
5802 (verilog-backward-up-list 1)
5803 (verilog-backward-syntactic-ws)
5804 (let ((back (point)))
5805 (forward-word -1)
5806 (cond
5807 ;;XX
5808 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
5809 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
5810 ((looking-at verilog-uvm-statement-re)
5811 nil)
5812 ((looking-at verilog-uvm-begin-re)
5813 t)
5814 ((looking-at verilog-uvm-end-re)
5815 t)
5816 ((looking-at verilog-ovm-statement-re)
5817 nil)
5818 ((looking-at verilog-ovm-begin-re)
5819 t)
5820 ((looking-at verilog-ovm-end-re)
5821 t)
5822 ;; JBA find VMM macros
5823 ((looking-at verilog-vmm-statement-re)
5824 nil )
5825 ((looking-at verilog-vmm-begin-re)
5826 t)
5827 ((looking-at verilog-vmm-end-re)
5828 nil)
5829 ;; JBA trying to catch macro lines with no ; at end
5830 ((looking-at "\\<`")
5831 nil)
5832 (t
5833 (goto-char back)
5834 (cond
5835 ((= (preceding-char) ?\@)
5836 (backward-char)
5837 (save-excursion
5838 (verilog-backward-token)
5839 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
5840 ((= (preceding-char) ?\#)
5841 (backward-char))
5842 (t t)))))))
5843
5844 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
5845 t
5846 (forward-word -1)
5847 (while (= (preceding-char) ?\_)
5848 (forward-word -1))
5849 (cond
5850 ((looking-at "\\<else\\>")
5851 t)
5852 ((looking-at verilog-behavioral-block-beg-re)
5853 t)
5854 ((looking-at verilog-indent-re)
5855 nil)
5856 (t
5857 (let
5858 ((back (point)))
5859 (verilog-backward-syntactic-ws)
5860 (cond
5861 ((= (preceding-char) ?\:)
5862 (backward-char)
5863 (verilog-backward-syntactic-ws)
5864 (backward-sexp)
5865 (if (looking-at verilog-nameable-item-re )
5866 nil
5867 t))
5868 ((= (preceding-char) ?\#)
5869 (backward-char)
5870 t)
5871 ((= (preceding-char) ?\`)
5872 (backward-char)
5873 t)
5874
5875 (t
5876 (goto-char back)
5877 t))))))))
5878
5879 (defun verilog-backward-syntactic-ws ()
5880 "Move backwards putting point after first non-whitespace non-comment."
5881 (verilog-skip-backward-comments)
5882 (forward-comment (- (buffer-size))))
5883
5884 (defun verilog-backward-syntactic-ws-quick ()
5885 "As with `verilog-backward-syntactic-ws' but use `verilog-scan' cache."
5886 (while (cond ((bobp)
5887 nil) ; Done
5888 ((> (skip-syntax-backward " ") 0)
5889 t)
5890 ((eq (preceding-char) ?\n) ;; \n's terminate // so aren't space syntax
5891 (forward-char -1)
5892 t)
5893 ((or (verilog-inside-comment-or-string-p (1- (point)))
5894 (verilog-inside-comment-or-string-p (point)))
5895 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
5896 t))))
5897
5898 (defun verilog-forward-syntactic-ws ()
5899 (verilog-skip-forward-comment-p)
5900 (forward-comment (buffer-size)))
5901
5902 (defun verilog-backward-ws&directives (&optional bound)
5903 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
5904 Optional BOUND limits search."
5905 (save-restriction
5906 (let* ((bound (or bound (point-min)))
5907 (here bound)
5908 (p nil) )
5909 (if (< bound (point))
5910 (progn
5911 (let ((state (save-excursion (verilog-syntax-ppss))))
5912 (cond
5913 ((nth 7 state) ;; in // comment
5914 (verilog-re-search-backward "//" nil 'move)
5915 (skip-chars-backward "/"))
5916 ((nth 4 state) ;; in /* */ comment
5917 (verilog-re-search-backward "/\*" nil 'move))))
5918 (narrow-to-region bound (point))
5919 (while (/= here (point))
5920 (setq here (point))
5921 (verilog-skip-backward-comments)
5922 (setq p
5923 (save-excursion
5924 (beginning-of-line)
5925 (cond
5926 ((and verilog-highlight-translate-off
5927 (verilog-within-translate-off))
5928 (verilog-back-to-start-translate-off (point-min)))
5929 ((looking-at verilog-directive-re-1)
5930 (point))
5931 (t
5932 nil))))
5933 (if p (goto-char p))))))))
5934
5935 (defun verilog-forward-ws&directives (&optional bound)
5936 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
5937 Optional BOUND limits search."
5938 (save-restriction
5939 (let* ((bound (or bound (point-max)))
5940 (here bound)
5941 jump)
5942 (if (> bound (point))
5943 (progn
5944 (let ((state (save-excursion (verilog-syntax-ppss))))
5945 (cond
5946 ((nth 7 state) ;; in // comment
5947 (end-of-line)
5948 (forward-char 1)
5949 (skip-chars-forward " \t\n\f")
5950 )
5951 ((nth 4 state) ;; in /* */ comment
5952 (verilog-re-search-forward "\*\/\\s-*" nil 'move))))
5953 (narrow-to-region (point) bound)
5954 (while (/= here (point))
5955 (setq here (point)
5956 jump nil)
5957 (forward-comment (buffer-size))
5958 (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ;; Attribute
5959 (goto-char (match-end 0)))
5960 (save-excursion
5961 (beginning-of-line)
5962 (if (looking-at verilog-directive-re-1)
5963 (setq jump t)))
5964 (if jump
5965 (beginning-of-line 2))))))))
5966
5967 (defun verilog-in-comment-p ()
5968 "Return true if in a star or // comment."
5969 (let ((state (save-excursion (verilog-syntax-ppss))))
5970 (or (nth 4 state) (nth 7 state))))
5971
5972 (defun verilog-in-star-comment-p ()
5973 "Return true if in a star comment."
5974 (let ((state (save-excursion (verilog-syntax-ppss))))
5975 (and
5976 (nth 4 state) ; t if in a comment of style a // or b /**/
5977 (not
5978 (nth 7 state) ; t if in a comment of style b /**/
5979 ))))
5980
5981 (defun verilog-in-slash-comment-p ()
5982 "Return true if in a slash comment."
5983 (let ((state (save-excursion (verilog-syntax-ppss))))
5984 (nth 7 state)))
5985
5986 (defun verilog-in-comment-or-string-p ()
5987 "Return true if in a string or comment."
5988 (let ((state (save-excursion (verilog-syntax-ppss))))
5989 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
5990
5991 (defun verilog-in-attribute-p ()
5992 "Return true if point is in an attribute (* [] attribute *)."
5993 (save-match-data
5994 (save-excursion
5995 (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
5996 (numberp (match-beginning 1)))))
5997
5998 (defun verilog-in-parameter-p ()
5999 "Return true if point is in a parameter assignment #( p1=1, p2=5)."
6000 (save-match-data
6001 (save-excursion
6002 (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
6003 (numberp (match-beginning 1)))))
6004
6005 (defun verilog-in-escaped-name-p ()
6006 "Return true if in an escaped name."
6007 (save-excursion
6008 (backward-char)
6009 (skip-chars-backward "^ \t\n\f")
6010 (if (equal (char-after (point) ) ?\\ )
6011 t
6012 nil)))
6013 (defun verilog-in-directive-p ()
6014 "Return true if in a directive."
6015 (save-excursion
6016 (beginning-of-line)
6017 (looking-at verilog-directive-re-1)))
6018
6019 (defun verilog-in-parenthesis-p ()
6020 "Return true if in a ( ) expression (but not { } or [ ])."
6021 (save-match-data
6022 (save-excursion
6023 (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
6024 (numberp (match-beginning 1)))))
6025
6026 (defun verilog-in-paren ()
6027 "Return true if in a parenthetical expression.
6028 May cache result using `verilog-syntax-ppss'."
6029 (let ((state (save-excursion (verilog-syntax-ppss))))
6030 (> (nth 0 state) 0 )))
6031
6032 (defun verilog-in-paren-count ()
6033 "Return paren depth, floor to 0.
6034 May cache result using `verilog-syntax-ppss'."
6035 (let ((state (save-excursion (verilog-syntax-ppss))))
6036 (if (> (nth 0 state) 0)
6037 (nth 0 state)
6038 0 )))
6039
6040 (defun verilog-in-paren-quick ()
6041 "Return true if in a parenthetical expression.
6042 Always starts from `point-min', to allow inserts with hooks disabled."
6043 ;; The -quick refers to its use alongside the other -quick functions,
6044 ;; not that it's likely to be faster than verilog-in-paren.
6045 (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
6046 (> (nth 0 state) 0 )))
6047
6048 (defun verilog-in-struct-p ()
6049 "Return true if in a struct declaration."
6050 (interactive)
6051 (save-excursion
6052 (if (verilog-in-paren)
6053 (progn
6054 (verilog-backward-up-list 1)
6055 (verilog-at-struct-p)
6056 )
6057 nil)))
6058
6059 (defun verilog-in-struct-nested-p ()
6060 "Return nil for not in struct.
6061 Return 0 for in non-nested struct.
6062 Return >0 for nested struct."
6063 (interactive)
6064 (let (col)
6065 (save-excursion
6066 (if (verilog-in-paren)
6067 (progn
6068 (verilog-backward-up-list 1)
6069 (setq col (verilog-at-struct-mv-p))
6070 (if col
6071 (if (verilog-in-struct-p) (current-column) 0)))
6072 nil))))
6073
6074 (defun verilog-in-coverage-p ()
6075 "Return true if in a constraint or coverpoint expression."
6076 (interactive)
6077 (save-excursion
6078 (if (verilog-in-paren)
6079 (progn
6080 (verilog-backward-up-list 1)
6081 (verilog-at-constraint-p)
6082 )
6083 nil)))
6084 (defun verilog-at-close-constraint-p ()
6085 "If at the } that closes a constraint or covergroup, return true."
6086 (if (and
6087 (equal (char-after) ?\})
6088 (verilog-in-coverage-p))
6089
6090 (save-excursion
6091 (verilog-backward-ws&directives)
6092 (if (or (equal (char-before) ?\;)
6093 (equal (char-before) ?\}) ;; can end with inner constraint { } block or ;
6094 (equal (char-before) ?\{)) ;; empty constraint block
6095 (point)
6096 nil))))
6097
6098 (defun verilog-at-constraint-p ()
6099 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
6100 (if (save-excursion
6101 (and
6102 (equal (char-after) ?\{)
6103 (forward-list)
6104 (progn (backward-char 1)
6105 (verilog-backward-ws&directives)
6106 (or (equal (char-before) ?\{) ;; empty case
6107 (equal (char-before) ?\;)
6108 (equal (char-before) ?\})))))
6109 (progn
6110 (let ( (pt (point)) (pass 0))
6111 (verilog-backward-ws&directives)
6112 (verilog-backward-token)
6113 (if (looking-at (concat "\\<constraint\\|coverpoint\\|cross\\|with\\>\\|" verilog-in-constraint-re))
6114 (progn (setq pass 1)
6115 (if (looking-at "\\<with\\>")
6116 (progn (verilog-backward-ws&directives)
6117 (beginning-of-line) ;; 1
6118 (verilog-forward-ws&directives)
6119 1 )
6120 (verilog-beg-of-statement)
6121 ))
6122 ;; if first word token not keyword, it maybe the instance name
6123 ;; check next word token
6124 (if (looking-at "\\<\\w+\\>\\|\\s-*(\\s-*\\w+")
6125 (progn (verilog-beg-of-statement)
6126 (if (looking-at (concat "\\<\\(constraint\\|"
6127 "\\(?:\\w+\\s-*:\\s-*\\)?\\(coverpoint\\|cross\\)"
6128 "\\|with\\)\\>\\|" verilog-in-constraint-re))
6129 (setq pass 1)))))
6130 (if (eq pass 0)
6131 (progn (goto-char pt) nil) 1)))
6132 ;; not
6133 nil))
6134
6135 (defun verilog-at-struct-p ()
6136 "If at the { of a struct, return true, not moving point."
6137 (save-excursion
6138 (if (and (equal (char-after) ?\{)
6139 (verilog-backward-token))
6140 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6141 nil)))
6142
6143 (defun verilog-at-struct-mv-p ()
6144 "If at the { of a struct, return true, moving point to struct."
6145 (let ((pt (point)))
6146 (if (and (equal (char-after) ?\{)
6147 (verilog-backward-token))
6148 (if (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6149 (progn (verilog-beg-of-statement) (point))
6150 (progn (goto-char pt) nil))
6151 (progn (goto-char pt) nil))))
6152
6153 (defun verilog-at-close-struct-p ()
6154 "If at the } that closes a struct, return true."
6155 (if (and
6156 (equal (char-after) ?\})
6157 (verilog-in-struct-p))
6158 ;; true
6159 (save-excursion
6160 (if (looking-at "}\\(?:\\s-*\\w+\\s-*\\)?;") 1))
6161 ;; false
6162 nil))
6163
6164 (defun verilog-parenthesis-depth ()
6165 "Return non zero if in parenthetical-expression."
6166 (save-excursion (nth 1 (verilog-syntax-ppss))))
6167
6168
6169 (defun verilog-skip-forward-comment-or-string ()
6170 "Return true if in a string or comment."
6171 (let ((state (save-excursion (verilog-syntax-ppss))))
6172 (cond
6173 ((nth 3 state) ;Inside string
6174 (search-forward "\"")
6175 t)
6176 ((nth 7 state) ;Inside // comment
6177 (forward-line 1)
6178 t)
6179 ((nth 4 state) ;Inside any comment (hence /**/)
6180 (search-forward "*/"))
6181 (t
6182 nil))))
6183
6184 (defun verilog-skip-backward-comment-or-string ()
6185 "Return true if in a string or comment."
6186 (let ((state (save-excursion (verilog-syntax-ppss))))
6187 (cond
6188 ((nth 3 state) ;Inside string
6189 (search-backward "\"")
6190 t)
6191 ((nth 7 state) ;Inside // comment
6192 (search-backward "//")
6193 (skip-chars-backward "/")
6194 t)
6195 ((nth 4 state) ;Inside /* */ comment
6196 (search-backward "/*")
6197 t)
6198 (t
6199 nil))))
6200
6201 (defun verilog-skip-backward-comments ()
6202 "Return true if a comment was skipped."
6203 (let ((more t))
6204 (while more
6205 (setq more
6206 (let ((state (save-excursion (verilog-syntax-ppss))))
6207 (cond
6208 ((nth 7 state) ;Inside // comment
6209 (search-backward "//")
6210 (skip-chars-backward "/")
6211 (skip-chars-backward " \t\n\f")
6212 t)
6213 ((nth 4 state) ;Inside /* */ comment
6214 (search-backward "/*")
6215 (skip-chars-backward " \t\n\f")
6216 t)
6217 ((and (not (bobp))
6218 (= (char-before) ?\/)
6219 (= (char-before (1- (point))) ?\*))
6220 (goto-char (- (point) 2))
6221 t) ;; Let nth 4 state handle the rest
6222 ((and (not (bobp))
6223 (= (char-before) ?\))
6224 (= (char-before (1- (point))) ?\*))
6225 (goto-char (- (point) 2))
6226 (if (search-backward "(*" nil t)
6227 (progn
6228 (skip-chars-backward " \t\n\f")
6229 t)
6230 (progn
6231 (goto-char (+ (point) 2))
6232 nil)))
6233 (t
6234 (/= (skip-chars-backward " \t\n\f") 0))))))))
6235
6236 (defun verilog-skip-forward-comment-p ()
6237 "If in comment, move to end and return true."
6238 (let* (h
6239 (state (save-excursion (verilog-syntax-ppss)))
6240 (skip (cond
6241 ((nth 3 state) ;Inside string
6242 t)
6243 ((nth 7 state) ;Inside // comment
6244 (end-of-line)
6245 (forward-char 1)
6246 t)
6247 ((nth 4 state) ;Inside /* comment
6248 (search-forward "*/")
6249 t)
6250 ((verilog-in-attribute-p) ;Inside (* attribute
6251 (search-forward "*)" nil t)
6252 t)
6253 (t nil))))
6254 (skip-chars-forward " \t\n\f")
6255 (while
6256 (cond
6257 ((looking-at "\\/\\*")
6258 (progn
6259 (setq h (point))
6260 (goto-char (match-end 0))
6261 (if (search-forward "*/" nil t)
6262 (progn
6263 (skip-chars-forward " \t\n\f")
6264 (setq skip 't))
6265 (progn
6266 (goto-char h)
6267 nil))))
6268 ((looking-at "(\\*")
6269 (progn
6270 (setq h (point))
6271 (goto-char (match-end 0))
6272 (if (search-forward "*)" nil t)
6273 (progn
6274 (skip-chars-forward " \t\n\f")
6275 (setq skip 't))
6276 (progn
6277 (goto-char h)
6278 nil))))
6279 (t nil)))
6280 skip))
6281
6282 (defun verilog-indent-line-relative ()
6283 "Cheap version of indent line.
6284 Only look at a few lines to determine indent level."
6285 (interactive)
6286 (let ((indent-str)
6287 (sp (point)))
6288 (if (looking-at "^[ \t]*$")
6289 (cond ;- A blank line; No need to be too smart.
6290 ((bobp)
6291 (setq indent-str (list 'cpp 0)))
6292 ((verilog-continued-line)
6293 (let ((sp1 (point)))
6294 (if (verilog-continued-line)
6295 (progn
6296 (goto-char sp)
6297 (setq indent-str
6298 (list 'statement (verilog-current-indent-level))))
6299 (goto-char sp1)
6300 (setq indent-str (list 'block (verilog-current-indent-level)))))
6301 (goto-char sp))
6302 ((goto-char sp)
6303 (setq indent-str (verilog-calculate-indent))))
6304 (progn (skip-chars-forward " \t")
6305 (setq indent-str (verilog-calculate-indent))))
6306 (verilog-do-indent indent-str)))
6307
6308 (defun verilog-indent-line ()
6309 "Indent for special part of code."
6310 (verilog-do-indent (verilog-calculate-indent)))
6311
6312 (defun verilog-do-indent (indent-str)
6313 (let ((type (car indent-str))
6314 (ind (car (cdr indent-str))))
6315 (cond
6316 (; handle continued exp
6317 (eq type 'cexp)
6318 (let ((here (point)))
6319 (verilog-backward-syntactic-ws)
6320 (cond
6321 ((or
6322 (= (preceding-char) ?\,)
6323 (= (preceding-char) ?\])
6324 (save-excursion
6325 (verilog-beg-of-statement-1)
6326 (looking-at verilog-declaration-re)))
6327 (let* ( fst
6328 (val
6329 (save-excursion
6330 (backward-char 1)
6331 (verilog-beg-of-statement-1)
6332 (setq fst (point))
6333 (if (looking-at verilog-declaration-re)
6334 (progn ;; we have multiple words
6335 (goto-char (match-end 0))
6336 (skip-chars-forward " \t")
6337 (cond
6338 ((and verilog-indent-declaration-macros
6339 (= (following-char) ?\`))
6340 (progn
6341 (forward-char 1)
6342 (forward-word 1)
6343 (skip-chars-forward " \t")))
6344 ((= (following-char) ?\[)
6345 (progn
6346 (forward-char 1)
6347 (verilog-backward-up-list -1)
6348 (skip-chars-forward " \t"))))
6349 (current-column))
6350 (progn
6351 (goto-char fst)
6352 (+ (current-column) verilog-cexp-indent))))))
6353 (goto-char here)
6354 (indent-line-to val)
6355 (if (and (not verilog-indent-lists)
6356 (verilog-in-paren))
6357 (verilog-pretty-declarations-auto))
6358 ))
6359 ((= (preceding-char) ?\) )
6360 (goto-char here)
6361 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6362 (indent-line-to val)))
6363 (t
6364 (goto-char here)
6365 (let ((val))
6366 (verilog-beg-of-statement-1)
6367 (if (and (< (point) here)
6368 (verilog-re-search-forward "=[ \\t]*" here 'move))
6369 (setq val (current-column))
6370 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
6371 (goto-char here)
6372 (indent-line-to val))))))
6373
6374 (; handle inside parenthetical expressions
6375 (eq type 'cparenexp)
6376 (let* ( here
6377 (val (save-excursion
6378 (verilog-backward-up-list 1)
6379 (forward-char 1)
6380 (if verilog-indent-lists
6381 (skip-chars-forward " \t")
6382 (verilog-forward-syntactic-ws))
6383 (setq here (point))
6384 (current-column)))
6385
6386 (decl (save-excursion
6387 (goto-char here)
6388 (verilog-forward-syntactic-ws)
6389 (setq here (point))
6390 (looking-at verilog-declaration-re))))
6391 (indent-line-to val)
6392 (if decl
6393 (verilog-pretty-declarations-auto))))
6394
6395 (;-- Handle the ends
6396 (or
6397 (looking-at verilog-end-block-re)
6398 (verilog-at-close-constraint-p)
6399 (verilog-at-close-struct-p))
6400 (let ((val (if (eq type 'statement)
6401 (- ind verilog-indent-level)
6402 ind)))
6403 (indent-line-to val)))
6404
6405 (;-- Case -- maybe line 'em up
6406 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
6407 (progn
6408 (cond
6409 ((looking-at "\\<endcase\\>")
6410 (indent-line-to ind))
6411 (t
6412 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6413 (indent-line-to val))))))
6414
6415 (;-- defun
6416 (and (eq type 'defun)
6417 (looking-at verilog-zero-indent-re))
6418 (indent-line-to 0))
6419
6420 (;-- declaration
6421 (and (or
6422 (eq type 'defun)
6423 (eq type 'block))
6424 (looking-at verilog-declaration-re))
6425 (verilog-indent-declaration ind))
6426
6427 (;-- Everything else
6428 t
6429 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6430 (indent-line-to val))))
6431
6432 (if (looking-at "[ \t]+$")
6433 (skip-chars-forward " \t"))
6434 indent-str ; Return indent data
6435 ))
6436
6437 (defun verilog-current-indent-level ()
6438 "Return the indent-level of the current statement."
6439 (save-excursion
6440 (let (par-pos)
6441 (beginning-of-line)
6442 (setq par-pos (verilog-parenthesis-depth))
6443 (while par-pos
6444 (goto-char par-pos)
6445 (beginning-of-line)
6446 (setq par-pos (verilog-parenthesis-depth)))
6447 (skip-chars-forward " \t")
6448 (current-column))))
6449
6450 (defun verilog-case-indent-level ()
6451 "Return the indent-level of the current statement.
6452 Do not count named blocks or case-statements."
6453 (save-excursion
6454 (skip-chars-forward " \t")
6455 (cond
6456 ((looking-at verilog-named-block-re)
6457 (current-column))
6458 ((and (not (looking-at verilog-extended-case-re))
6459 (looking-at "^[^:;]+[ \t]*:"))
6460 (verilog-re-search-forward ":" nil t)
6461 (skip-chars-forward " \t")
6462 (current-column))
6463 (t
6464 (current-column)))))
6465
6466 (defun verilog-indent-comment ()
6467 "Indent current line as comment."
6468 (let* ((stcol
6469 (cond
6470 ((verilog-in-star-comment-p)
6471 (save-excursion
6472 (re-search-backward "/\\*" nil t)
6473 (1+(current-column))))
6474 (comment-column
6475 comment-column )
6476 (t
6477 (save-excursion
6478 (re-search-backward "//" nil t)
6479 (current-column))))))
6480 (indent-line-to stcol)
6481 stcol))
6482
6483 (defun verilog-more-comment ()
6484 "Make more comment lines like the previous."
6485 (let* ((star 0)
6486 (stcol
6487 (cond
6488 ((verilog-in-star-comment-p)
6489 (save-excursion
6490 (setq star 1)
6491 (re-search-backward "/\\*" nil t)
6492 (1+(current-column))))
6493 (comment-column
6494 comment-column )
6495 (t
6496 (save-excursion
6497 (re-search-backward "//" nil t)
6498 (current-column))))))
6499 (progn
6500 (indent-to stcol)
6501 (if (and star
6502 (save-excursion
6503 (forward-line -1)
6504 (skip-chars-forward " \t")
6505 (looking-at "\*")))
6506 (insert "* ")))))
6507
6508 (defun verilog-comment-indent (&optional _arg)
6509 "Return the column number the line should be indented to.
6510 _ARG is ignored, for `comment-indent-function' compatibility."
6511 (cond
6512 ((verilog-in-star-comment-p)
6513 (save-excursion
6514 (re-search-backward "/\\*" nil t)
6515 (1+(current-column))))
6516 ( comment-column
6517 comment-column )
6518 (t
6519 (save-excursion
6520 (re-search-backward "//" nil t)
6521 (current-column)))))
6522
6523 ;;
6524
6525 (defun verilog-pretty-declarations-auto (&optional quiet)
6526 "Call `verilog-pretty-declarations' QUIET based on `verilog-auto-lineup'."
6527 (when (or (eq 'all verilog-auto-lineup)
6528 (eq 'declarations verilog-auto-lineup))
6529 (verilog-pretty-declarations quiet)))
6530
6531 (defun verilog-pretty-declarations (&optional quiet)
6532 "Line up declarations around point.
6533 Be verbose about progress unless optional QUIET set."
6534 (interactive)
6535 (let* ((m1 (make-marker))
6536 (e (point))
6537 el
6538 r
6539 (here (point))
6540 ind
6541 start
6542 startpos
6543 end
6544 endpos
6545 base-ind
6546 )
6547 (save-excursion
6548 (if (progn
6549 ; (verilog-beg-of-statement-1)
6550 (beginning-of-line)
6551 (verilog-forward-syntactic-ws)
6552 (and (not (verilog-in-directive-p)) ;; could have `define input foo
6553 (looking-at verilog-declaration-re)))
6554 (progn
6555 (if (verilog-parenthesis-depth)
6556 ;; in an argument list or parameter block
6557 (setq el (verilog-backward-up-list -1)
6558 start (progn
6559 (goto-char e)
6560 (verilog-backward-up-list 1)
6561 (forward-line) ;; ignore ( input foo,
6562 (verilog-re-search-forward verilog-declaration-re el 'move)
6563 (goto-char (match-beginning 0))
6564 (skip-chars-backward " \t")
6565 (point))
6566 startpos (set-marker (make-marker) start)
6567 end (progn
6568 (goto-char start)
6569 (verilog-backward-up-list -1)
6570 (forward-char -1)
6571 (verilog-backward-syntactic-ws)
6572 (point))
6573 endpos (set-marker (make-marker) end)
6574 base-ind (progn
6575 (goto-char start)
6576 (forward-char 1)
6577 (skip-chars-forward " \t")
6578 (current-column)))
6579 ;; in a declaration block (not in argument list)
6580 (setq
6581 start (progn
6582 (verilog-beg-of-statement-1)
6583 (while (and (looking-at verilog-declaration-re)
6584 (not (bobp)))
6585 (skip-chars-backward " \t")
6586 (setq e (point))
6587 (beginning-of-line)
6588 (verilog-backward-syntactic-ws)
6589 (backward-char)
6590 (verilog-beg-of-statement-1))
6591 e)
6592 startpos (set-marker (make-marker) start)
6593 end (progn
6594 (goto-char here)
6595 (verilog-end-of-statement)
6596 (setq e (point)) ;Might be on last line
6597 (verilog-forward-syntactic-ws)
6598 (while (looking-at verilog-declaration-re)
6599 (verilog-end-of-statement)
6600 (setq e (point))
6601 (verilog-forward-syntactic-ws))
6602 e)
6603 endpos (set-marker (make-marker) end)
6604 base-ind (progn
6605 (goto-char start)
6606 (verilog-do-indent (verilog-calculate-indent))
6607 (verilog-forward-ws&directives)
6608 (current-column))))
6609 ;; OK, start and end are set
6610 (goto-char (marker-position startpos))
6611 (if (and (not quiet)
6612 (> (- end start) 100))
6613 (message "Lining up declarations..(please stand by)"))
6614 ;; Get the beginning of line indent first
6615 (while (progn (setq e (marker-position endpos))
6616 (< (point) e))
6617 (cond
6618 ((save-excursion (skip-chars-backward " \t")
6619 (bolp))
6620 (verilog-forward-ws&directives)
6621 (indent-line-to base-ind)
6622 (verilog-forward-ws&directives)
6623 (if (< (point) e)
6624 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6625 (t
6626 (just-one-space)
6627 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6628 ;;(forward-line)
6629 )
6630 ;; Now find biggest prefix
6631 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
6632 ;; Now indent each line.
6633 (goto-char (marker-position startpos))
6634 (while (progn (setq e (marker-position endpos))
6635 (setq r (- e (point)))
6636 (> r 0))
6637 (setq e (point))
6638 (unless quiet (message "%d" r))
6639 ;;(verilog-do-indent (verilog-calculate-indent)))
6640 (verilog-forward-ws&directives)
6641 (cond
6642 ((or (and verilog-indent-declaration-macros
6643 (looking-at verilog-declaration-re-2-macro))
6644 (looking-at verilog-declaration-re-2-no-macro))
6645 (let ((p (match-end 0)))
6646 (set-marker m1 p)
6647 (if (verilog-re-search-forward "[[#`]" p 'move)
6648 (progn
6649 (forward-char -1)
6650 (just-one-space)
6651 (goto-char (marker-position m1))
6652 (just-one-space)
6653 (indent-to ind))
6654 (progn
6655 (just-one-space)
6656 (indent-to ind)))))
6657 ((verilog-continued-line-1 (marker-position startpos))
6658 (goto-char e)
6659 (indent-line-to ind))
6660 ((verilog-in-struct-p)
6661 ;; could have a declaration of a user defined item
6662 (goto-char e)
6663 (verilog-end-of-statement))
6664 (t ; Must be comment or white space
6665 (goto-char e)
6666 (verilog-forward-ws&directives)
6667 (forward-line -1)))
6668 (forward-line 1))
6669 (unless quiet (message "")))))))
6670
6671 (defun verilog-pretty-expr (&optional quiet _myre)
6672 "Line up expressions around point, optionally QUIET with regexp _MYRE ignored."
6673 (interactive)
6674 (if (not (verilog-in-comment-or-string-p))
6675 (save-excursion
6676 (let ( (rexp (concat "^\\s-*" verilog-complete-reg))
6677 (rexp1 (concat "^\\s-*" verilog-basic-complete-re)))
6678 (beginning-of-line)
6679 (if (and (not (looking-at rexp ))
6680 (looking-at verilog-assignment-operation-re)
6681 (save-excursion
6682 (goto-char (match-end 2))
6683 (and (not (verilog-in-attribute-p))
6684 (not (verilog-in-parameter-p))
6685 (not (verilog-in-comment-or-string-p)))))
6686 (let* ((here (point))
6687 (e) (r)
6688 (start
6689 (progn
6690 (beginning-of-line)
6691 (setq e (point))
6692 (verilog-backward-syntactic-ws)
6693 (beginning-of-line)
6694 (while (and (not (looking-at rexp1))
6695 (looking-at verilog-assignment-operation-re)
6696 (not (bobp))
6697 )
6698 (setq e (point))
6699 (verilog-backward-syntactic-ws)
6700 (beginning-of-line)
6701 ) ;Ack, need to grok `define
6702 e))
6703 (end
6704 (progn
6705 (goto-char here)
6706 (end-of-line)
6707 (setq e (point)) ;Might be on last line
6708 (verilog-forward-syntactic-ws)
6709 (beginning-of-line)
6710 (while (and
6711 (not (looking-at rexp1 ))
6712 (looking-at verilog-assignment-operation-re)
6713 (progn
6714 (end-of-line)
6715 (not (eq e (point)))))
6716 (setq e (point))
6717 (verilog-forward-syntactic-ws)
6718 (beginning-of-line)
6719 )
6720 e))
6721 (endpos (set-marker (make-marker) end))
6722 (ind)
6723 )
6724 (goto-char start)
6725 (verilog-do-indent (verilog-calculate-indent))
6726 (if (and (not quiet)
6727 (> (- end start) 100))
6728 (message "Lining up expressions..(please stand by)"))
6729
6730 ;; Set indent to minimum throughout region
6731 (while (< (point) (marker-position endpos))
6732 (beginning-of-line)
6733 (verilog-just-one-space verilog-assignment-operation-re)
6734 (beginning-of-line)
6735 (verilog-do-indent (verilog-calculate-indent))
6736 (end-of-line)
6737 (verilog-forward-syntactic-ws)
6738 )
6739
6740 ;; Now find biggest prefix
6741 (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
6742
6743 ;; Now indent each line.
6744 (goto-char start)
6745 (while (progn (setq e (marker-position endpos))
6746 (setq r (- e (point)))
6747 (> r 0))
6748 (setq e (point))
6749 (if (not quiet) (message "%d" r))
6750 (cond
6751 ((looking-at verilog-assignment-operation-re)
6752 (goto-char (match-beginning 2))
6753 (if (not (or (verilog-in-parenthesis-p) ;; leave attributes and comparisons alone
6754 (verilog-in-coverage-p)))
6755 (if (eq (char-after) ?=)
6756 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
6757 (indent-to ind)
6758 ))
6759 )
6760 ((verilog-continued-line-1 start)
6761 (goto-char e)
6762 (indent-line-to ind))
6763 (t ; Must be comment or white space
6764 (goto-char e)
6765 (verilog-forward-ws&directives)
6766 (forward-line -1))
6767 )
6768 (forward-line 1))
6769 (unless quiet (message ""))
6770 ))))))
6771
6772 (defun verilog-just-one-space (myre)
6773 "Remove extra spaces around regular expression MYRE."
6774 (interactive)
6775 (if (and (not(looking-at verilog-complete-reg))
6776 (looking-at myre))
6777 (let ((p1 (match-end 1))
6778 (p2 (match-end 2)))
6779 (progn
6780 (goto-char p2)
6781 (just-one-space)
6782 (goto-char p1)
6783 (just-one-space)))))
6784
6785 (defun verilog-indent-declaration (baseind)
6786 "Indent current lines as declaration.
6787 Line up the variable names based on previous declaration's indentation.
6788 BASEIND is the base indent to offset everything."
6789 (interactive)
6790 (let ((pos (point-marker))
6791 (lim (save-excursion
6792 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
6793 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
6794 (point)))
6795 (ind)
6796 (val)
6797 (m1 (make-marker)))
6798 (setq val
6799 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6800 (indent-line-to val)
6801
6802 ;; Use previous declaration (in this module) as template.
6803 (if (or (eq 'all verilog-auto-lineup)
6804 (eq 'declarations verilog-auto-lineup))
6805 (if (verilog-re-search-backward
6806 (or (and verilog-indent-declaration-macros
6807 verilog-declaration-re-1-macro)
6808 verilog-declaration-re-1-no-macro) lim t)
6809 (progn
6810 (goto-char (match-end 0))
6811 (skip-chars-forward " \t")
6812 (setq ind (current-column))
6813 (goto-char pos)
6814 (setq val
6815 (+ baseind
6816 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6817 (indent-line-to val)
6818 (if (and verilog-indent-declaration-macros
6819 (looking-at verilog-declaration-re-2-macro))
6820 (let ((p (match-end 0)))
6821 (set-marker m1 p)
6822 (if (verilog-re-search-forward "[[#`]" p 'move)
6823 (progn
6824 (forward-char -1)
6825 (just-one-space)
6826 (goto-char (marker-position m1))
6827 (just-one-space)
6828 (indent-to ind))
6829 (if (/= (current-column) ind)
6830 (progn
6831 (just-one-space)
6832 (indent-to ind)))))
6833 (if (looking-at verilog-declaration-re-2-no-macro)
6834 (let ((p (match-end 0)))
6835 (set-marker m1 p)
6836 (if (verilog-re-search-forward "[[`#]" p 'move)
6837 (progn
6838 (forward-char -1)
6839 (just-one-space)
6840 (goto-char (marker-position m1))
6841 (just-one-space)
6842 (indent-to ind))
6843 (if (/= (current-column) ind)
6844 (progn
6845 (just-one-space)
6846 (indent-to ind))))))))))
6847 (goto-char pos)))
6848
6849 (defun verilog-get-lineup-indent (b edpos)
6850 "Return the indent level that will line up several lines within the region.
6851 Region is defined by B and EDPOS."
6852 (save-excursion
6853 (let ((ind 0) e)
6854 (goto-char b)
6855 ;; Get rightmost position
6856 (while (progn (setq e (marker-position edpos))
6857 (< (point) e))
6858 (if (verilog-re-search-forward
6859 (or (and verilog-indent-declaration-macros
6860 verilog-declaration-re-1-macro)
6861 verilog-declaration-re-1-no-macro) e 'move)
6862 (progn
6863 (goto-char (match-end 0))
6864 (verilog-backward-syntactic-ws)
6865 (if (> (current-column) ind)
6866 (setq ind (current-column)))
6867 (goto-char (match-end 0)))))
6868 (if (> ind 0)
6869 (1+ ind)
6870 ;; No lineup-string found
6871 (goto-char b)
6872 (end-of-line)
6873 (verilog-backward-syntactic-ws)
6874 ;;(skip-chars-backward " \t")
6875 (1+ (current-column))))))
6876
6877 (defun verilog-get-lineup-indent-2 (myre b edpos)
6878 "Return the indent level that will line up several lines within the region."
6879 (save-excursion
6880 (let ((ind 0) e)
6881 (goto-char b)
6882 ;; Get rightmost position
6883 (while (progn (setq e (marker-position edpos))
6884 (< (point) e))
6885 (if (and (verilog-re-search-forward myre e 'move)
6886 (not (verilog-in-attribute-p))) ;; skip attribute exprs
6887 (progn
6888 (goto-char (match-beginning 2))
6889 (verilog-backward-syntactic-ws)
6890 (if (> (current-column) ind)
6891 (setq ind (current-column)))
6892 (goto-char (match-end 0)))
6893 ))
6894 (if (> ind 0)
6895 (1+ ind)
6896 ;; No lineup-string found
6897 (goto-char b)
6898 (end-of-line)
6899 (skip-chars-backward " \t")
6900 (1+ (current-column))))))
6901
6902 (defun verilog-comment-depth (type val)
6903 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
6904 (save-excursion
6905 (let
6906 ((b (prog2
6907 (beginning-of-line)
6908 (point-marker)
6909 (end-of-line))))
6910 (if (re-search-backward " /\\* \[#-\]# \[a-zA-Z\]+ \[0-9\]+ ## \\*/" b t)
6911 (progn
6912 (replace-match " /* -# ## */")
6913 (end-of-line))
6914 (progn
6915 (end-of-line)
6916 (insert " /* ## ## */"))))
6917 (backward-char 6)
6918 (insert
6919 (format "%s %d" type val))))
6920
6921 ;; \f
6922 ;;
6923 ;; Completion
6924 ;;
6925 (defvar verilog-str nil)
6926 (defvar verilog-all nil)
6927 (defvar verilog-pred nil)
6928 (defvar verilog-buffer-to-use nil)
6929 (defvar verilog-flag nil)
6930 (defvar verilog-toggle-completions nil
6931 "True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
6932 Repeated use of \\[verilog-complete-word] will show you all of them.
6933 Normally, when there is more than one possible completion,
6934 it displays a list of all possible completions.")
6935
6936
6937 (defvar verilog-type-keywords
6938 '(
6939 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
6940 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
6941 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
6942 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
6943 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
6944 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
6945 )
6946 "Keywords for types used when completing a word in a declaration or parmlist.
6947 \(integer, real, reg...)")
6948
6949 (defvar verilog-cpp-keywords
6950 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
6951 "endif")
6952 "Keywords to complete when at first word of a line in declarative scope.
6953 \(initial, always, begin, assign...)
6954 The procedures and variables defined within the Verilog program
6955 will be completed at runtime and should not be added to this list.")
6956
6957 (defvar verilog-defun-keywords
6958 (append
6959 '(
6960 "always" "always_comb" "always_ff" "always_latch" "assign"
6961 "begin" "end" "generate" "endgenerate" "module" "endmodule"
6962 "specify" "endspecify" "function" "endfunction" "initial" "final"
6963 "task" "endtask" "primitive" "endprimitive"
6964 )
6965 verilog-type-keywords)
6966 "Keywords to complete when at first word of a line in declarative scope.
6967 \(initial, always, begin, assign...)
6968 The procedures and variables defined within the Verilog program
6969 will be completed at runtime and should not be added to this list.")
6970
6971 (defvar verilog-block-keywords
6972 '(
6973 "begin" "break" "case" "continue" "else" "end" "endfunction"
6974 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
6975 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
6976 "while")
6977 "Keywords to complete when at first word of a line in behavioral scope.
6978 \(begin, if, then, else, for, fork...)
6979 The procedures and variables defined within the Verilog program
6980 will be completed at runtime and should not be added to this list.")
6981
6982 (defvar verilog-tf-keywords
6983 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
6984 "Keywords to complete when at first word of a line in a task or function.
6985 \(begin, if, then, else, for, fork.)
6986 The procedures and variables defined within the Verilog program
6987 will be completed at runtime and should not be added to this list.")
6988
6989 (defvar verilog-case-keywords
6990 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
6991 "Keywords to complete when at first word of a line in case scope.
6992 \(begin, if, then, else, for, fork...)
6993 The procedures and variables defined within the Verilog program
6994 will be completed at runtime and should not be added to this list.")
6995
6996 (defvar verilog-separator-keywords
6997 '("else" "then" "begin")
6998 "Keywords to complete when NOT standing at the first word of a statement.
6999 \(else, then, begin...)
7000 Variables and function names defined within the Verilog program
7001 will be completed at runtime and should not be added to this list.")
7002
7003 (defvar verilog-gate-ios
7004 ;; All these have an implied {"input"...} at the end
7005 '(("and" "output")
7006 ("buf" "output")
7007 ("bufif0" "output")
7008 ("bufif1" "output")
7009 ("cmos" "output")
7010 ("nand" "output")
7011 ("nmos" "output")
7012 ("nor" "output")
7013 ("not" "output")
7014 ("notif0" "output")
7015 ("notif1" "output")
7016 ("or" "output")
7017 ("pmos" "output")
7018 ("pulldown" "output")
7019 ("pullup" "output")
7020 ("rcmos" "output")
7021 ("rnmos" "output")
7022 ("rpmos" "output")
7023 ("rtran" "inout" "inout")
7024 ("rtranif0" "inout" "inout")
7025 ("rtranif1" "inout" "inout")
7026 ("tran" "inout" "inout")
7027 ("tranif0" "inout" "inout")
7028 ("tranif1" "inout" "inout")
7029 ("xnor" "output")
7030 ("xor" "output"))
7031 "Map of direction for each positional argument to each gate primitive.")
7032
7033 (defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
7034 "Keywords for gate primitives.")
7035
7036 (defun verilog-string-diff (str1 str2)
7037 "Return index of first letter where STR1 and STR2 differs."
7038 (catch 'done
7039 (let ((diff 0))
7040 (while t
7041 (if (or (> (1+ diff) (length str1))
7042 (> (1+ diff) (length str2)))
7043 (throw 'done diff))
7044 (or (equal (aref str1 diff) (aref str2 diff))
7045 (throw 'done diff))
7046 (setq diff (1+ diff))))))
7047
7048 ;; Calculate all possible completions for functions if argument is `function',
7049 ;; completions for procedures if argument is `procedure' or both functions and
7050 ;; procedures otherwise.
7051
7052 (defun verilog-func-completion (type)
7053 "Build regular expression for module/task/function names.
7054 TYPE is 'module, 'tf for task or function, or t if unknown."
7055 (if (string= verilog-str "")
7056 (setq verilog-str "[a-zA-Z_]"))
7057 (let ((verilog-str (concat (cond
7058 ((eq type 'module) "\\<\\(module\\)\\s +")
7059 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
7060 (t "\\<\\(task\\|function\\|module\\)\\s +"))
7061 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
7062 match)
7063
7064 (if (not (looking-at verilog-defun-re))
7065 (verilog-re-search-backward verilog-defun-re nil t))
7066 (forward-char 1)
7067
7068 ;; Search through all reachable functions
7069 (goto-char (point-min))
7070 (while (verilog-re-search-forward verilog-str (point-max) t)
7071 (progn (setq match (buffer-substring (match-beginning 2)
7072 (match-end 2)))
7073 (if (or (null verilog-pred)
7074 (funcall verilog-pred match))
7075 (setq verilog-all (cons match verilog-all)))))
7076 (if (match-beginning 0)
7077 (goto-char (match-beginning 0)))))
7078
7079 (defun verilog-get-completion-decl (end)
7080 "Macro for searching through current declaration (var, type or const)
7081 for matches of `str' and adding the occurrence tp `all' through point END."
7082 (let ((re (or (and verilog-indent-declaration-macros
7083 verilog-declaration-re-2-macro)
7084 verilog-declaration-re-2-no-macro))
7085 decl-end match)
7086 ;; Traverse lines
7087 (while (and (< (point) end)
7088 (verilog-re-search-forward re end t))
7089 ;; Traverse current line
7090 (setq decl-end (save-excursion (verilog-declaration-end)))
7091 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
7092 (not (match-end 1)))
7093 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
7094 (if (string-match (concat "\\<" verilog-str) match)
7095 (if (or (null verilog-pred)
7096 (funcall verilog-pred match))
7097 (setq verilog-all (cons match verilog-all)))))
7098 (forward-line 1)))
7099 verilog-all)
7100
7101 (defun verilog-var-completion ()
7102 "Calculate all possible completions for variables (or constants)."
7103 (let ((start (point)))
7104 ;; Search for all reachable var declarations
7105 (verilog-beg-of-defun)
7106 (save-excursion
7107 ;; Check var declarations
7108 (verilog-get-completion-decl start))))
7109
7110 (defun verilog-keyword-completion (keyword-list)
7111 "Give list of all possible completions of keywords in KEYWORD-LIST."
7112 (mapcar (lambda (s)
7113 (if (string-match (concat "\\<" verilog-str) s)
7114 (if (or (null verilog-pred)
7115 (funcall verilog-pred s))
7116 (setq verilog-all (cons s verilog-all)))))
7117 keyword-list))
7118
7119
7120 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
7121 "Function passed to `completing-read', `try-completion' or `all-completions'.
7122 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
7123 must be a function to be called for every match to check if this should
7124 really be a match. If VERILOG-FLAG is t, the function returns a list of
7125 all possible completions. If VERILOG-FLAG is nil it returns a string,
7126 the longest possible completion, or t if VERILOG-STR is an exact match.
7127 If VERILOG-FLAG is 'lambda, the function returns t if VERILOG-STR is an
7128 exact match, nil otherwise."
7129 (save-excursion
7130 (let ((verilog-all nil))
7131 ;; Set buffer to use for searching labels. This should be set
7132 ;; within functions which use verilog-completions
7133 (set-buffer verilog-buffer-to-use)
7134
7135 ;; Determine what should be completed
7136 (let ((state (car (verilog-calculate-indent))))
7137 (cond ((eq state 'defun)
7138 (save-excursion (verilog-var-completion))
7139 (verilog-func-completion 'module)
7140 (verilog-keyword-completion verilog-defun-keywords))
7141
7142 ((eq state 'behavioral)
7143 (save-excursion (verilog-var-completion))
7144 (verilog-func-completion 'module)
7145 (verilog-keyword-completion verilog-defun-keywords))
7146
7147 ((eq state 'block)
7148 (save-excursion (verilog-var-completion))
7149 (verilog-func-completion 'tf)
7150 (verilog-keyword-completion verilog-block-keywords))
7151
7152 ((eq state 'case)
7153 (save-excursion (verilog-var-completion))
7154 (verilog-func-completion 'tf)
7155 (verilog-keyword-completion verilog-case-keywords))
7156
7157 ((eq state 'tf)
7158 (save-excursion (verilog-var-completion))
7159 (verilog-func-completion 'tf)
7160 (verilog-keyword-completion verilog-tf-keywords))
7161
7162 ((eq state 'cpp)
7163 (save-excursion (verilog-var-completion))
7164 (verilog-keyword-completion verilog-cpp-keywords))
7165
7166 ((eq state 'cparenexp)
7167 (save-excursion (verilog-var-completion)))
7168
7169 (t;--Anywhere else
7170 (save-excursion (verilog-var-completion))
7171 (verilog-func-completion 'both)
7172 (verilog-keyword-completion verilog-separator-keywords))))
7173
7174 ;; Now we have built a list of all matches. Give response to caller
7175 (verilog-completion-response))))
7176
7177 (defun verilog-completion-response ()
7178 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
7179 ;; This was not called by all-completions
7180 (if (null verilog-all)
7181 ;; Return nil if there was no matching label
7182 nil
7183 ;; Get longest string common in the labels
7184 ;; FIXME: Why not use `try-completion'?
7185 (let* ((elm (cdr verilog-all))
7186 (match (car verilog-all))
7187 (min (length match))
7188 tmp)
7189 (if (string= match verilog-str)
7190 ;; Return t if first match was an exact match
7191 (setq match t)
7192 (while (not (null elm))
7193 ;; Find longest common string
7194 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
7195 (progn
7196 (setq min tmp)
7197 (setq match (substring match 0 min))))
7198 ;; Terminate with match=t if this is an exact match
7199 (if (string= (car elm) verilog-str)
7200 (progn
7201 (setq match t)
7202 (setq elm nil))
7203 (setq elm (cdr elm)))))
7204 ;; If this is a test just for exact match, return nil ot t
7205 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
7206 nil
7207 match))))
7208 ;; If flag is t, this was called by all-completions. Return
7209 ;; list of all possible completions
7210 (verilog-flag
7211 verilog-all)))
7212
7213 (defvar verilog-last-word-numb 0)
7214 (defvar verilog-last-word-shown nil)
7215 (defvar verilog-last-completions nil)
7216
7217 (defun verilog-complete-word ()
7218 "Complete word at current point.
7219 \(See also `verilog-toggle-completions', `verilog-type-keywords',
7220 and `verilog-separator-keywords'.)"
7221 ;; FIXME: Provide completion-at-point-function.
7222 (interactive)
7223 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7224 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7225 (verilog-str (buffer-substring b e))
7226 ;; The following variable is used in verilog-completion
7227 (verilog-buffer-to-use (current-buffer))
7228 (allcomp (if (and verilog-toggle-completions
7229 (string= verilog-last-word-shown verilog-str))
7230 verilog-last-completions
7231 (all-completions verilog-str 'verilog-completion)))
7232 (match (if verilog-toggle-completions
7233 "" (try-completion
7234 verilog-str (mapcar (lambda (elm)
7235 (cons elm 0)) allcomp)))))
7236 ;; Delete old string
7237 (delete-region b e)
7238
7239 ;; Toggle-completions inserts whole labels
7240 (if verilog-toggle-completions
7241 (progn
7242 ;; Update entry number in list
7243 (setq verilog-last-completions allcomp
7244 verilog-last-word-numb
7245 (if (>= verilog-last-word-numb (1- (length allcomp)))
7246 0
7247 (1+ verilog-last-word-numb)))
7248 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
7249 ;; Display next match or same string if no match was found
7250 (if (not (null allcomp))
7251 (insert "" verilog-last-word-shown)
7252 (insert "" verilog-str)
7253 (message "(No match)")))
7254 ;; The other form of completion does not necessarily do that.
7255
7256 ;; Insert match if found, or the original string if no match
7257 (if (or (null match) (equal match 't))
7258 (progn (insert "" verilog-str)
7259 (message "(No match)"))
7260 (insert "" match))
7261 ;; Give message about current status of completion
7262 (cond ((equal match 't)
7263 (if (not (null (cdr allcomp)))
7264 (message "(Complete but not unique)")
7265 (message "(Sole completion)")))
7266 ;; Display buffer if the current completion didn't help
7267 ;; on completing the label.
7268 ((and (not (null (cdr allcomp))) (= (length verilog-str)
7269 (length match)))
7270 (with-output-to-temp-buffer "*Completions*"
7271 (display-completion-list allcomp))
7272 ;; Wait for a key press. Then delete *Completion* window
7273 (momentary-string-display "" (point))
7274 (delete-window (get-buffer-window (get-buffer "*Completions*")))
7275 )))))
7276
7277 (defun verilog-show-completions ()
7278 "Show all possible completions at current point."
7279 (interactive)
7280 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7281 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7282 (verilog-str (buffer-substring b e))
7283 ;; The following variable is used in verilog-completion
7284 (verilog-buffer-to-use (current-buffer))
7285 (allcomp (if (and verilog-toggle-completions
7286 (string= verilog-last-word-shown verilog-str))
7287 verilog-last-completions
7288 (all-completions verilog-str 'verilog-completion))))
7289 ;; Show possible completions in a temporary buffer.
7290 (with-output-to-temp-buffer "*Completions*"
7291 (display-completion-list allcomp))
7292 ;; Wait for a key press. Then delete *Completion* window
7293 (momentary-string-display "" (point))
7294 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
7295
7296
7297 (defun verilog-get-default-symbol ()
7298 "Return symbol around current point as a string."
7299 (save-excursion
7300 (buffer-substring (progn
7301 (skip-chars-backward " \t")
7302 (skip-chars-backward "a-zA-Z0-9_")
7303 (point))
7304 (progn
7305 (skip-chars-forward "a-zA-Z0-9_")
7306 (point)))))
7307
7308 (defun verilog-build-defun-re (str &optional arg)
7309 "Return function/task/module starting with STR as regular expression.
7310 With optional second ARG non-nil, STR is the complete name of the instruction."
7311 (if arg
7312 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
7313 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
7314
7315 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
7316 "Function passed to `completing-read', `try-completion' or `all-completions'.
7317 Returns a completion on any function name based on VERILOG-STR prefix. If
7318 VERILOG-PRED is non-nil, it must be a function to be called for every match
7319 to check if this should really be a match. If VERILOG-FLAG is t, the
7320 function returns a list of all possible completions. If it is nil it
7321 returns a string, the longest possible completion, or t if VERILOG-STR is
7322 an exact match. If VERILOG-FLAG is 'lambda, the function returns t if
7323 VERILOG-STR is an exact match, nil otherwise."
7324 (save-excursion
7325 (let ((verilog-all nil)
7326 match)
7327
7328 ;; Set buffer to use for searching labels. This should be set
7329 ;; within functions which use verilog-completions
7330 (set-buffer verilog-buffer-to-use)
7331
7332 (let ((verilog-str verilog-str))
7333 ;; Build regular expression for functions
7334 (if (string= verilog-str "")
7335 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
7336 (setq verilog-str (verilog-build-defun-re verilog-str)))
7337 (goto-char (point-min))
7338
7339 ;; Build a list of all possible completions
7340 (while (verilog-re-search-forward verilog-str nil t)
7341 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
7342 (if (or (null verilog-pred)
7343 (funcall verilog-pred match))
7344 (setq verilog-all (cons match verilog-all)))))
7345
7346 ;; Now we have built a list of all matches. Give response to caller
7347 (verilog-completion-response))))
7348
7349 (defun verilog-goto-defun ()
7350 "Move to specified Verilog module/interface/task/function.
7351 The default is a name found in the buffer around point.
7352 If search fails, other files are checked based on
7353 `verilog-library-flags'."
7354 (interactive)
7355 (let* ((default (verilog-get-default-symbol))
7356 ;; The following variable is used in verilog-comp-function
7357 (verilog-buffer-to-use (current-buffer))
7358 (label (if (not (string= default ""))
7359 ;; Do completion with default
7360 (completing-read (concat "Goto-Label: (default "
7361 default ") ")
7362 'verilog-comp-defun nil nil "")
7363 ;; There is no default value. Complete without it
7364 (completing-read "Goto-Label: "
7365 'verilog-comp-defun nil nil "")))
7366 pt)
7367 ;; Make sure library paths are correct, in case need to resolve module
7368 (verilog-auto-reeval-locals)
7369 (verilog-getopt-flags)
7370 ;; If there was no response on prompt, use default value
7371 (if (string= label "")
7372 (setq label default))
7373 ;; Goto right place in buffer if label is not an empty string
7374 (or (string= label "")
7375 (progn
7376 (save-excursion
7377 (goto-char (point-min))
7378 (setq pt
7379 (re-search-forward (verilog-build-defun-re label t) nil t)))
7380 (when pt
7381 (goto-char pt)
7382 (beginning-of-line))
7383 pt)
7384 (verilog-goto-defun-file label))))
7385
7386 ;; Eliminate compile warning
7387 (defvar occur-pos-list)
7388
7389 (defun verilog-showscopes ()
7390 "List all scopes in this module."
7391 (interactive)
7392 (let ((buffer (current-buffer))
7393 (linenum 1)
7394 (nlines 0)
7395 (first 1)
7396 (prevpos (point-min))
7397 (final-context-start (make-marker))
7398 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
7399 (with-output-to-temp-buffer "*Occur*"
7400 (save-excursion
7401 (message (format "Searching for %s ..." regexp))
7402 ;; Find next match, but give up if prev match was at end of buffer.
7403 (while (and (not (= prevpos (point-max)))
7404 (verilog-re-search-forward regexp nil t))
7405 (goto-char (match-beginning 0))
7406 (beginning-of-line)
7407 (save-match-data
7408 (setq linenum (+ linenum (count-lines prevpos (point)))))
7409 (setq prevpos (point))
7410 (goto-char (match-end 0))
7411 (let* ((start (save-excursion
7412 (goto-char (match-beginning 0))
7413 (forward-line (if (< nlines 0) nlines (- nlines)))
7414 (point)))
7415 (end (save-excursion
7416 (goto-char (match-end 0))
7417 (if (> nlines 0)
7418 (forward-line (1+ nlines))
7419 (forward-line 1))
7420 (point)))
7421 (tag (format "%3d" linenum))
7422 (empty (make-string (length tag) ?\ ))
7423 tem)
7424 (save-excursion
7425 (setq tem (make-marker))
7426 (set-marker tem (point))
7427 (set-buffer standard-output)
7428 (setq occur-pos-list (cons tem occur-pos-list))
7429 (or first (zerop nlines)
7430 (insert "--------\n"))
7431 (setq first nil)
7432 (insert-buffer-substring buffer start end)
7433 (backward-char (- end start))
7434 (setq tem (if (< nlines 0) (- nlines) nlines))
7435 (while (> tem 0)
7436 (insert empty ?:)
7437 (forward-line 1)
7438 (setq tem (1- tem)))
7439 (let ((this-linenum linenum))
7440 (set-marker final-context-start
7441 (+ (point) (- (match-end 0) (match-beginning 0))))
7442 (while (< (point) final-context-start)
7443 (if (null tag)
7444 (setq tag (format "%3d" this-linenum)))
7445 (insert tag ?:)))))))
7446 (set-buffer-modified-p nil))))
7447
7448
7449 ;; Highlight helper functions
7450 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
7451 (defun verilog-within-translate-off ()
7452 "Return point if within translate-off region, else nil."
7453 (and (save-excursion
7454 (re-search-backward
7455 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
7456 nil t))
7457 (equal "off" (match-string 2))
7458 (point)))
7459
7460 (defun verilog-start-translate-off (limit)
7461 "Return point before translate-off directive if before LIMIT, else nil."
7462 (when (re-search-forward
7463 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7464 limit t)
7465 (match-beginning 0)))
7466
7467 (defun verilog-back-to-start-translate-off (limit)
7468 "Return point before translate-off directive if before LIMIT, else nil."
7469 (when (re-search-backward
7470 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7471 limit t)
7472 (match-beginning 0)))
7473
7474 (defun verilog-end-translate-off (limit)
7475 "Return point after translate-on directive if before LIMIT, else nil."
7476
7477 (re-search-forward (concat
7478 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
7479
7480 (defun verilog-match-translate-off (limit)
7481 "Match a translate-off block, setting `match-data' and returning t, else nil.
7482 Bound search by LIMIT."
7483 (when (< (point) limit)
7484 (let ((start (or (verilog-within-translate-off)
7485 (verilog-start-translate-off limit)))
7486 (case-fold-search t))
7487 (when start
7488 (let ((end (or (verilog-end-translate-off limit) limit)))
7489 (set-match-data (list start end))
7490 (goto-char end))))))
7491
7492 (defun verilog-font-lock-match-item (limit)
7493 "Match, and move over, any declaration item after point.
7494 Bound search by LIMIT. Adapted from
7495 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
7496 (condition-case nil
7497 (save-restriction
7498 (narrow-to-region (point-min) limit)
7499 ;; match item
7500 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
7501 (save-match-data
7502 (goto-char (match-end 1))
7503 ;; move to next item
7504 (if (looking-at "\\(\\s-*,\\)")
7505 (goto-char (match-end 1))
7506 (end-of-line) t))))
7507 (error nil)))
7508
7509
7510 ;; Added by Subbu Meiyappan for Header
7511
7512 (defun verilog-header ()
7513 "Insert a standard Verilog file header.
7514 See also `verilog-sk-header' for an alternative format."
7515 (interactive)
7516 (let ((start (point)))
7517 (insert "\
7518 //-----------------------------------------------------------------------------
7519 // Title : <title>
7520 // Project : <project>
7521 //-----------------------------------------------------------------------------
7522 // File : <filename>
7523 // Author : <author>
7524 // Created : <credate>
7525 // Last modified : <moddate>
7526 //-----------------------------------------------------------------------------
7527 // Description :
7528 // <description>
7529 //-----------------------------------------------------------------------------
7530 // Copyright (c) <copydate> by <company> This model is the confidential and
7531 // proprietary property of <company> and the possession or use of this
7532 // file requires a written license from <company>.
7533 //------------------------------------------------------------------------------
7534 // Modification history :
7535 // <modhist>
7536 //-----------------------------------------------------------------------------
7537
7538 ")
7539 (goto-char start)
7540 (search-forward "<filename>")
7541 (replace-match (buffer-name) t t)
7542 (search-forward "<author>") (replace-match "" t t)
7543 (insert (user-full-name))
7544 (insert " <" (user-login-name) "@" (system-name) ">")
7545 (search-forward "<credate>") (replace-match "" t t)
7546 (verilog-insert-date)
7547 (search-forward "<moddate>") (replace-match "" t t)
7548 (verilog-insert-date)
7549 (search-forward "<copydate>") (replace-match "" t t)
7550 (verilog-insert-year)
7551 (search-forward "<modhist>") (replace-match "" t t)
7552 (verilog-insert-date)
7553 (insert " : created")
7554 (goto-char start)
7555 (let (string)
7556 (setq string (read-string "title: "))
7557 (search-forward "<title>")
7558 (replace-match string t t)
7559 (setq string (read-string "project: " verilog-project))
7560 (setq verilog-project string)
7561 (search-forward "<project>")
7562 (replace-match string t t)
7563 (setq string (read-string "Company: " verilog-company))
7564 (setq verilog-company string)
7565 (search-forward "<company>")
7566 (replace-match string t t)
7567 (search-forward "<company>")
7568 (replace-match string t t)
7569 (search-forward "<company>")
7570 (replace-match string t t)
7571 (search-backward "<description>")
7572 (replace-match "" t t))))
7573
7574 ;; verilog-header Uses the verilog-insert-date function
7575
7576 (defun verilog-insert-date ()
7577 "Insert date from the system."
7578 (interactive)
7579 (if verilog-date-scientific-format
7580 (insert (format-time-string "%Y/%m/%d"))
7581 (insert (format-time-string "%d.%m.%Y"))))
7582
7583 (defun verilog-insert-year ()
7584 "Insert year from the system."
7585 (interactive)
7586 (insert (format-time-string "%Y")))
7587
7588 \f
7589 ;;
7590 ;; Signal list parsing
7591 ;;
7592
7593 ;; Elements of a signal list
7594 ;; Unfortunately we use 'assoc' on this, so can't be a vector
7595 (defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
7596 (list name bits comment mem enum signed type multidim modport))
7597 (defsubst verilog-sig-name (sig)
7598 (car sig))
7599 (defsubst verilog-sig-bits (sig) ;; First element of packed array (pre signal-name)
7600 (nth 1 sig))
7601 (defsubst verilog-sig-comment (sig)
7602 (nth 2 sig))
7603 (defsubst verilog-sig-memory (sig) ;; Unpacked array (post signal-name)
7604 (nth 3 sig))
7605 (defsubst verilog-sig-enum (sig)
7606 (nth 4 sig))
7607 (defsubst verilog-sig-signed (sig)
7608 (nth 5 sig))
7609 (defsubst verilog-sig-type (sig)
7610 (nth 6 sig))
7611 (defsubst verilog-sig-type-set (sig type)
7612 (setcar (nthcdr 6 sig) type))
7613 (defsubst verilog-sig-multidim (sig) ;; Second and additional elements of packed array
7614 (nth 7 sig))
7615 (defsubst verilog-sig-multidim-string (sig)
7616 (if (verilog-sig-multidim sig)
7617 (let ((str "") (args (verilog-sig-multidim sig)))
7618 (while args
7619 (setq str (concat str (car args)))
7620 (setq args (cdr args)))
7621 str)))
7622 (defsubst verilog-sig-modport (sig)
7623 (nth 8 sig))
7624 (defsubst verilog-sig-width (sig)
7625 (verilog-make-width-expression (verilog-sig-bits sig)))
7626
7627 (defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
7628 (vector outputs-del outputs-imm temps inputs))
7629 (defsubst verilog-alw-get-outputs-delayed (sigs)
7630 (aref sigs 0))
7631 (defsubst verilog-alw-get-outputs-immediate (sigs)
7632 (aref sigs 1))
7633 (defsubst verilog-alw-get-temps (sigs)
7634 (aref sigs 2))
7635 (defsubst verilog-alw-get-inputs (sigs)
7636 (aref sigs 3))
7637 (defsubst verilog-alw-get-uses-delayed (sigs)
7638 (aref sigs 0))
7639
7640 (defsubst verilog-modport-new (name clockings decls)
7641 (list name clockings decls))
7642 (defsubst verilog-modport-name (sig)
7643 (car sig))
7644 (defsubst verilog-modport-clockings (sig)
7645 (nth 1 sig)) ;; Returns list of names
7646 (defsubst verilog-modport-clockings-add (sig val)
7647 (setcar (nthcdr 1 sig) (cons val (nth 1 sig))))
7648 (defsubst verilog-modport-decls (sig)
7649 (nth 2 sig)) ;; Returns verilog-decls-* structure
7650 (defsubst verilog-modport-decls-set (sig val)
7651 (setcar (nthcdr 2 sig) val))
7652
7653 (defsubst verilog-modi-new (name fob pt type)
7654 (vector name fob pt type))
7655 (defsubst verilog-modi-name (modi)
7656 (aref modi 0))
7657 (defsubst verilog-modi-file-or-buffer (modi)
7658 (aref modi 1))
7659 (defsubst verilog-modi-get-point (modi)
7660 (aref modi 2))
7661 (defsubst verilog-modi-get-type (modi) ;; "module" or "interface"
7662 (aref modi 3))
7663 (defsubst verilog-modi-get-decls (modi)
7664 (verilog-modi-cache-results modi 'verilog-read-decls))
7665 (defsubst verilog-modi-get-sub-decls (modi)
7666 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
7667
7668 ;; Signal reading for given module
7669 ;; Note these all take modi's - as returned from verilog-modi-current
7670 (defsubst verilog-decls-new (out inout in vars modports assigns consts gparams interfaces)
7671 (vector out inout in vars modports assigns consts gparams interfaces))
7672 (defsubst verilog-decls-append (a b)
7673 (cond ((not a) b) ((not b) a)
7674 (t (vector (append (aref a 0) (aref b 0)) (append (aref a 1) (aref b 1))
7675 (append (aref a 2) (aref b 2)) (append (aref a 3) (aref b 3))
7676 (append (aref a 4) (aref b 4)) (append (aref a 5) (aref b 5))
7677 (append (aref a 6) (aref b 6)) (append (aref a 7) (aref b 7))
7678 (append (aref a 8) (aref b 8))))))
7679 (defsubst verilog-decls-get-outputs (decls)
7680 (aref decls 0))
7681 (defsubst verilog-decls-get-inouts (decls)
7682 (aref decls 1))
7683 (defsubst verilog-decls-get-inputs (decls)
7684 (aref decls 2))
7685 (defsubst verilog-decls-get-vars (decls)
7686 (aref decls 3))
7687 (defsubst verilog-decls-get-modports (decls) ;; Also for clocking blocks; contains another verilog-decls struct
7688 (aref decls 4)) ;; Returns verilog-modport* structure
7689 (defsubst verilog-decls-get-assigns (decls)
7690 (aref decls 5))
7691 (defsubst verilog-decls-get-consts (decls)
7692 (aref decls 6))
7693 (defsubst verilog-decls-get-gparams (decls)
7694 (aref decls 7))
7695 (defsubst verilog-decls-get-interfaces (decls)
7696 (aref decls 8))
7697
7698
7699 (defsubst verilog-subdecls-new (out inout in intf intfd)
7700 (vector out inout in intf intfd))
7701 (defsubst verilog-subdecls-get-outputs (subdecls)
7702 (aref subdecls 0))
7703 (defsubst verilog-subdecls-get-inouts (subdecls)
7704 (aref subdecls 1))
7705 (defsubst verilog-subdecls-get-inputs (subdecls)
7706 (aref subdecls 2))
7707 (defsubst verilog-subdecls-get-interfaces (subdecls)
7708 (aref subdecls 3))
7709 (defsubst verilog-subdecls-get-interfaced (subdecls)
7710 (aref subdecls 4))
7711
7712 (defun verilog-signals-from-signame (signame-list)
7713 "Return signals in standard form from SIGNAME-LIST, a simple list of names."
7714 (mapcar (lambda (name) (verilog-sig-new name nil nil nil nil nil nil nil nil))
7715 signame-list))
7716
7717 (defun verilog-signals-in (in-list not-list)
7718 "Return list of signals in IN-LIST that are also in NOT-LIST.
7719 Also remove any duplicates in IN-LIST.
7720 Signals must be in standard (base vector) form."
7721 ;; This function is hot, so implemented as O(1)
7722 (cond ((eval-when-compile (fboundp 'make-hash-table))
7723 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7724 (ht-not (make-hash-table :test 'equal :rehash-size 4.0))
7725 out-list)
7726 (while not-list
7727 (puthash (car (car not-list)) t ht-not)
7728 (setq not-list (cdr not-list)))
7729 (while in-list
7730 (when (and (gethash (verilog-sig-name (car in-list)) ht-not)
7731 (not (gethash (verilog-sig-name (car in-list)) ht)))
7732 (setq out-list (cons (car in-list) out-list))
7733 (puthash (verilog-sig-name (car in-list)) t ht))
7734 (setq in-list (cdr in-list)))
7735 (nreverse out-list)))
7736 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7737 (t
7738 (let (out-list)
7739 (while in-list
7740 (if (and (assoc (verilog-sig-name (car in-list)) not-list)
7741 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7742 (setq out-list (cons (car in-list) out-list)))
7743 (setq in-list (cdr in-list)))
7744 (nreverse out-list)))))
7745 ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("C" "")))
7746
7747 (defun verilog-signals-not-in (in-list not-list)
7748 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7749 Also remove any duplicates in IN-LIST.
7750 Signals must be in standard (base vector) form."
7751 ;; This function is hot, so implemented as O(1)
7752 (cond ((eval-when-compile (fboundp 'make-hash-table))
7753 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7754 out-list)
7755 (while not-list
7756 (puthash (car (car not-list)) t ht)
7757 (setq not-list (cdr not-list)))
7758 (while in-list
7759 (when (not (gethash (verilog-sig-name (car in-list)) ht))
7760 (setq out-list (cons (car in-list) out-list))
7761 (puthash (verilog-sig-name (car in-list)) t ht))
7762 (setq in-list (cdr in-list)))
7763 (nreverse out-list)))
7764 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7765 (t
7766 (let (out-list)
7767 (while in-list
7768 (if (and (not (assoc (verilog-sig-name (car in-list)) not-list))
7769 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7770 (setq out-list (cons (car in-list) out-list)))
7771 (setq in-list (cdr in-list)))
7772 (nreverse out-list)))))
7773 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
7774
7775 (defun verilog-signals-memory (in-list)
7776 "Return list of signals in IN-LIST that are memorized (multidimensional)."
7777 (let (out-list)
7778 (while in-list
7779 (if (nth 3 (car in-list))
7780 (setq out-list (cons (car in-list) out-list)))
7781 (setq in-list (cdr in-list)))
7782 out-list))
7783 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
7784
7785 (defun verilog-signals-sort-compare (a b)
7786 "Compare signal A and B for sorting."
7787 (string< (verilog-sig-name a) (verilog-sig-name b)))
7788
7789 (defun verilog-signals-not-params (in-list)
7790 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
7791 (let (out-list)
7792 (while in-list
7793 ;; Namespace intentionally short for AUTOs and compatibility
7794 (unless (boundp (intern (concat "vh-" (verilog-sig-name (car in-list)))))
7795 (setq out-list (cons (car in-list) out-list)))
7796 (setq in-list (cdr in-list)))
7797 (nreverse out-list)))
7798
7799 (defun verilog-signals-with (func in-list)
7800 "Return list of signals where FUNC is true executed on each signal in IN-LIST."
7801 (let (out-list)
7802 (while in-list
7803 (when (funcall func (car in-list))
7804 (setq out-list (cons (car in-list) out-list)))
7805 (setq in-list (cdr in-list)))
7806 (nreverse out-list)))
7807
7808 (defun verilog-signals-combine-bus (in-list)
7809 "Return a list of signals in IN-LIST, with buses combined.
7810 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
7811 (let (combo buswarn
7812 out-list
7813 sig highbit lowbit ; Temp information about current signal
7814 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
7815 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
7816 sv-modport
7817 bus)
7818 ;; Shove signals so duplicated signals will be adjacent
7819 (setq in-list (sort in-list `verilog-signals-sort-compare))
7820 (while in-list
7821 (setq sig (car in-list))
7822 ;; No current signal; form from existing details
7823 (unless sv-name
7824 (setq sv-name (verilog-sig-name sig)
7825 sv-highbit nil
7826 sv-busstring nil
7827 sv-comment (verilog-sig-comment sig)
7828 sv-memory (verilog-sig-memory sig)
7829 sv-enum (verilog-sig-enum sig)
7830 sv-signed (verilog-sig-signed sig)
7831 sv-type (verilog-sig-type sig)
7832 sv-multidim (verilog-sig-multidim sig)
7833 sv-modport (verilog-sig-modport sig)
7834 combo ""
7835 buswarn ""))
7836 ;; Extract bus details
7837 (setq bus (verilog-sig-bits sig))
7838 (setq bus (and bus (verilog-simplify-range-expression bus)))
7839 (cond ((and bus
7840 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
7841 (setq highbit (string-to-number (match-string 1 bus))
7842 lowbit (string-to-number
7843 (match-string 2 bus))))
7844 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
7845 (setq highbit (string-to-number (match-string 1 bus))
7846 lowbit highbit))))
7847 ;; Combine bits in bus
7848 (if sv-highbit
7849 (setq sv-highbit (max highbit sv-highbit)
7850 sv-lowbit (min lowbit sv-lowbit))
7851 (setq sv-highbit highbit
7852 sv-lowbit lowbit)))
7853 (bus
7854 ;; String, probably something like `preproc:0
7855 (setq sv-busstring bus)))
7856 ;; Peek ahead to next signal
7857 (setq in-list (cdr in-list))
7858 (setq sig (car in-list))
7859 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
7860 ;; Combine with this signal
7861 (when (and sv-busstring
7862 (not (equal sv-busstring (verilog-sig-bits sig))))
7863 (when nil ;; Debugging
7864 (message (concat "Warning, can't merge into single bus "
7865 sv-name bus
7866 ", the AUTOs may be wrong")))
7867 (setq buswarn ", Couldn't Merge"))
7868 (if (verilog-sig-comment sig) (setq combo ", ..."))
7869 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
7870 sv-enum (or sv-enum (verilog-sig-enum sig))
7871 sv-signed (or sv-signed (verilog-sig-signed sig))
7872 sv-type (or sv-type (verilog-sig-type sig))
7873 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
7874 sv-modport (or sv-modport (verilog-sig-modport sig))))
7875 ;; Doesn't match next signal, add to queue, zero in prep for next
7876 ;; Note sig may also be nil for the last signal in the list
7877 (t
7878 (setq out-list
7879 (cons (verilog-sig-new
7880 sv-name
7881 (or sv-busstring
7882 (if sv-highbit
7883 (concat "[" (int-to-string sv-highbit) ":"
7884 (int-to-string sv-lowbit) "]")))
7885 (concat sv-comment combo buswarn)
7886 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
7887 out-list)
7888 sv-name nil))))
7889 ;;
7890 out-list))
7891
7892 (defun verilog-sig-tieoff (sig)
7893 "Return tieoff expression for given SIG, with appropriate width.
7894 Tieoff value uses `verilog-active-low-regexp' and
7895 `verilog-auto-reset-widths'."
7896 (concat
7897 (if (and verilog-active-low-regexp
7898 (verilog-string-match-fold verilog-active-low-regexp (verilog-sig-name sig)))
7899 "~" "")
7900 (cond ((not verilog-auto-reset-widths)
7901 "0")
7902 ((equal verilog-auto-reset-widths 'unbased)
7903 "'0")
7904 ;; Else presume verilog-auto-reset-widths is true
7905 (t
7906 (let* ((width (verilog-sig-width sig)))
7907 (cond ((not width)
7908 "`0/*NOWIDTH*/")
7909 ((string-match "^[0-9]+$" width)
7910 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
7911 (t
7912 (concat "{" width "{1'b0}}"))))))))
7913
7914 ;;
7915 ;; Dumping
7916 ;;
7917
7918 (defun verilog-decls-princ (decls &optional header prefix)
7919 "For debug, dump the `verilog-read-decls' structure DECLS."
7920 (when decls
7921 (if header (princ header))
7922 (setq prefix (or prefix ""))
7923 (verilog-signals-princ (verilog-decls-get-outputs decls)
7924 (concat prefix "Outputs:\n") (concat prefix " "))
7925 (verilog-signals-princ (verilog-decls-get-inouts decls)
7926 (concat prefix "Inout:\n") (concat prefix " "))
7927 (verilog-signals-princ (verilog-decls-get-inputs decls)
7928 (concat prefix "Inputs:\n") (concat prefix " "))
7929 (verilog-signals-princ (verilog-decls-get-vars decls)
7930 (concat prefix "Vars:\n") (concat prefix " "))
7931 (verilog-signals-princ (verilog-decls-get-assigns decls)
7932 (concat prefix "Assigns:\n") (concat prefix " "))
7933 (verilog-signals-princ (verilog-decls-get-consts decls)
7934 (concat prefix "Consts:\n") (concat prefix " "))
7935 (verilog-signals-princ (verilog-decls-get-gparams decls)
7936 (concat prefix "Gparams:\n") (concat prefix " "))
7937 (verilog-signals-princ (verilog-decls-get-interfaces decls)
7938 (concat prefix "Interfaces:\n") (concat prefix " "))
7939 (verilog-modport-princ (verilog-decls-get-modports decls)
7940 (concat prefix "Modports:\n") (concat prefix " "))
7941 (princ "\n")))
7942
7943 (defun verilog-signals-princ (signals &optional header prefix)
7944 "For debug, dump internal SIGNALS structures, with HEADER and PREFIX."
7945 (when signals
7946 (if header (princ header))
7947 (while signals
7948 (let ((sig (car signals)))
7949 (setq signals (cdr signals))
7950 (princ prefix)
7951 (princ "\"") (princ (verilog-sig-name sig)) (princ "\"")
7952 (princ " bits=") (princ (verilog-sig-bits sig))
7953 (princ " cmt=") (princ (verilog-sig-comment sig))
7954 (princ " mem=") (princ (verilog-sig-memory sig))
7955 (princ " enum=") (princ (verilog-sig-enum sig))
7956 (princ " sign=") (princ (verilog-sig-signed sig))
7957 (princ " type=") (princ (verilog-sig-type sig))
7958 (princ " dim=") (princ (verilog-sig-multidim sig))
7959 (princ " modp=") (princ (verilog-sig-modport sig))
7960 (princ "\n")))))
7961
7962 (defun verilog-modport-princ (modports &optional header prefix)
7963 "For debug, dump internal MODPORT structures, with HEADER and PREFIX."
7964 (when modports
7965 (if header (princ header))
7966 (while modports
7967 (let ((sig (car modports)))
7968 (setq modports (cdr modports))
7969 (princ prefix)
7970 (princ "\"") (princ (verilog-modport-name sig)) (princ "\"")
7971 (princ " clockings=") (princ (verilog-modport-clockings sig))
7972 (princ "\n")
7973 (verilog-decls-princ (verilog-modport-decls sig)
7974 (concat prefix " syms:\n")
7975 (concat prefix " "))))))
7976
7977 ;;
7978 ;; Port/Wire/Etc Reading
7979 ;;
7980
7981 (defun verilog-read-inst-backward-name ()
7982 "Internal. Move point back to beginning of inst-name."
7983 (verilog-backward-open-paren)
7984 (let (done)
7985 (while (not done)
7986 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil) ; ] isn't word boundary
7987 (cond ((looking-at ")")
7988 (verilog-backward-open-paren))
7989 (t (setq done t)))))
7990 (while (looking-at "\\]")
7991 (verilog-backward-open-bracket)
7992 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil))
7993 (skip-chars-backward "a-zA-Z0-9`_$"))
7994
7995 (defun verilog-read-inst-module-matcher ()
7996 "Set match data 0 with module_name when point is inside instantiation."
7997 (verilog-read-inst-backward-name)
7998 ;; Skip over instantiation name
7999 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
8000 ;; Check for parameterized instantiations
8001 (when (looking-at ")")
8002 (verilog-backward-open-paren)
8003 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
8004 (skip-chars-backward "a-zA-Z0-9'_$")
8005 ;; #1 is legal syntax for gate primitives
8006 (when (save-excursion
8007 (verilog-backward-syntactic-ws-quick)
8008 (eq ?# (char-before)))
8009 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil)
8010 (skip-chars-backward "a-zA-Z0-9'_$"))
8011 (looking-at "[a-zA-Z0-9`_\$]+")
8012 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8013 (buffer-substring-no-properties (match-beginning 0) (match-end 0))
8014 ;; Caller assumes match-beginning/match-end is still set
8015 )
8016
8017 (defun verilog-read-inst-module ()
8018 "Return module_name when point is inside instantiation."
8019 (save-excursion
8020 (verilog-read-inst-module-matcher)))
8021
8022 (defun verilog-read-inst-name ()
8023 "Return instance_name when point is inside instantiation."
8024 (save-excursion
8025 (verilog-read-inst-backward-name)
8026 (looking-at "[a-zA-Z0-9`_\$]+")
8027 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8028 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
8029
8030 (defun verilog-read-module-name ()
8031 "Return module name when after its ( or ;."
8032 (save-excursion
8033 (re-search-backward "[(;]")
8034 ;; Due to "module x import y (" we must search for declaration begin
8035 (verilog-re-search-backward-quick verilog-defun-re nil nil)
8036 (goto-char (match-end 0))
8037 (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_\$]+" nil nil)
8038 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8039 (verilog-symbol-detick
8040 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
8041
8042 (defun verilog-read-inst-param-value ()
8043 "Return list of parameters and values when point is inside instantiation."
8044 (save-excursion
8045 (verilog-read-inst-backward-name)
8046 ;; Skip over instantiation name
8047 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
8048 ;; If there are parameterized instantiations
8049 (when (looking-at ")")
8050 (let ((end-pt (point))
8051 params
8052 param-name paren-beg-pt param-value)
8053 (verilog-backward-open-paren)
8054 (while (verilog-re-search-forward-quick "\\." end-pt t)
8055 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_\$]\\)" nil nil)
8056 (skip-chars-backward "a-zA-Z0-9'_$")
8057 (looking-at "[a-zA-Z0-9`_\$]+")
8058 (setq param-name (buffer-substring-no-properties
8059 (match-beginning 0) (match-end 0)))
8060 (verilog-re-search-forward-quick "(" nil nil)
8061 (setq paren-beg-pt (point))
8062 (verilog-forward-close-paren)
8063 (setq param-value (verilog-string-remove-spaces
8064 (buffer-substring-no-properties
8065 paren-beg-pt (1- (point)))))
8066 (setq params (cons (list param-name param-value) params)))
8067 params))))
8068
8069 (defun verilog-read-auto-params (num-param &optional max-param)
8070 "Return parameter list inside auto.
8071 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
8072 (let ((olist))
8073 (save-excursion
8074 ;; /*AUTOPUNT("parameter", "parameter")*/
8075 (backward-sexp 1)
8076 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
8077 (setq olist (cons (match-string 1) olist))
8078 (goto-char (match-end 0))))
8079 (or (eq nil num-param)
8080 (<= num-param (length olist))
8081 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
8082 (if (eq max-param nil) (setq max-param num-param))
8083 (or (eq nil max-param)
8084 (>= max-param (length olist))
8085 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
8086 (nreverse olist)))
8087
8088 (defun verilog-read-decls ()
8089 "Compute signal declaration information for the current module at point.
8090 Return an array of [outputs inouts inputs wire reg assign const]."
8091 (let ((end-mod-point (or (verilog-get-end-of-defun) (point-max)))
8092 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
8093 in-modport in-clocking in-ign-to-semi ptype ign-prop
8094 sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
8095 sigs-gparam sigs-intf sigs-modports
8096 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
8097 modport
8098 varstack tmp)
8099 ;;(if dbg (setq dbg (concat dbg (format "\n\nverilog-read-decls START PT %s END %s\n" (point) end-mod-point))))
8100 (save-excursion
8101 (verilog-beg-of-defun-quick)
8102 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
8103 (while (< (point) end-mod-point)
8104 ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
8105 (cond
8106 ((looking-at "//")
8107 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8108 (setq enum (match-string 2)))
8109 (search-forward "\n"))
8110 ((looking-at "/\\*")
8111 (forward-char 2)
8112 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8113 (setq enum (match-string 2)))
8114 (or (search-forward "*/")
8115 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8116 ((looking-at "(\\*")
8117 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8118 (forward-char 1)
8119 (or (search-forward "*)")
8120 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8121 ((eq ?\" (following-char))
8122 (or (re-search-forward "[^\\]\"" nil t) ;; don't forward-char first, since we look for a non backslash first
8123 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8124 ((eq ?\; (following-char))
8125 (cond (in-ign-to-semi ;; Such as inside a "import ...;" in a module header
8126 (setq in-ign-to-semi nil))
8127 ((and in-modport (not (eq in-modport t))) ;; end of a modport declaration
8128 (verilog-modport-decls-set
8129 in-modport
8130 (verilog-decls-new sigs-out sigs-inout sigs-in
8131 nil nil nil nil nil nil))
8132 ;; Pop from varstack to restore state to pre-clocking
8133 (setq tmp (car varstack)
8134 varstack (cdr varstack)
8135 sigs-out (aref tmp 0)
8136 sigs-inout (aref tmp 1)
8137 sigs-in (aref tmp 2))
8138 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8139 v2kargs-ok nil in-modport nil ign-prop nil))
8140 (t
8141 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8142 v2kargs-ok nil in-modport nil ign-prop nil)))
8143 (forward-char 1))
8144 ((eq ?= (following-char))
8145 (setq rvalue t newsig nil)
8146 (forward-char 1))
8147 ((and (eq ?, (following-char))
8148 (eq paren sig-paren))
8149 (setq rvalue nil)
8150 (forward-char 1))
8151 ;; ,'s can occur inside {} & funcs
8152 ((looking-at "[{(]")
8153 (setq paren (1+ paren))
8154 (forward-char 1))
8155 ((looking-at "[})]")
8156 (setq paren (1- paren))
8157 (forward-char 1)
8158 (when (< paren sig-paren)
8159 (setq expect-signal nil rvalue nil))) ; ) that ends variables inside v2k arg list
8160 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
8161 (goto-char (match-end 0))
8162 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
8163 (setcar (cdr (cdr (cdr newsig)))
8164 (if (verilog-sig-memory newsig)
8165 (concat (verilog-sig-memory newsig) (match-string 1))
8166 (match-string 1))))
8167 (vec ;; Multidimensional
8168 (setq multidim (cons vec multidim))
8169 (setq vec (verilog-string-replace-matches
8170 "\\s-+" "" nil nil (match-string 1))))
8171 (t ;; Bit width
8172 (setq vec (verilog-string-replace-matches
8173 "\\s-+" "" nil nil (match-string 1))))))
8174 ;; Normal or escaped identifier -- note we remember the \ if escaped
8175 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8176 (goto-char (match-end 0))
8177 (setq keywd (match-string 1))
8178 (when (string-match "^\\\\" (match-string 1))
8179 (setq keywd (concat keywd " "))) ;; Escaped ID needs space at end
8180 ;; Add any :: package names to same identifier
8181 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8182 (goto-char (match-end 0))
8183 (setq keywd (concat keywd "::" (match-string 1)))
8184 (when (string-match "^\\\\" (match-string 1))
8185 (setq keywd (concat keywd " ")))) ;; Escaped ID needs space at end
8186 (cond ((equal keywd "input")
8187 (setq vec nil enum nil rvalue nil newsig nil signed nil
8188 typedefed nil multidim nil ptype nil modport nil
8189 expect-signal 'sigs-in io t sig-paren paren))
8190 ((equal keywd "output")
8191 (setq vec nil enum nil rvalue nil newsig nil signed nil
8192 typedefed nil multidim nil ptype nil modport nil
8193 expect-signal 'sigs-out io t sig-paren paren))
8194 ((equal keywd "inout")
8195 (setq vec nil enum nil rvalue nil newsig nil signed nil
8196 typedefed nil multidim nil ptype nil modport nil
8197 expect-signal 'sigs-inout io t sig-paren paren))
8198 ((equal keywd "parameter")
8199 (setq vec nil enum nil rvalue nil signed nil
8200 typedefed nil multidim nil ptype nil modport nil
8201 expect-signal 'sigs-gparam io t sig-paren paren))
8202 ((member keywd '("wire" "reg" ; Fast
8203 ;; net_type
8204 "tri" "tri0" "tri1" "triand" "trior" "trireg"
8205 "uwire" "wand" "wor"
8206 ;; integer_atom_type
8207 "byte" "shortint" "int" "longint" "integer" "time"
8208 "supply0" "supply1"
8209 ;; integer_vector_type - "reg" above
8210 "bit" "logic"
8211 ;; non_integer_type
8212 "shortreal" "real" "realtime"
8213 ;; data_type
8214 "string" "event" "chandle"))
8215 (cond (io
8216 (setq typedefed
8217 (if typedefed (concat typedefed " " keywd) keywd)))
8218 (t (setq vec nil enum nil rvalue nil signed nil
8219 typedefed nil multidim nil sig-paren paren
8220 expect-signal 'sigs-var modport nil))))
8221 ((equal keywd "assign")
8222 (setq vec nil enum nil rvalue nil signed nil
8223 typedefed nil multidim nil ptype nil modport nil
8224 expect-signal 'sigs-assign sig-paren paren))
8225 ((member keywd '("localparam" "genvar"))
8226 (unless io
8227 (setq vec nil enum nil rvalue nil signed nil
8228 typedefed nil multidim nil ptype nil modport nil
8229 expect-signal 'sigs-const sig-paren paren)))
8230 ((member keywd '("signed" "unsigned"))
8231 (setq signed keywd))
8232 ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
8233 (setq ign-prop t))
8234 ((member keywd '("class" "covergroup" "function"
8235 "property" "randsequence" "sequence" "task"))
8236 (unless ign-prop
8237 (setq functask (1+ functask))))
8238 ((member keywd '("endclass" "endgroup" "endfunction"
8239 "endproperty" "endsequence" "endtask"))
8240 (setq functask (1- functask)))
8241 ((equal keywd "modport")
8242 (setq in-modport t))
8243 ((equal keywd "clocking")
8244 (setq in-clocking t))
8245 ((equal keywd "import")
8246 (if v2kargs-ok ;; import in module header, not a modport import
8247 (setq in-ign-to-semi t rvalue t)))
8248 ((equal keywd "type")
8249 (setq ptype t))
8250 ((equal keywd "var"))
8251 ;; Ifdef? Ignore name of define
8252 ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8253 (setq rvalue t))
8254 ;; Type?
8255 ((unless ptype
8256 (verilog-typedef-name-p keywd))
8257 (cond (io
8258 (setq typedefed
8259 (if typedefed (concat typedefed " " keywd) keywd)))
8260 (t (setq vec nil enum nil rvalue nil signed nil
8261 typedefed nil multidim nil sig-paren paren
8262 expect-signal 'sigs-var modport nil))))
8263 ;; Interface with optional modport in v2k arglist?
8264 ;; Skip over parsing modport, and take the interface name as the type
8265 ((and v2kargs-ok
8266 (eq paren 1)
8267 (not rvalue)
8268 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
8269 (when (match-end 2) (goto-char (match-end 2)))
8270 (setq vec nil enum nil rvalue nil signed nil
8271 typedefed keywd multidim nil ptype nil modport (match-string 2)
8272 newsig nil sig-paren paren
8273 expect-signal 'sigs-intf io t ))
8274 ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
8275 ((looking-at "\\s-*\\.")
8276 (goto-char (match-end 0))
8277 (when (not rvalue)
8278 (setq expect-signal nil)))
8279 ;; "modport <keywd>"
8280 ((and (eq in-modport t)
8281 (not (member keywd verilog-keywords)))
8282 (setq in-modport (verilog-modport-new keywd nil nil))
8283 (setq sigs-modports (cons in-modport sigs-modports))
8284 ;; Push old sig values to stack and point to new signal list
8285 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8286 varstack))
8287 (setq sigs-in nil sigs-inout nil sigs-out nil))
8288 ;; "modport x (clocking <keywd>)"
8289 ((and in-modport in-clocking)
8290 (verilog-modport-clockings-add in-modport keywd)
8291 (setq in-clocking nil))
8292 ;; endclocking
8293 ((and in-clocking
8294 (equal keywd "endclocking"))
8295 (unless (eq in-clocking t)
8296 (verilog-modport-decls-set
8297 in-clocking
8298 (verilog-decls-new sigs-out sigs-inout sigs-in
8299 nil nil nil nil nil nil))
8300 ;; Pop from varstack to restore state to pre-clocking
8301 (setq tmp (car varstack)
8302 varstack (cdr varstack)
8303 sigs-out (aref tmp 0)
8304 sigs-inout (aref tmp 1)
8305 sigs-in (aref tmp 2)))
8306 (setq in-clocking nil))
8307 ;; "clocking <keywd>"
8308 ((and (eq in-clocking t)
8309 (not (member keywd verilog-keywords)))
8310 (setq in-clocking (verilog-modport-new keywd nil nil))
8311 (setq sigs-modports (cons in-clocking sigs-modports))
8312 ;; Push old sig values to stack and point to new signal list
8313 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8314 varstack))
8315 (setq sigs-in nil sigs-inout nil sigs-out nil))
8316 ;; New signal, maybe?
8317 ((and expect-signal
8318 (not rvalue)
8319 (eq functask 0)
8320 (not (member keywd verilog-keywords)))
8321 ;; Add new signal to expect-signal's variable
8322 ;;(if dbg (setq dbg (concat dbg (format "Pt %s New sig %s'\n" (point) keywd))))
8323 (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
8324 (set expect-signal (cons newsig
8325 (symbol-value expect-signal))))))
8326 (t
8327 (forward-char 1)))
8328 (skip-syntax-forward " "))
8329 ;; Return arguments
8330 (setq tmp (verilog-decls-new (nreverse sigs-out)
8331 (nreverse sigs-inout)
8332 (nreverse sigs-in)
8333 (nreverse sigs-var)
8334 (nreverse sigs-modports)
8335 (nreverse sigs-assign)
8336 (nreverse sigs-const)
8337 (nreverse sigs-gparam)
8338 (nreverse sigs-intf)))
8339 ;;(if dbg (verilog-decls-princ tmp))
8340 tmp)))
8341
8342 (defvar verilog-read-sub-decls-in-interfaced nil
8343 "For `verilog-read-sub-decls', process next signal as under interfaced block.")
8344
8345 (defvar verilog-read-sub-decls-gate-ios nil
8346 "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
8347
8348 (eval-when-compile
8349 ;; Prevent compile warnings; these are let's, not globals
8350 ;; Do not remove the eval-when-compile
8351 ;; - we want an error when we are debugging this code if they are refed.
8352 (defvar sigs-in)
8353 (defvar sigs-inout)
8354 (defvar sigs-intf)
8355 (defvar sigs-intfd)
8356 (defvar sigs-out)
8357 (defvar sigs-out-d)
8358 (defvar sigs-out-i)
8359 (defvar sigs-out-unk)
8360 (defvar sigs-temp)
8361 ;; These are known to be from other packages and may not be defined
8362 (defvar diff-command nil)
8363 (defvar vector-skip-list)
8364 ;; There are known to be from newer versions of Emacs
8365 (defvar create-lockfiles))
8366
8367 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
8368 "For `verilog-read-sub-decls-line', add a signal."
8369 ;; sig eq t to indicate .name syntax
8370 ;;(message "vrsds: %s(%S)" port sig)
8371 (let ((dotname (eq sig t))
8372 portdata)
8373 (when sig
8374 (setq port (verilog-symbol-detick-denumber port))
8375 (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
8376 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
8377 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
8378 (unless (or (not sig)
8379 (equal sig "")) ;; Ignore .foo(1'b1) assignments
8380 (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
8381 (equal "inout" verilog-read-sub-decls-gate-ios))
8382 (setq sigs-inout
8383 (cons (verilog-sig-new
8384 sig
8385 (if dotname (verilog-sig-bits portdata) vec)
8386 (concat "To/From " comment)
8387 (verilog-sig-memory portdata)
8388 nil
8389 (verilog-sig-signed portdata)
8390 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8391 (verilog-sig-type portdata))
8392 multidim nil)
8393 sigs-inout)))
8394 ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
8395 (equal "output" verilog-read-sub-decls-gate-ios))
8396 (setq sigs-out
8397 (cons (verilog-sig-new
8398 sig
8399 (if dotname (verilog-sig-bits portdata) vec)
8400 (concat "From " comment)
8401 (verilog-sig-memory portdata)
8402 nil
8403 (verilog-sig-signed portdata)
8404 ;; Though ok in SV, in V2K code, propagating the
8405 ;; "reg" in "output reg" upwards isn't legal.
8406 ;; Also for backwards compatibility we don't propagate
8407 ;; "input wire" upwards.
8408 ;; See also `verilog-signals-edit-wire-reg'.
8409 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8410 (verilog-sig-type portdata))
8411 multidim nil)
8412 sigs-out)))
8413 ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
8414 (equal "input" verilog-read-sub-decls-gate-ios))
8415 (setq sigs-in
8416 (cons (verilog-sig-new
8417 sig
8418 (if dotname (verilog-sig-bits portdata) vec)
8419 (concat "To " comment)
8420 (verilog-sig-memory portdata)
8421 nil
8422 (verilog-sig-signed portdata)
8423 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8424 (verilog-sig-type portdata))
8425 multidim nil)
8426 sigs-in)))
8427 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
8428 (setq sigs-intf
8429 (cons (verilog-sig-new
8430 sig
8431 (if dotname (verilog-sig-bits portdata) vec)
8432 (concat "To/From " comment)
8433 (verilog-sig-memory portdata)
8434 nil
8435 (verilog-sig-signed portdata)
8436 (verilog-sig-type portdata)
8437 multidim nil)
8438 sigs-intf)))
8439 ((setq portdata (and verilog-read-sub-decls-in-interfaced
8440 (assoc port (verilog-decls-get-vars submoddecls))))
8441 (setq sigs-intfd
8442 (cons (verilog-sig-new
8443 sig
8444 (if dotname (verilog-sig-bits portdata) vec)
8445 (concat "To/From " comment)
8446 (verilog-sig-memory portdata)
8447 nil
8448 (verilog-sig-signed portdata)
8449 (verilog-sig-type portdata)
8450 multidim nil)
8451 sigs-intf)))
8452 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
8453 )))))
8454
8455 (defun verilog-read-sub-decls-expr (submoddecls comment port expr)
8456 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
8457 ;;(message "vrsde: '%s'" expr)
8458 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
8459 (setq expr (verilog-string-replace-matches "/\\*\\(\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
8460 ;; Remove front operators
8461 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8462 ;;
8463 (cond
8464 ;; {..., a, b} requires us to recurse on a,b
8465 ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
8466 ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
8467 (unless verilog-auto-ignore-concat
8468 (let ((mlst (split-string (match-string 1 expr) "[{},]"))
8469 mstr)
8470 (while (setq mstr (pop mlst))
8471 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
8472 (t
8473 (let (sig vec multidim)
8474 ;; Remove leading reduction operators, etc
8475 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8476 ;;(message "vrsde-ptop: '%s'" expr)
8477 (cond ;; Find \signal. Final space is part of escaped signal name
8478 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
8479 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8480 (setq sig (match-string 1 expr)
8481 expr (substring expr (match-end 0))))
8482 ;; Find signal
8483 ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
8484 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8485 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
8486 expr (substring expr (match-end 0)))))
8487 ;; Find [vector] or [multi][multi][multi][vector]
8488 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
8489 ;;(message "vrsde-v: '%s'" (match-string 1 expr))
8490 (when vec (setq multidim (cons vec multidim)))
8491 (setq vec (match-string 1 expr)
8492 expr (substring expr (match-end 0))))
8493 ;; If found signal, and nothing unrecognized, add the signal
8494 ;;(message "vrsde-rem: '%s'" expr)
8495 (when (and sig (string-match "^\\s-*$" expr))
8496 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))))
8497
8498 (defun verilog-read-sub-decls-line (submoddecls comment)
8499 "For `verilog-read-sub-decls', read lines of port defs until none match.
8500 Inserts the list of signals found, using submodi to look up each port."
8501 (let (done port)
8502 (save-excursion
8503 (forward-line 1)
8504 (while (not done)
8505 ;; Get port name
8506 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
8507 (setq port (match-string 1))
8508 (goto-char (match-end 0)))
8509 ;; .\escaped (
8510 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
8511 (setq port (concat (match-string 1) " ")) ;; escaped id's need trailing space
8512 (goto-char (match-end 0)))
8513 ;; .name
8514 ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
8515 (verilog-read-sub-decls-sig
8516 submoddecls comment (match-string 1) t ; sig==t for .name
8517 nil nil) ; vec multidim
8518 (setq port nil))
8519 ;; .\escaped_name
8520 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
8521 (verilog-read-sub-decls-sig
8522 submoddecls comment (concat (match-string 1) " ") t ; sig==t for .name
8523 nil nil) ; vec multidim
8524 (setq port nil))
8525 ;; random
8526 ((looking-at "\\s-*\\.[^(]*(")
8527 (setq port nil) ;; skip this line
8528 (goto-char (match-end 0)))
8529 (t
8530 (setq port nil done t))) ;; Unknown, ignore rest of line
8531 ;; Get signal name. Point is at the first-non-space after (
8532 ;; We intentionally ignore (non-escaped) signals with .s in them
8533 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
8534 (when port
8535 (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
8536 (verilog-read-sub-decls-sig
8537 submoddecls comment port
8538 (verilog-string-remove-spaces (match-string 1)) ; sig
8539 nil nil)) ; vec multidim
8540 ;;
8541 ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
8542 (verilog-read-sub-decls-sig
8543 submoddecls comment port
8544 (verilog-string-remove-spaces (match-string 1)) ; sig
8545 (match-string 2) nil)) ; vec multidim
8546 ;; Fastpath was above looking-at's.
8547 ;; For something more complicated invoke a parser
8548 ((looking-at "[^)]+")
8549 (verilog-read-sub-decls-expr
8550 submoddecls comment port
8551 (buffer-substring
8552 (point) (1- (progn (search-backward "(") ; start at (
8553 (verilog-forward-sexp-ign-cmt 1)
8554 (point)))))))) ; expr
8555 ;;
8556 (forward-line 1)))))
8557
8558 (defun verilog-read-sub-decls-gate (submoddecls comment submod end-inst-point)
8559 "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
8560 Inserts the list of signals found."
8561 (save-excursion
8562 (let ((iolist (cdr (assoc submod verilog-gate-ios))))
8563 (while (< (point) end-inst-point)
8564 ;; Get primitive's signal name, as will never have port, and no trailing )
8565 (cond ((looking-at "//")
8566 (search-forward "\n"))
8567 ((looking-at "/\\*")
8568 (or (search-forward "*/")
8569 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8570 ((looking-at "(\\*")
8571 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8572 (forward-char 1)
8573 (or (search-forward "*)")
8574 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8575 ;; On pins, parse and advance to next pin
8576 ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
8577 ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
8578 (goto-char (match-end 0))
8579 (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
8580 iolist (cdr iolist))
8581 (verilog-read-sub-decls-expr
8582 submoddecls comment "primitive_port"
8583 (match-string 0)))
8584 (t
8585 (forward-char 1)
8586 (skip-syntax-forward " ")))))))
8587
8588 (defun verilog-read-sub-decls ()
8589 "Internally parse signals going to modules under this module.
8590 Return an array of [ outputs inouts inputs ] signals for modules that are
8591 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
8592 is an output, then SIG will be included in the list.
8593
8594 This only works on instantiations created with /*AUTOINST*/ converted by
8595 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
8596 component library to determine connectivity of the design.
8597
8598 One work around for this problem is to manually create // Inputs and //
8599 Outputs comments above subcell signals, for example:
8600
8601 module ModuleName (
8602 // Outputs
8603 .out (out),
8604 // Inputs
8605 .in (in));"
8606 (save-excursion
8607 (let ((end-mod-point (verilog-get-end-of-defun))
8608 st-point end-inst-point
8609 ;; below 3 modified by verilog-read-sub-decls-line
8610 sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
8611 (verilog-beg-of-defun-quick)
8612 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
8613 (save-excursion
8614 (goto-char (match-beginning 0))
8615 (unless (verilog-inside-comment-or-string-p)
8616 ;; Attempt to snarf a comment
8617 (let* ((submod (verilog-read-inst-module))
8618 (inst (verilog-read-inst-name))
8619 (subprim (member submod verilog-gate-keywords))
8620 (comment (concat inst " of " submod ".v"))
8621 submodi submoddecls)
8622 (cond
8623 (subprim
8624 (setq submodi `primitive
8625 submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
8626 comment (concat inst " of " submod))
8627 (verilog-backward-open-paren)
8628 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8629 (point))
8630 st-point (point))
8631 (forward-char 1)
8632 (verilog-read-sub-decls-gate submoddecls comment submod end-inst-point))
8633 ;; Non-primitive
8634 (t
8635 (when (setq submodi (verilog-modi-lookup submod t))
8636 (setq submoddecls (verilog-modi-get-decls submodi)
8637 verilog-read-sub-decls-gate-ios nil)
8638 (verilog-backward-open-paren)
8639 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8640 (point))
8641 st-point (point))
8642 ;; This could have used a list created by verilog-auto-inst
8643 ;; However I want it to be runnable even on user's manually added signals
8644 (let ((verilog-read-sub-decls-in-interfaced t))
8645 (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
8646 (verilog-read-sub-decls-line submoddecls comment))) ;; Modifies sigs-ifd
8647 (goto-char st-point)
8648 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
8649 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8650 (goto-char st-point)
8651 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
8652 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8653 (goto-char st-point)
8654 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
8655 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-inout
8656 (goto-char st-point)
8657 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
8658 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-in
8659 )))))))
8660 ;; Combine duplicate bits
8661 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
8662 (verilog-subdecls-new
8663 (verilog-signals-combine-bus (nreverse sigs-out))
8664 (verilog-signals-combine-bus (nreverse sigs-inout))
8665 (verilog-signals-combine-bus (nreverse sigs-in))
8666 (verilog-signals-combine-bus (nreverse sigs-intf))
8667 (verilog-signals-combine-bus (nreverse sigs-intfd))))))
8668
8669 (defun verilog-read-inst-pins ()
8670 "Return an array of [ pins ] for the current instantiation at point.
8671 For example if declare A A (.B(SIG)) then B will be included in the list."
8672 (save-excursion
8673 (let ((end-mod-point (point)) ;; presume at /*AUTOINST*/ point
8674 pins pin)
8675 (verilog-backward-open-paren)
8676 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
8677 (setq pin (match-string 1))
8678 (unless (verilog-inside-comment-or-string-p)
8679 (setq pins (cons (list pin) pins))
8680 (when (looking-at "(")
8681 (verilog-forward-sexp-ign-cmt 1))))
8682 (vector pins))))
8683
8684 (defun verilog-read-arg-pins ()
8685 "Return an array of [ pins ] for the current argument declaration at point."
8686 (save-excursion
8687 (let ((end-mod-point (point)) ;; presume at /*AUTOARG*/ point
8688 pins pin)
8689 (verilog-backward-open-paren)
8690 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
8691 (setq pin (match-string 1))
8692 (unless (verilog-inside-comment-or-string-p)
8693 (setq pins (cons (list pin) pins))))
8694 (vector pins))))
8695
8696 (defun verilog-read-auto-constants (beg end-mod-point)
8697 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
8698 ;; Insert new
8699 (save-excursion
8700 (let (sig-list tpl-end-pt)
8701 (goto-char beg)
8702 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
8703 (if (not (looking-at "\\s *("))
8704 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
8705 (search-forward "(" end-mod-point)
8706 (setq tpl-end-pt (save-excursion
8707 (backward-char 1)
8708 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
8709 (backward-char 1)
8710 (point)))
8711 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
8712 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
8713 sig-list)))
8714
8715 (defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
8716 (make-variable-buffer-local 'verilog-cache-has-lisp)
8717
8718 (defun verilog-read-auto-lisp-present ()
8719 "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
8720 (save-excursion
8721 (goto-char (point-min))
8722 (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
8723
8724 (defun verilog-read-auto-lisp (start end)
8725 "Look for and evaluate an AUTO_LISP between START and END.
8726 Must call `verilog-read-auto-lisp-present' before this function."
8727 ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
8728 (when verilog-cache-has-lisp
8729 (save-excursion
8730 (goto-char start)
8731 (while (re-search-forward "\\<AUTO_LISP(" end t)
8732 (backward-char)
8733 (let* ((beg-pt (prog1 (point)
8734 (verilog-forward-sexp-cmt 1))) ;; Closing paren
8735 (end-pt (point))
8736 (verilog-in-hooks t))
8737 (eval-region beg-pt end-pt nil))))))
8738
8739 (defun verilog-read-always-signals-recurse
8740 (exit-keywd rvalue temp-next)
8741 "Recursive routine for parentheses/bracket matching.
8742 EXIT-KEYWD is expression to stop at, nil if top level.
8743 RVALUE is true if at right hand side of equal.
8744 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
8745 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ;; true if after a ; we are looking for rvalue
8746 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
8747 ignore-next)
8748 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
8749 (while (not (or (eobp) gotend))
8750 (cond
8751 ((looking-at "//")
8752 (search-forward "\n"))
8753 ((looking-at "/\\*")
8754 (or (search-forward "*/")
8755 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8756 ((looking-at "(\\*")
8757 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8758 (forward-char 1)
8759 (or (search-forward "*)")
8760 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8761 (t (setq keywd (buffer-substring-no-properties
8762 (point)
8763 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8764 (forward-char 1))
8765 (point)))
8766 sig-last-tolk sig-tolk
8767 sig-tolk nil)
8768 ;;(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))))
8769 (cond
8770 ((equal keywd "\"")
8771 (or (re-search-forward "[^\\]\"" nil t)
8772 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8773 ;; else at top level loop, keep parsing
8774 ((and end-else-check (equal keywd "else"))
8775 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
8776 ;; no forward movement, want to see else in lower loop
8777 (setq end-else-check nil))
8778 ;; End at top level loop
8779 ((and end-else-check (looking-at "[^ \t\n\f]"))
8780 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
8781 (setq gotend t))
8782 ;; Final statement?
8783 ((and exit-keywd (equal keywd exit-keywd))
8784 (setq gotend t)
8785 (forward-char (length keywd)))
8786 ;; Standard tokens...
8787 ((equal keywd ";")
8788 (setq ignore-next nil rvalue semi-rvalue)
8789 ;; Final statement at top level loop?
8790 (when (not exit-keywd)
8791 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
8792 (setq end-else-check t))
8793 (forward-char 1))
8794 ((equal keywd "'")
8795 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
8796 (goto-char (match-end 0))
8797 (forward-char 1)))
8798 ((equal keywd ":") ;; Case statement, begin/end label, x?y:z
8799 (cond ((equal "endcase" exit-keywd) ;; case x: y=z; statement next
8800 (setq ignore-next nil rvalue nil))
8801 ((equal "?" exit-keywd) ;; x?y:z rvalue
8802 ) ;; NOP
8803 ((equal "]" exit-keywd) ;; [x:y] rvalue
8804 ) ;; NOP
8805 (got-sig ;; label: statement
8806 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
8807 ((not rvalue) ;; begin label
8808 (setq ignore-next t rvalue nil)))
8809 (forward-char 1))
8810 ((equal keywd "=")
8811 (when got-sig
8812 ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
8813 (set got-list (cons got-sig (symbol-value got-list)))
8814 (setq got-sig nil))
8815 (when (not rvalue)
8816 (if (eq (char-before) ?< )
8817 (setq sigs-out-d (append sigs-out-d sigs-out-unk)
8818 sigs-out-unk nil)
8819 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8820 sigs-out-unk nil)))
8821 (setq ignore-next nil rvalue t)
8822 (forward-char 1))
8823 ((equal keywd "?")
8824 (forward-char 1)
8825 (verilog-read-always-signals-recurse ":" rvalue nil))
8826 ((equal keywd "[")
8827 (forward-char 1)
8828 (verilog-read-always-signals-recurse "]" t nil))
8829 ((equal keywd "(")
8830 (forward-char 1)
8831 (cond (sig-last-tolk ;; Function call; zap last signal
8832 (setq got-sig nil)))
8833 (cond ((equal last-keywd "for")
8834 ;; temp-next: Variables on LHS are lvalues, but generally we want
8835 ;; to ignore them, assuming they are loop increments
8836 (verilog-read-always-signals-recurse ";" nil t)
8837 (verilog-read-always-signals-recurse ";" t nil)
8838 (verilog-read-always-signals-recurse ")" nil nil))
8839 (t (verilog-read-always-signals-recurse ")" t nil))))
8840 ((equal keywd "begin")
8841 (skip-syntax-forward "w_")
8842 (verilog-read-always-signals-recurse "end" nil nil)
8843 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
8844 (setq ignore-next nil rvalue semi-rvalue)
8845 (if (not exit-keywd) (setq end-else-check t)))
8846 ((member keywd '("case" "casex" "casez"))
8847 (skip-syntax-forward "w_")
8848 (verilog-read-always-signals-recurse "endcase" t nil)
8849 (setq ignore-next nil rvalue semi-rvalue)
8850 (if (not exit-keywd) (setq gotend t))) ;; top level begin/end
8851 ((string-match "^[$`a-zA-Z_]" keywd) ;; not exactly word constituent
8852 (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8853 (setq ignore-next t))
8854 ((or ignore-next
8855 (member keywd verilog-keywords)
8856 (string-match "^\\$" keywd)) ;; PLI task
8857 (setq ignore-next nil))
8858 (t
8859 (setq keywd (verilog-symbol-detick-denumber keywd))
8860 (when got-sig
8861 (set got-list (cons got-sig (symbol-value got-list)))
8862 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
8863 )
8864 (setq got-list (cond (temp-next 'sigs-temp)
8865 (rvalue 'sigs-in)
8866 (t 'sigs-out-unk))
8867 got-sig (if (or (not keywd)
8868 (assoc keywd (symbol-value got-list)))
8869 nil (list keywd nil nil))
8870 temp-next nil
8871 sig-tolk t)))
8872 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8873 (t
8874 (forward-char 1)))
8875 ;; End of non-comment token
8876 (setq last-keywd keywd)))
8877 (skip-syntax-forward " "))
8878 ;; Append the final pending signal
8879 (when got-sig
8880 ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
8881 (set got-list (cons got-sig (symbol-value got-list)))
8882 (setq got-sig nil))
8883 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
8884 ))
8885
8886 (defun verilog-read-always-signals ()
8887 "Parse always block at point and return list of (outputs inout inputs)."
8888 (save-excursion
8889 (let* (;;(dbg "")
8890 sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
8891 (search-forward ")")
8892 (verilog-read-always-signals-recurse nil nil nil)
8893 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8894 sigs-out-unk nil)
8895 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg ""))
8896 ;; Return what was found
8897 (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
8898
8899 (defun verilog-read-instants ()
8900 "Parse module at point and return list of ( ( file instance ) ... )."
8901 (verilog-beg-of-defun-quick)
8902 (let* ((end-mod-point (verilog-get-end-of-defun))
8903 (state nil)
8904 (instants-list nil))
8905 (save-excursion
8906 (while (< (point) end-mod-point)
8907 ;; Stay at level 0, no comments
8908 (while (progn
8909 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
8910 (or (> (car state) 0) ; in parens
8911 (nth 5 state) ; comment
8912 ))
8913 (forward-line 1))
8914 (beginning-of-line)
8915 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
8916 ;;(if (looking-at "^\\(.+\\)$")
8917 (let ((module (match-string 1))
8918 (instant (match-string 2)))
8919 (if (not (member module verilog-keywords))
8920 (setq instants-list (cons (list module instant) instants-list)))))
8921 (forward-line 1)))
8922 instants-list))
8923
8924
8925 (defun verilog-read-auto-template-middle ()
8926 "With point in middle of an AUTO_TEMPLATE, parse it.
8927 Returns REGEXP and list of ( (signal_name connection_name)... )."
8928 (save-excursion
8929 ;; Find beginning
8930 (let ((tpl-regexp "\\([0-9]+\\)")
8931 (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
8932 (templateno 0)
8933 tpl-sig-list tpl-wild-list tpl-end-pt rep)
8934 ;; Parse "REGEXP"
8935 ;; We reserve @"..." for future lisp expressions that evaluate
8936 ;; once-per-AUTOINST
8937 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
8938 (setq tpl-regexp (match-string 1))
8939 (goto-char (match-end 0)))
8940 (search-forward "(")
8941 ;; Parse lines in the template
8942 (when (or verilog-auto-inst-template-numbers
8943 verilog-auto-template-warn-unused)
8944 (save-excursion
8945 (let ((pre-pt (point)))
8946 (goto-char (point-min))
8947 (while (search-forward "AUTO_TEMPLATE" pre-pt t)
8948 (setq templateno (1+ templateno)))
8949 (while (< (point) pre-pt)
8950 (forward-line 1)
8951 (setq lineno (1+ lineno))))))
8952 (setq tpl-end-pt (save-excursion
8953 (backward-char 1)
8954 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
8955 (backward-char 1)
8956 (point)))
8957 ;;
8958 (while (< (point) tpl-end-pt)
8959 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8960 (setq tpl-sig-list
8961 (cons (list
8962 (match-string-no-properties 1)
8963 (match-string-no-properties 2)
8964 templateno lineno)
8965 tpl-sig-list))
8966 (goto-char (match-end 0)))
8967 ;; Regexp form??
8968 ((looking-at
8969 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
8970 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8971 (setq rep (match-string-no-properties 3))
8972 (goto-char (match-end 0))
8973 (setq tpl-wild-list
8974 (cons (list
8975 (concat "^"
8976 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
8977 (match-string 1))
8978 "$")
8979 rep
8980 templateno lineno)
8981 tpl-wild-list)))
8982 ((looking-at "[ \t\f]+")
8983 (goto-char (match-end 0)))
8984 ((looking-at "\n")
8985 (setq lineno (1+ lineno))
8986 (goto-char (match-end 0)))
8987 ((looking-at "//")
8988 (search-forward "\n")
8989 (setq lineno (1+ lineno)))
8990 ((looking-at "/\\*")
8991 (forward-char 2)
8992 (or (search-forward "*/")
8993 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8994 (t
8995 (error "%s: AUTO_TEMPLATE parsing error: %s"
8996 (verilog-point-text)
8997 (progn (looking-at ".*$") (match-string 0))))))
8998 ;; Return
8999 (vector tpl-regexp
9000 (list tpl-sig-list tpl-wild-list)))))
9001
9002 (defun verilog-read-auto-template (module)
9003 "Look for an auto_template for the instantiation of the given MODULE.
9004 If found returns `verilog-read-auto-template-inside' structure."
9005 (save-excursion
9006 ;; Find beginning
9007 (let ((pt (point)))
9008 ;; Note this search is expensive, as we hunt from mod-begin to point
9009 ;; for every instantiation. Likewise in verilog-read-auto-lisp.
9010 ;; So, we look first for an exact string rather than a slow regexp.
9011 ;; Someday we may keep a cache of every template, but this would also
9012 ;; need to record the relative position of each AUTOINST, as multiple
9013 ;; templates exist for each module, and we're inserting lines.
9014 (cond ((or
9015 ;; See also regexp in `verilog-auto-template-lint'
9016 (verilog-re-search-backward-substr
9017 "AUTO_TEMPLATE"
9018 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
9019 ;; Also try forward of this AUTOINST
9020 ;; This is for historical support; this isn't speced as working
9021 (progn
9022 (goto-char pt)
9023 (verilog-re-search-forward-substr
9024 "AUTO_TEMPLATE"
9025 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
9026 (goto-char (match-end 0))
9027 (verilog-read-auto-template-middle))
9028 ;; If no template found
9029 (t (vector "" nil))))))
9030 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
9031
9032 (defvar verilog-auto-template-hits nil "Successful lookups with `verilog-read-auto-template-hit'.")
9033 (make-variable-buffer-local 'verilog-auto-template-hits)
9034
9035 (defun verilog-read-auto-template-hit (tpl-ass)
9036 "Record that TPL-ASS template from `verilog-read-auto-template' was used."
9037 (when (eval-when-compile (fboundp 'make-hash-table)) ;; else feature not allowed
9038 (when verilog-auto-template-warn-unused
9039 (unless verilog-auto-template-hits
9040 (setq verilog-auto-template-hits
9041 (make-hash-table :test 'equal :rehash-size 4.0)))
9042 (puthash (vector (nth 2 tpl-ass) (nth 3 tpl-ass)) t
9043 verilog-auto-template-hits))))
9044
9045 (defun verilog-set-define (defname defvalue &optional buffer enumname)
9046 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
9047 Optionally associate it with the specified enumeration ENUMNAME."
9048 (with-current-buffer (or buffer (current-buffer))
9049 ;; Namespace intentionally short for AUTOs and compatibility
9050 (let ((mac (intern (concat "vh-" defname))))
9051 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
9052 ;; Need to define to a constant if no value given
9053 (set (make-local-variable mac)
9054 (if (equal defvalue "") "1" defvalue)))
9055 (if enumname
9056 ;; Namespace intentionally short for AUTOs and compatibility
9057 (let ((enumvar (intern (concat "venum-" enumname))))
9058 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
9059 (unless (boundp enumvar) (set enumvar nil))
9060 (add-to-list (make-local-variable enumvar) defname)))))
9061
9062 (defun verilog-read-defines (&optional filename recurse subcall)
9063 "Read `defines and parameters for the current file, or optional FILENAME.
9064 If the filename is provided, `verilog-library-flags' will be used to
9065 resolve it. If optional RECURSE is non-nil, recurse through `includes.
9066
9067 Parameters must be simple assignments to constants, or have their own
9068 \"parameter\" label rather than a list of parameters. Thus:
9069
9070 parameter X = 5, Y = 10; // Ok
9071 parameter X = {1'b1, 2'h2}; // Ok
9072 parameter X = {1'b1, 2'h2}, Y = 10; // Bad, make into 2 parameter lines
9073
9074 Defines must be simple text substitutions, one on a line, starting
9075 at the beginning of the line. Any ifdefs or multiline comments around the
9076 define are ignored.
9077
9078 Defines are stored inside Emacs variables using the name vh-{definename}.
9079
9080 This function is useful for setting vh-* variables. The file variables
9081 feature can be used to set defines that `verilog-mode' can see; put at the
9082 *END* of your file something like:
9083
9084 // Local Variables:
9085 // vh-macro:\"macro_definition\"
9086 // End:
9087
9088 If macros are defined earlier in the same file and you want their values,
9089 you can read them automatically (provided `enable-local-eval' is on):
9090
9091 // Local Variables:
9092 // eval:(verilog-read-defines)
9093 // eval:(verilog-read-defines \"group_standard_includes.v\")
9094 // End:
9095
9096 Note these are only read when the file is first visited, you must use
9097 \\[find-alternate-file] RET to have these take effect after editing them!
9098
9099 If you want to disable the \"Process `eval' or hook local variables\"
9100 warning message, you need to add to your init file:
9101
9102 (setq enable-local-eval t)"
9103 (let ((origbuf (current-buffer)))
9104 (save-excursion
9105 (unless subcall (verilog-getopt-flags))
9106 (when filename
9107 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
9108 (if fns
9109 (set-buffer (find-file-noselect (car fns)))
9110 (error (concat (verilog-point-text)
9111 ": Can't find verilog-read-defines file: " filename)))))
9112 (when recurse
9113 (goto-char (point-min))
9114 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9115 (let ((inc (verilog-string-replace-matches
9116 "\"" "" nil nil (match-string-no-properties 1))))
9117 (unless (verilog-inside-comment-or-string-p)
9118 (verilog-read-defines inc recurse t)))))
9119 ;; Read `defines
9120 ;; note we don't use verilog-re... it's faster this way, and that
9121 ;; function has problems when comments are at the end of the define
9122 (goto-char (point-min))
9123 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
9124 (let ((defname (match-string-no-properties 1))
9125 (defvalue (match-string-no-properties 2)))
9126 (unless (verilog-inside-comment-or-string-p (match-beginning 0))
9127 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
9128 (verilog-set-define defname defvalue origbuf))))
9129 ;; Hack: Read parameters
9130 (goto-char (point-min))
9131 (while (re-search-forward
9132 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
9133 (let (enumname)
9134 ;; The primary way of getting defines is verilog-read-decls
9135 ;; However, that isn't called yet for included files, so we'll add another scheme
9136 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
9137 (setq enumname (match-string-no-properties 2)))
9138 (forward-comment 99999)
9139 (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
9140 "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
9141 (unless (verilog-inside-comment-or-string-p (match-beginning 0))
9142 (verilog-set-define (match-string-no-properties 1)
9143 (match-string-no-properties 2) origbuf enumname))
9144 (goto-char (match-end 0))
9145 (forward-comment 99999)))))))
9146
9147 (defun verilog-read-includes ()
9148 "Read `includes for the current file.
9149 This will find all of the `includes which are at the beginning of lines,
9150 ignoring any ifdefs or multiline comments around them.
9151 `verilog-read-defines' is then performed on the current and each included
9152 file.
9153
9154 It is often useful put at the *END* of your file something like:
9155
9156 // Local Variables:
9157 // eval:(verilog-read-defines)
9158 // eval:(verilog-read-includes)
9159 // End:
9160
9161 Note includes are only read when the file is first visited, you must use
9162 \\[find-alternate-file] RET to have these take effect after editing them!
9163
9164 It is good to get in the habit of including all needed files in each .v
9165 file that needs it, rather than waiting for compile time. This will aid
9166 this process, Verilint, and readability. To prevent defining the same
9167 variable over and over when many modules are compiled together, put a test
9168 around the inside each include file:
9169
9170 foo.v (an include file):
9171 `ifdef _FOO_V // include if not already included
9172 `else
9173 `define _FOO_V
9174 ... contents of file
9175 `endif // _FOO_V"
9176 ;;slow: (verilog-read-defines nil t)
9177 (save-excursion
9178 (verilog-getopt-flags)
9179 (goto-char (point-min))
9180 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9181 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
9182 (verilog-read-defines inc nil t)))))
9183
9184 (defun verilog-read-signals (&optional start end)
9185 "Return a simple list of all possible signals in the file.
9186 Bounded by optional region from START to END. Overly aggressive but fast.
9187 Some macros and such are also found and included. For dinotrace.el."
9188 (let (sigs-all keywd)
9189 (progn;save-excursion
9190 (goto-char (or start (point-min)))
9191 (setq end (or end (point-max)))
9192 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
9193 (forward-char -1)
9194 (cond
9195 ((looking-at "//")
9196 (search-forward "\n"))
9197 ((looking-at "/\\*")
9198 (search-forward "*/"))
9199 ((looking-at "(\\*")
9200 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
9201 (search-forward "*)")))
9202 ((eq ?\" (following-char))
9203 (re-search-forward "[^\\]\"")) ;; don't forward-char first, since we look for a non backslash first
9204 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
9205 (goto-char (match-end 0))
9206 (setq keywd (match-string-no-properties 1))
9207 (or (member keywd verilog-keywords)
9208 (member keywd sigs-all)
9209 (setq sigs-all (cons keywd sigs-all))))
9210 (t (forward-char 1))))
9211 ;; Return list
9212 sigs-all)))
9213
9214 ;;
9215 ;; Argument file parsing
9216 ;;
9217
9218 (defun verilog-getopt (arglist)
9219 "Parse -f, -v etc arguments in ARGLIST list or string."
9220 (unless (listp arglist) (setq arglist (list arglist)))
9221 (let ((space-args '())
9222 arg next-param)
9223 ;; Split on spaces, so users can pass whole command lines
9224 (while arglist
9225 (setq arg (car arglist)
9226 arglist (cdr arglist))
9227 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
9228 (setq space-args (append space-args
9229 (list (match-string-no-properties 1 arg))))
9230 (setq arg (match-string 2 arg))))
9231 ;; Parse arguments
9232 (while space-args
9233 (setq arg (car space-args)
9234 space-args (cdr space-args))
9235 (cond
9236 ;; Need another arg
9237 ((equal arg "-f")
9238 (setq next-param arg))
9239 ((equal arg "-v")
9240 (setq next-param arg))
9241 ((equal arg "-y")
9242 (setq next-param arg))
9243 ;; +libext+(ext1)+(ext2)...
9244 ((string-match "^\\+libext\\+\\(.*\\)" arg)
9245 (setq arg (match-string 1 arg))
9246 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
9247 (verilog-add-list-unique `verilog-library-extensions
9248 (match-string 1 arg))
9249 (setq arg (match-string 2 arg))))
9250 ;;
9251 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; -Ddefine=val
9252 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ;; -Ddefine
9253 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; +define+val
9254 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ;; +define+define
9255 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
9256 ;;
9257 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ;; +incdir+dir
9258 (string-match "^-I\\(.*\\)" arg)) ;; -Idir
9259 (verilog-add-list-unique `verilog-library-directories
9260 (match-string 1 (substitute-in-file-name arg))))
9261 ;; Ignore
9262 ((equal "+librescan" arg))
9263 ((string-match "^-U\\(.*\\)" arg)) ;; -Udefine
9264 ;; Second parameters
9265 ((equal next-param "-f")
9266 (setq next-param nil)
9267 (verilog-getopt-file (substitute-in-file-name arg)))
9268 ((equal next-param "-v")
9269 (setq next-param nil)
9270 (verilog-add-list-unique `verilog-library-files
9271 (substitute-in-file-name arg)))
9272 ((equal next-param "-y")
9273 (setq next-param nil)
9274 (verilog-add-list-unique `verilog-library-directories
9275 (substitute-in-file-name arg)))
9276 ;; Filename
9277 ((string-match "^[^-+]" arg)
9278 (verilog-add-list-unique `verilog-library-files
9279 (substitute-in-file-name arg)))
9280 ;; Default - ignore; no warning
9281 ))))
9282 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
9283
9284 (defun verilog-getopt-file (filename)
9285 "Read Verilog options from the specified FILENAME."
9286 (save-excursion
9287 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
9288 (orig-buffer (current-buffer))
9289 line)
9290 (if fns
9291 (set-buffer (find-file-noselect (car fns)))
9292 (error (concat (verilog-point-text)
9293 ": Can't find verilog-getopt-file -f file: " filename)))
9294 (goto-char (point-min))
9295 (while (not (eobp))
9296 (setq line (buffer-substring (point) (point-at-eol)))
9297 (forward-line 1)
9298 (when (string-match "//" line)
9299 (setq line (substring line 0 (match-beginning 0))))
9300 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
9301 (verilog-getopt line))))))
9302
9303 (defun verilog-getopt-flags ()
9304 "Convert `verilog-library-flags' into standard library variables."
9305 ;; If the flags are local, then all the outputs should be local also
9306 (when (local-variable-p `verilog-library-flags (current-buffer))
9307 (mapc 'make-local-variable '(verilog-library-extensions
9308 verilog-library-directories
9309 verilog-library-files
9310 verilog-library-flags)))
9311 ;; Allow user to customize
9312 (verilog-run-hooks 'verilog-before-getopt-flags-hook)
9313 ;; Process arguments
9314 (verilog-getopt verilog-library-flags)
9315 ;; Allow user to customize
9316 (verilog-run-hooks 'verilog-getopt-flags-hook))
9317
9318 (defun verilog-add-list-unique (varref object)
9319 "Append to VARREF list the given OBJECT,
9320 unless it is already a member of the variable's list."
9321 (unless (member object (symbol-value varref))
9322 (set varref (append (symbol-value varref) (list object))))
9323 varref)
9324 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
9325
9326 (defun verilog-current-flags ()
9327 "Convert `verilog-library-flags' and similar variables to command line.
9328 Used for __FLAGS__ in `verilog-expand-command'."
9329 (let ((cmd (mapconcat `concat verilog-library-flags " ")))
9330 (when (equal cmd "")
9331 (setq cmd (concat
9332 "+libext+" (mapconcat `concat verilog-library-extensions "+")
9333 (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
9334 verilog-library-directories "")
9335 (mapconcat (lambda (i) (concat " -v " i))
9336 verilog-library-files ""))))
9337 cmd))
9338 ;;(verilog-current-flags)
9339
9340 \f
9341 ;;
9342 ;; Cached directory support
9343 ;;
9344
9345 (defvar verilog-dir-cache-preserving nil
9346 "If true, the directory cache is enabled, and file system changes are ignored.
9347 See `verilog-dir-exists-p' and `verilog-dir-files'.")
9348
9349 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
9350 (defvar verilog-dir-cache-list nil
9351 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
9352 (defvar verilog-dir-cache-lib-filenames nil
9353 "Cached data for `verilog-library-filenames'.")
9354
9355 (defmacro verilog-preserve-dir-cache (&rest body)
9356 "Execute the BODY forms, allowing directory cache preservation within BODY.
9357 This means that changes inside BODY made to the file system will not be
9358 seen by the `verilog-dir-files' and related functions."
9359 `(let ((verilog-dir-cache-preserving (current-buffer))
9360 verilog-dir-cache-list
9361 verilog-dir-cache-lib-filenames)
9362 (progn ,@body)))
9363
9364 (defun verilog-dir-files (dirname)
9365 "Return all filenames in the DIRNAME directory.
9366 Relative paths depend on the `default-directory'.
9367 Results are cached if inside `verilog-preserve-dir-cache'."
9368 (unless verilog-dir-cache-preserving
9369 (setq verilog-dir-cache-list nil)) ;; Cache disabled
9370 ;; We don't use expand-file-name on the dirname to make key, as it's slow
9371 (let* ((cache-key (list dirname default-directory))
9372 (fass (assoc cache-key verilog-dir-cache-list))
9373 exp-dirname data)
9374 (cond (fass ;; Return data from cache hit
9375 (nth 1 fass))
9376 (t
9377 (setq exp-dirname (expand-file-name dirname)
9378 data (and (file-directory-p exp-dirname)
9379 (directory-files exp-dirname nil nil nil)))
9380 ;; Note we also encache nil for non-existing dirs.
9381 (setq verilog-dir-cache-list (cons (list cache-key data)
9382 verilog-dir-cache-list))
9383 data))))
9384 ;; Miss-and-hit test:
9385 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
9386 ;; (prin1 (verilog-dir-files ".")) nil)
9387
9388 (defun verilog-dir-file-exists-p (filename)
9389 "Return true if FILENAME exists.
9390 Like `file-exists-p' but results are cached if inside
9391 `verilog-preserve-dir-cache'."
9392 (let* ((dirname (file-name-directory filename))
9393 ;; Correct for file-name-nondirectory returning same if no slash.
9394 (dirnamed (if (or (not dirname) (equal dirname filename))
9395 default-directory dirname))
9396 (flist (verilog-dir-files dirnamed)))
9397 (and flist
9398 (member (file-name-nondirectory filename) flist)
9399 t)))
9400 ;;(verilog-dir-file-exists-p "verilog-mode.el")
9401 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
9402
9403 \f
9404 ;;
9405 ;; Module name lookup
9406 ;;
9407
9408 (defun verilog-module-inside-filename-p (module filename)
9409 "Return modi if MODULE is specified inside FILENAME, else nil.
9410 Allows version control to check out the file if need be."
9411 (and (or (file-exists-p filename)
9412 (and (fboundp 'vc-backend)
9413 (vc-backend filename)))
9414 (let (modi type)
9415 (with-current-buffer (find-file-noselect filename)
9416 (save-excursion
9417 (goto-char (point-min))
9418 (while (and
9419 ;; It may be tempting to look for verilog-defun-re,
9420 ;; don't, it slows things down a lot!
9421 (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
9422 (setq type (match-string-no-properties 0))
9423 (verilog-re-search-forward-quick "[(;]" nil t))
9424 (if (equal module (verilog-read-module-name))
9425 (setq modi (verilog-modi-new module filename (point) type))))
9426 modi)))))
9427
9428 (defun verilog-is-number (symbol)
9429 "Return true if SYMBOL is number-like."
9430 (or (string-match "^[0-9 \t:]+$" symbol)
9431 (string-match "^[---]*[0-9]+$" symbol)
9432 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
9433
9434 (defun verilog-symbol-detick (symbol wing-it)
9435 "Return an expanded SYMBOL name without any defines.
9436 If the variable vh-{symbol} is defined, return that value.
9437 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
9438 (while (and symbol (string-match "^`" symbol))
9439 (setq symbol (substring symbol 1))
9440 (setq symbol
9441 ;; Namespace intentionally short for AUTOs and compatibility
9442 (if (boundp (intern (concat "vh-" symbol)))
9443 ;; Emacs has a bug where boundp on a buffer-local
9444 ;; variable in only one buffer returns t in another.
9445 ;; This can confuse, so check for nil.
9446 ;; Namespace intentionally short for AUTOs and compatibility
9447 (let ((val (eval (intern (concat "vh-" symbol)))))
9448 (if (eq val nil)
9449 (if wing-it symbol nil)
9450 val))
9451 (if wing-it symbol nil))))
9452 symbol)
9453 ;;(verilog-symbol-detick "`mod" nil)
9454
9455 (defun verilog-symbol-detick-denumber (symbol)
9456 "Return SYMBOL with defines converted and any numbers dropped to nil."
9457 (when (string-match "^`" symbol)
9458 ;; This only will work if the define is a simple signal, not
9459 ;; something like a[b]. Sorry, it should be substituted into the parser
9460 (setq symbol
9461 (verilog-string-replace-matches
9462 "\[[^0-9: \t]+\]" "" nil nil
9463 (or (verilog-symbol-detick symbol nil)
9464 (if verilog-auto-sense-defines-constant
9465 "0"
9466 symbol)))))
9467 (if (verilog-is-number symbol)
9468 nil
9469 symbol))
9470
9471 (defun verilog-symbol-detick-text (text)
9472 "Return TEXT without any known defines.
9473 If the variable vh-{symbol} is defined, substitute that value."
9474 (let ((ok t) symbol val)
9475 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
9476 (setq symbol (match-string 1 text))
9477 ;;(message symbol)
9478 (cond ((and
9479 ;; Namespace intentionally short for AUTOs and compatibility
9480 (boundp (intern (concat "vh-" symbol)))
9481 ;; Emacs has a bug where boundp on a buffer-local
9482 ;; variable in only one buffer returns t in another.
9483 ;; This can confuse, so check for nil.
9484 ;; Namespace intentionally short for AUTOs and compatibility
9485 (setq val (eval (intern (concat "vh-" symbol)))))
9486 (setq text (replace-match val nil nil text)))
9487 (t (setq ok nil)))))
9488 text)
9489 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
9490
9491 (defun verilog-expand-dirnames (&optional dirnames)
9492 "Return a list of existing directories given a list of wildcarded DIRNAMES.
9493 Or, just the existing dirnames themselves if there are no wildcards."
9494 ;; Note this function is performance critical.
9495 ;; Do not call anything that requires disk access that cannot be cached.
9496 (interactive)
9497 (unless dirnames (error "`verilog-library-directories' should include at least '.'"))
9498 (setq dirnames (reverse dirnames)) ; not nreverse
9499 (let ((dirlist nil)
9500 pattern dirfile dirfiles dirname root filename rest basefile)
9501 (while dirnames
9502 (setq dirname (substitute-in-file-name (car dirnames))
9503 dirnames (cdr dirnames))
9504 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ;; root
9505 "\\([^/\\]*[*?][^/\\]*\\)" ;; filename with *?
9506 "\\(.*\\)") ;; rest
9507 dirname)
9508 (setq root (match-string 1 dirname)
9509 filename (match-string 2 dirname)
9510 rest (match-string 3 dirname)
9511 pattern filename)
9512 ;; now replace those * and ? with .+ and .
9513 ;; use ^ and /> to get only whole file names
9514 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
9515 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
9516 pattern (concat "^" pattern "$")
9517 dirfiles (verilog-dir-files root))
9518 (while dirfiles
9519 (setq basefile (car dirfiles)
9520 dirfile (expand-file-name (concat root basefile rest))
9521 dirfiles (cdr dirfiles))
9522 (if (and (string-match pattern basefile)
9523 ;; Don't allow abc/*/rtl to match abc/rtl via ..
9524 (not (equal basefile "."))
9525 (not (equal basefile ".."))
9526 (file-directory-p dirfile))
9527 (setq dirlist (cons dirfile dirlist)))))
9528 ;; Defaults
9529 (t
9530 (if (file-directory-p dirname)
9531 (setq dirlist (cons dirname dirlist))))))
9532 dirlist))
9533 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
9534
9535 (defun verilog-library-filenames (filename &optional current check-ext)
9536 "Return a search path to find the given FILENAME or module name.
9537 Uses the optional CURRENT filename or variable `buffer-file-name', plus
9538 `verilog-library-directories' and `verilog-library-extensions'
9539 variables to build the path. With optional CHECK-EXT also check
9540 `verilog-library-extensions'."
9541 (unless current (setq current (buffer-file-name)))
9542 (unless verilog-dir-cache-preserving
9543 (setq verilog-dir-cache-lib-filenames nil))
9544 (let* ((cache-key (list filename current check-ext))
9545 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
9546 chkdirs chkdir chkexts fn outlist)
9547 (cond (fass ;; Return data from cache hit
9548 (nth 1 fass))
9549 (t
9550 ;; Note this expand can't be easily cached, as we need to
9551 ;; pick up buffer-local variables for newly read sub-module files
9552 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
9553 (while chkdirs
9554 (setq chkdir (expand-file-name (car chkdirs)
9555 (file-name-directory current))
9556 chkexts (if check-ext verilog-library-extensions `("")))
9557 (while chkexts
9558 (setq fn (expand-file-name (concat filename (car chkexts))
9559 chkdir))
9560 ;;(message "Check for %s" fn)
9561 (if (verilog-dir-file-exists-p fn)
9562 (setq outlist (cons (expand-file-name
9563 fn (file-name-directory current))
9564 outlist)))
9565 (setq chkexts (cdr chkexts)))
9566 (setq chkdirs (cdr chkdirs)))
9567 (setq outlist (nreverse outlist))
9568 (setq verilog-dir-cache-lib-filenames
9569 (cons (list cache-key outlist)
9570 verilog-dir-cache-lib-filenames))
9571 outlist))))
9572
9573 (defun verilog-module-filenames (module current)
9574 "Return a search path to find the given MODULE name.
9575 Uses the CURRENT filename, `verilog-library-extensions',
9576 `verilog-library-directories' and `verilog-library-files'
9577 variables to build the path."
9578 ;; Return search locations for it
9579 (append (list current) ; first, current buffer
9580 (verilog-library-filenames module current t)
9581 verilog-library-files)) ; finally, any libraries
9582
9583 ;;
9584 ;; Module Information
9585 ;;
9586 ;; Many of these functions work on "modi" a module information structure
9587 ;; A modi is: [module-name-string file-name begin-point]
9588
9589 (defvar verilog-cache-enabled t
9590 "Non-nil enables caching of signals, etc. Set to nil for debugging to make things SLOW!")
9591
9592 (defvar verilog-modi-cache-list nil
9593 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
9594 For speeding up verilog-modi-get-* commands.
9595 Buffer-local.")
9596 (make-variable-buffer-local 'verilog-modi-cache-list)
9597
9598 (defvar verilog-modi-cache-preserve-tick nil
9599 "Modification tick after which the cache is still considered valid.
9600 Use `verilog-preserve-modi-cache' to set it.")
9601 (defvar verilog-modi-cache-preserve-buffer nil
9602 "Modification tick after which the cache is still considered valid.
9603 Use `verilog-preserve-modi-cache' to set it.")
9604 (defvar verilog-modi-cache-current-enable nil
9605 "Non-nil means allow caching `verilog-modi-current', set by let().")
9606 (defvar verilog-modi-cache-current nil
9607 "Currently active `verilog-modi-current', if any, set by let().")
9608 (defvar verilog-modi-cache-current-max nil
9609 "Current endmodule point for `verilog-modi-cache-current', if any.")
9610
9611 (defun verilog-modi-current ()
9612 "Return the modi structure for the module currently at point, possibly cached."
9613 (cond ((and verilog-modi-cache-current
9614 (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
9615 (<= (point) verilog-modi-cache-current-max))
9616 ;; Slow assertion, for debugging the cache:
9617 ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
9618 verilog-modi-cache-current)
9619 (verilog-modi-cache-current-enable
9620 (setq verilog-modi-cache-current (verilog-modi-current-get)
9621 verilog-modi-cache-current-max
9622 ;; The cache expires when we pass "endmodule" as then the
9623 ;; current modi may change to the next module
9624 ;; This relies on the AUTOs generally inserting, not deleting text
9625 (save-excursion
9626 (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
9627 verilog-modi-cache-current)
9628 (t
9629 (verilog-modi-current-get))))
9630
9631 (defun verilog-modi-current-get ()
9632 "Return the modi structure for the module currently at point."
9633 (let* (name type pt)
9634 ;; read current module's name
9635 (save-excursion
9636 (verilog-re-search-backward-quick verilog-defun-re nil nil)
9637 (setq type (match-string-no-properties 0))
9638 (verilog-re-search-forward-quick "(" nil nil)
9639 (setq name (verilog-read-module-name))
9640 (setq pt (point)))
9641 ;; return modi - note this vector built two places
9642 (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
9643
9644 (defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
9645 (make-variable-buffer-local 'verilog-modi-lookup-cache)
9646 (defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
9647 (defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
9648
9649 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
9650 "Find the file and point at which MODULE is defined.
9651 If ALLOW-CACHE is set, check and remember cache of previous lookups.
9652 Return modi if successful, else print message unless IGNORE-ERROR is true."
9653 (let* ((current (or (buffer-file-name) (current-buffer)))
9654 modi)
9655 ;; Check cache
9656 ;;(message "verilog-modi-lookup: %s" module)
9657 (cond ((and verilog-modi-lookup-cache
9658 verilog-cache-enabled
9659 allow-cache
9660 (setq modi (gethash module verilog-modi-lookup-cache))
9661 (equal verilog-modi-lookup-last-current current)
9662 ;; If hit is in current buffer, then tick must match
9663 (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
9664 (not (equal current (verilog-modi-file-or-buffer modi)))))
9665 ;;(message "verilog-modi-lookup: HIT %S" modi)
9666 modi)
9667 ;; Miss
9668 (t (let* ((realname (verilog-symbol-detick module t))
9669 (orig-filenames (verilog-module-filenames realname current))
9670 (filenames orig-filenames)
9671 mif)
9672 (while (and filenames (not mif))
9673 (if (not (setq mif (verilog-module-inside-filename-p realname (car filenames))))
9674 (setq filenames (cdr filenames))))
9675 ;; mif has correct form to become later elements of modi
9676 (cond (mif (setq modi mif))
9677 (t (setq modi nil)
9678 (or ignore-error
9679 (error (concat (verilog-point-text)
9680 ": Can't locate " module " module definition"
9681 (if (not (equal module realname))
9682 (concat " (Expanded macro to " realname ")")
9683 "")
9684 "\n Check the verilog-library-directories variable."
9685 "\n I looked in (if not listed, doesn't exist):\n\t"
9686 (mapconcat 'concat orig-filenames "\n\t"))))))
9687 (when (eval-when-compile (fboundp 'make-hash-table))
9688 (unless verilog-modi-lookup-cache
9689 (setq verilog-modi-lookup-cache
9690 (make-hash-table :test 'equal :rehash-size 4.0)))
9691 (puthash module modi verilog-modi-lookup-cache))
9692 (setq verilog-modi-lookup-last-current current
9693 verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
9694 modi))
9695
9696 (defun verilog-modi-filename (modi)
9697 "Filename of MODI, or name of buffer if it's never been saved."
9698 (if (bufferp (verilog-modi-file-or-buffer modi))
9699 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
9700 (buffer-name (verilog-modi-file-or-buffer modi)))
9701 (verilog-modi-file-or-buffer modi)))
9702
9703 (defun verilog-modi-goto (modi)
9704 "Move point/buffer to specified MODI."
9705 (or modi (error "Passed unfound modi to goto, check earlier"))
9706 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
9707 (verilog-modi-file-or-buffer modi)
9708 (find-file-noselect (verilog-modi-file-or-buffer modi))))
9709 (or (equal major-mode `verilog-mode) ;; Put into Verilog mode to get syntax
9710 (verilog-mode))
9711 (goto-char (verilog-modi-get-point modi)))
9712
9713 (defun verilog-goto-defun-file (module)
9714 "Move point to the file at which a given MODULE is defined."
9715 (interactive "sGoto File for Module: ")
9716 (let* ((modi (verilog-modi-lookup module nil)))
9717 (when modi
9718 (verilog-modi-goto modi)
9719 (switch-to-buffer (current-buffer)))))
9720
9721 (defun verilog-modi-cache-results (modi function)
9722 "Run on MODI the given FUNCTION. Locate the module in a file.
9723 Cache the output of function so next call may have faster access."
9724 (let (fass)
9725 (save-excursion ;; Cache is buffer-local so can't avoid this.
9726 (verilog-modi-goto modi)
9727 (if (and (setq fass (assoc (list modi function)
9728 verilog-modi-cache-list))
9729 ;; Destroy caching when incorrect; Modified or file changed
9730 (not (and verilog-cache-enabled
9731 (or (equal (buffer-chars-modified-tick) (nth 1 fass))
9732 (and verilog-modi-cache-preserve-tick
9733 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
9734 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
9735 (equal (visited-file-modtime) (nth 2 fass)))))
9736 (setq verilog-modi-cache-list nil
9737 fass nil))
9738 (cond (fass
9739 ;; Return data from cache hit
9740 (nth 3 fass))
9741 (t
9742 ;; Read from file
9743 ;; Clear then restore any highlighting to make emacs19 happy
9744 (let (func-returns)
9745 (verilog-save-font-mods
9746 (setq func-returns (funcall function)))
9747 ;; Cache for next time
9748 (setq verilog-modi-cache-list
9749 (cons (list (list modi function)
9750 (buffer-chars-modified-tick)
9751 (visited-file-modtime)
9752 func-returns)
9753 verilog-modi-cache-list))
9754 func-returns))))))
9755
9756 (defun verilog-modi-cache-add (modi function element sig-list)
9757 "Add function return results to the module cache.
9758 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
9759 function now contains the additional SIG-LIST parameters."
9760 (let (fass)
9761 (save-excursion
9762 (verilog-modi-goto modi)
9763 (if (setq fass (assoc (list modi function)
9764 verilog-modi-cache-list))
9765 (let ((func-returns (nth 3 fass)))
9766 (aset func-returns element
9767 (append sig-list (aref func-returns element))))))))
9768
9769 (defmacro verilog-preserve-modi-cache (&rest body)
9770 "Execute the BODY forms, allowing cache preservation within BODY.
9771 This means that changes to the buffer will not result in the cache being
9772 flushed. If the changes affect the modsig state, they must call the
9773 modsig-cache-add-* function, else the results of later calls may be
9774 incorrect. Without this, changes are assumed to be adding/removing signals
9775 and invalidating the cache."
9776 `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
9777 (verilog-modi-cache-preserve-buffer (current-buffer)))
9778 (progn ,@body)))
9779
9780
9781 (defun verilog-modi-modport-lookup-one (modi name &optional ignore-error)
9782 "Given a MODI, return the declarations related to the given modport NAME."
9783 ;; Recursive routine - see below
9784 (let* ((realname (verilog-symbol-detick name t))
9785 (modport (assoc name (verilog-decls-get-modports (verilog-modi-get-decls modi)))))
9786 (or modport ignore-error
9787 (error (concat (verilog-point-text)
9788 ": Can't locate " name " modport definition"
9789 (if (not (equal name realname))
9790 (concat " (Expanded macro to " realname ")")
9791 ""))))
9792 (let* ((decls (verilog-modport-decls modport))
9793 (clks (verilog-modport-clockings modport)))
9794 ;; Now expand any clocking's
9795 (while clks
9796 (setq decls (verilog-decls-append
9797 decls
9798 (verilog-modi-modport-lookup-one modi (car clks) ignore-error)))
9799 (setq clks (cdr clks)))
9800 decls)))
9801
9802 (defun verilog-modi-modport-lookup (modi name-re &optional ignore-error)
9803 "Given a MODI, return the declarations related to the given modport NAME-RE.
9804 If the modport points to any clocking blocks, expand the signals to include
9805 those clocking block's signals."
9806 ;; Recursive routine - see below
9807 (let* ((mod-decls (verilog-modi-get-decls modi))
9808 (clks (verilog-decls-get-modports mod-decls))
9809 (name-re (concat "^" name-re "$"))
9810 (decls (verilog-decls-new nil nil nil nil nil nil nil nil nil)))
9811 ;; Pull in all modports
9812 (while clks
9813 (when (string-match name-re (verilog-modport-name (car clks)))
9814 (setq decls (verilog-decls-append
9815 decls
9816 (verilog-modi-modport-lookup-one modi (verilog-modport-name (car clks)) ignore-error))))
9817 (setq clks (cdr clks)))
9818 decls))
9819
9820 (defun verilog-signals-matching-enum (in-list enum)
9821 "Return all signals in IN-LIST matching the given ENUM."
9822 (let (out-list)
9823 (while in-list
9824 (if (equal (verilog-sig-enum (car in-list)) enum)
9825 (setq out-list (cons (car in-list) out-list)))
9826 (setq in-list (cdr in-list)))
9827 ;; New scheme
9828 ;; Namespace intentionally short for AUTOs and compatibility
9829 (let* ((enumvar (intern (concat "venum-" enum)))
9830 (enumlist (and (boundp enumvar) (eval enumvar))))
9831 (while enumlist
9832 (add-to-list 'out-list (list (car enumlist)))
9833 (setq enumlist (cdr enumlist))))
9834 (nreverse out-list)))
9835
9836 (defun verilog-signals-matching-regexp (in-list regexp)
9837 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
9838 (if (or (not regexp) (equal regexp ""))
9839 in-list
9840 (let ((case-fold-search verilog-case-fold)
9841 out-list)
9842 (while in-list
9843 (if (string-match regexp (verilog-sig-name (car in-list)))
9844 (setq out-list (cons (car in-list) out-list)))
9845 (setq in-list (cdr in-list)))
9846 (nreverse out-list))))
9847
9848 (defun verilog-signals-not-matching-regexp (in-list regexp)
9849 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
9850 (if (or (not regexp) (equal regexp ""))
9851 in-list
9852 (let ((case-fold-search verilog-case-fold)
9853 out-list)
9854 (while in-list
9855 (if (not (string-match regexp (verilog-sig-name (car in-list))))
9856 (setq out-list (cons (car in-list) out-list)))
9857 (setq in-list (cdr in-list)))
9858 (nreverse out-list))))
9859
9860 (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
9861 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
9862 if non-nil."
9863 (if (or (not regexp) (equal regexp ""))
9864 in-list
9865 (let (out-list to-match)
9866 (while in-list
9867 ;; Note verilog-insert-one-definition matches on this order
9868 (setq to-match (concat
9869 decl-type
9870 " " (verilog-sig-signed (car in-list))
9871 " " (verilog-sig-multidim (car in-list))
9872 (verilog-sig-bits (car in-list))))
9873 (if (string-match regexp to-match)
9874 (setq out-list (cons (car in-list) out-list)))
9875 (setq in-list (cdr in-list)))
9876 (nreverse out-list))))
9877
9878 (defun verilog-signals-edit-wire-reg (in-list)
9879 "Return all signals in IN-LIST with wire/reg data types made blank."
9880 (mapcar (lambda (sig)
9881 (when (member (verilog-sig-type sig) '("wire" "reg"))
9882 (verilog-sig-type-set sig nil))
9883 sig) in-list))
9884
9885 ;; Combined
9886 (defun verilog-decls-get-signals (decls)
9887 "Return all declared signals in DECLS, excluding 'assign' statements."
9888 (append
9889 (verilog-decls-get-outputs decls)
9890 (verilog-decls-get-inouts decls)
9891 (verilog-decls-get-inputs decls)
9892 (verilog-decls-get-vars decls)
9893 (verilog-decls-get-consts decls)
9894 (verilog-decls-get-gparams decls)))
9895
9896 (defun verilog-decls-get-ports (decls)
9897 (append
9898 (verilog-decls-get-outputs decls)
9899 (verilog-decls-get-inouts decls)
9900 (verilog-decls-get-inputs decls)))
9901
9902 (defun verilog-decls-get-iovars (decls)
9903 (append
9904 (verilog-decls-get-vars decls)
9905 (verilog-decls-get-outputs decls)
9906 (verilog-decls-get-inouts decls)
9907 (verilog-decls-get-inputs decls)))
9908
9909 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
9910 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
9911 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
9912 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
9913 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
9914 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
9915 (defsubst verilog-modi-cache-add-vars (modi sig-list)
9916 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
9917 (defsubst verilog-modi-cache-add-gparams (modi sig-list)
9918 (verilog-modi-cache-add modi 'verilog-read-decls 7 sig-list))
9919
9920 \f
9921 ;;
9922 ;; Auto creation utilities
9923 ;;
9924
9925 (defun verilog-auto-re-search-do (search-for func)
9926 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
9927 (goto-char (point-min))
9928 (while (verilog-re-search-forward-quick search-for nil t)
9929 (funcall func)))
9930
9931 (defun verilog-insert-one-definition (sig type indent-pt)
9932 "Print out a definition for SIG of the given TYPE,
9933 with appropriate INDENT-PT indentation."
9934 (indent-to indent-pt)
9935 ;; Note verilog-signals-matching-dir-re matches on this order
9936 (insert type)
9937 (when (verilog-sig-modport sig)
9938 (insert "." (verilog-sig-modport sig)))
9939 (when (verilog-sig-signed sig)
9940 (insert " " (verilog-sig-signed sig)))
9941 (when (verilog-sig-multidim sig)
9942 (insert " " (verilog-sig-multidim-string sig)))
9943 (when (verilog-sig-bits sig)
9944 (insert " " (verilog-sig-bits sig)))
9945 (indent-to (max 24 (+ indent-pt 16)))
9946 (unless (= (char-syntax (preceding-char)) ?\ )
9947 (insert " ")) ; Need space between "]name" if indent-to did nothing
9948 (insert (verilog-sig-name sig))
9949 (when (verilog-sig-memory sig)
9950 (insert " " (verilog-sig-memory sig))))
9951
9952 (defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
9953 "Print out a definition for MODI's list of SIGS of the given DIRECTION,
9954 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
9955 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
9956 When MODI is non-null, also add to modi-cache, for tracking."
9957 (when modi
9958 (cond ((equal direction "wire")
9959 (verilog-modi-cache-add-vars modi sigs))
9960 ((equal direction "reg")
9961 (verilog-modi-cache-add-vars modi sigs))
9962 ((equal direction "output")
9963 (verilog-modi-cache-add-outputs modi sigs)
9964 (when verilog-auto-declare-nettype
9965 (verilog-modi-cache-add-vars modi sigs)))
9966 ((equal direction "input")
9967 (verilog-modi-cache-add-inputs modi sigs)
9968 (when verilog-auto-declare-nettype
9969 (verilog-modi-cache-add-vars modi sigs)))
9970 ((equal direction "inout")
9971 (verilog-modi-cache-add-inouts modi sigs)
9972 (when verilog-auto-declare-nettype
9973 (verilog-modi-cache-add-vars modi sigs)))
9974 ((equal direction "interface"))
9975 ((equal direction "parameter")
9976 (verilog-modi-cache-add-gparams modi sigs))
9977 (t
9978 (error "Unsupported verilog-insert-definition direction: %s" direction))))
9979 (or dont-sort
9980 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
9981 (while sigs
9982 (let ((sig (car sigs)))
9983 (verilog-insert-one-definition
9984 sig
9985 ;; Want "type x" or "output type x", not "wire type x"
9986 (cond ((or (verilog-sig-type sig)
9987 verilog-auto-wire-type)
9988 (concat
9989 (when (member direction '("input" "output" "inout"))
9990 (concat direction " "))
9991 (or (verilog-sig-type sig)
9992 verilog-auto-wire-type)))
9993 ((and verilog-auto-declare-nettype
9994 (member direction '("input" "output" "inout")))
9995 (concat direction " " verilog-auto-declare-nettype))
9996 (t
9997 direction))
9998 indent-pt)
9999 (insert (if v2k "," ";"))
10000 (if (or (not (verilog-sig-comment sig))
10001 (equal "" (verilog-sig-comment sig)))
10002 (insert "\n")
10003 (indent-to (max 48 (+ indent-pt 40)))
10004 (verilog-insert "// " (verilog-sig-comment sig) "\n"))
10005 (setq sigs (cdr sigs)))))
10006
10007 (eval-when-compile
10008 (if (not (boundp 'indent-pt))
10009 (defvar indent-pt nil "Local used by insert-indent")))
10010
10011 (defun verilog-insert-indent (&rest stuff)
10012 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
10013 Presumes that any newlines end a list element."
10014 (let ((need-indent t))
10015 (while stuff
10016 (if need-indent (indent-to indent-pt))
10017 (setq need-indent nil)
10018 (verilog-insert (car stuff))
10019 (setq need-indent (string-match "\n$" (car stuff))
10020 stuff (cdr stuff)))))
10021 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
10022
10023 (defun verilog-forward-or-insert-line ()
10024 "Move forward a line, unless at EOB, then insert a newline."
10025 (if (eobp) (insert "\n")
10026 (forward-line)))
10027
10028 (defun verilog-repair-open-comma ()
10029 "Insert comma if previous argument is other than an open parenthesis or endif."
10030 ;; We can't just search backward for ) as it might be inside another expression.
10031 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
10032 (save-excursion
10033 (verilog-backward-syntactic-ws-quick)
10034 (when (and (not (save-excursion ;; Not beginning (, or existing ,
10035 (backward-char 1)
10036 (looking-at "[(,]")))
10037 (not (save-excursion ;; Not `endif, or user define
10038 (backward-char 1)
10039 (skip-chars-backward "[a-zA-Z0-9_`]")
10040 (looking-at "`"))))
10041 (insert ","))))
10042
10043 (defun verilog-repair-close-comma ()
10044 "If point is at a comma followed by a close parenthesis, fix it.
10045 This repairs those mis-inserted by an AUTOARG."
10046 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
10047 (save-excursion
10048 (verilog-forward-close-paren)
10049 (backward-char 1)
10050 (verilog-backward-syntactic-ws-quick)
10051 (backward-char 1)
10052 (when (looking-at ",")
10053 (delete-char 1))))
10054
10055 (defun verilog-make-width-expression (range-exp)
10056 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
10057 ;; strip off the []
10058 (cond ((not range-exp)
10059 "1")
10060 (t
10061 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
10062 (setq range-exp (match-string 1 range-exp)))
10063 (cond ((not range-exp)
10064 "1")
10065 ;; [#:#] We can compute a numeric result
10066 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
10067 range-exp)
10068 (int-to-string
10069 (1+ (abs (- (string-to-number (match-string 1 range-exp))
10070 (string-to-number (match-string 2 range-exp)))))))
10071 ;; [PARAM-1:0] can just return PARAM
10072 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
10073 (match-string 1 range-exp))
10074 ;; [arbitrary] need math
10075 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
10076 (concat "(1+(" (match-string 1 range-exp) ")"
10077 (if (equal "0" (match-string 2 range-exp))
10078 "" ;; Don't bother with -(0)
10079 (concat "-(" (match-string 2 range-exp) ")"))
10080 ")"))
10081 (t nil)))))
10082 ;;(verilog-make-width-expression "`A:`B")
10083
10084 (defun verilog-simplify-range-expression (expr)
10085 "Return a simplified range expression with constants eliminated from EXPR."
10086 ;; Note this is always called with brackets; ie [z] or [z:z]
10087 (if (not (string-match "[---+*()]" expr))
10088 expr ;; short-circuit
10089 (let ((out expr)
10090 (last-pass ""))
10091 (while (not (equal last-pass out))
10092 (setq last-pass out)
10093 ;; Prefix regexp needs beginning of match, or some symbol of
10094 ;; lesser or equal precedence. We assume the [:]'s exist in expr.
10095 ;; Ditto the end.
10096 (while (string-match
10097 (concat "\\([[({:*+-]\\)" ; - must be last
10098 "(\\<\\([0-9A-Za-z_]+\\))"
10099 "\\([])}:*+-]\\)")
10100 out)
10101 (setq out (replace-match "\\1\\2\\3" nil nil out)))
10102 (while (string-match
10103 (concat "\\([[({:*+-]\\)" ; - must be last
10104 "\\$clog2\\s *(\\<\\([0-9]+\\))"
10105 "\\([])}:*+-]\\)")
10106 out)
10107 (setq out (replace-match
10108 (concat
10109 (match-string 1 out)
10110 (int-to-string (verilog-clog2 (string-to-number (match-string 2 out))))
10111 (match-string 3 out))
10112 nil nil out)))
10113 ;; For precedence do * before +/-
10114 (while (string-match
10115 (concat "\\([[({:*+-]\\)"
10116 "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
10117 "\\([])}:*+-]\\)")
10118 out)
10119 (setq out (replace-match
10120 (concat (match-string 1 out)
10121 (int-to-string (* (string-to-number (match-string 2 out))
10122 (string-to-number (match-string 4 out))))
10123 (match-string 5 out))
10124 nil nil out)))
10125 (while (string-match
10126 (concat "\\([[({:+-]\\)" ; No * here as higher prec
10127 "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
10128 "\\([])}:+-]\\)")
10129 out)
10130 (let ((pre (match-string 1 out))
10131 (lhs (string-to-number (match-string 2 out)))
10132 (rhs (string-to-number (match-string 4 out)))
10133 (post (match-string 5 out))
10134 val)
10135 (when (equal pre "-")
10136 (setq lhs (- lhs)))
10137 (setq val (if (equal (match-string 3 out) "-")
10138 (- lhs rhs)
10139 (+ lhs rhs))
10140 out (replace-match
10141 (concat (if (and (equal pre "-")
10142 (< val 0))
10143 "" ;; Not "--20" but just "-20"
10144 pre)
10145 (int-to-string val)
10146 post)
10147 nil nil out)) )))
10148 out)))
10149
10150 ;;(verilog-simplify-range-expression "[1:3]") ;; 1
10151 ;;(verilog-simplify-range-expression "[(1):3]") ;; 1
10152 ;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ;;20
10153 ;;(verilog-simplify-range-expression "[(2*3+6*7)]") ;; 48
10154 ;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ;; FOO*4-2
10155 ;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ;; FOO*4+0
10156 ;;(verilog-simplify-range-expression "[(func(BAR))]") ;; func(BAR)
10157 ;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ;; FOO-0
10158 ;;(verilog-simplify-range-expression "[$clog2(2)]") ;; 1
10159 ;;(verilog-simplify-range-expression "[$clog2(7)]") ;; 3
10160
10161 (defun verilog-clog2 (value)
10162 "Compute $clog2 - ceiling log2 of VALUE."
10163 (if (< value 1)
10164 0
10165 (ceiling (/ (log value) (log 2)))))
10166
10167 (defun verilog-typedef-name-p (variable-name)
10168 "Return true if the VARIABLE-NAME is a type definition."
10169 (when verilog-typedef-regexp
10170 (verilog-string-match-fold verilog-typedef-regexp variable-name)))
10171 \f
10172 ;;
10173 ;; Auto deletion
10174 ;;
10175
10176 (defun verilog-delete-autos-lined ()
10177 "Delete autos that occupy multiple lines, between begin and end comments."
10178 ;; The newline must not have a comment property, so we must
10179 ;; delete the end auto's newline, not the first newline
10180 (forward-line 1)
10181 (let ((pt (point)))
10182 (when (and
10183 (looking-at "\\s-*// Beginning")
10184 (search-forward "// End of automatic" nil t))
10185 ;; End exists
10186 (end-of-line)
10187 (forward-line 1)
10188 (delete-region pt (point)))))
10189
10190 (defun verilog-delete-empty-auto-pair ()
10191 "Delete begin/end auto pair at point, if empty."
10192 (forward-line 0)
10193 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
10194 "\\s-*// End of automatics\n"))
10195 (delete-region (point) (save-excursion (forward-line 2) (point)))))
10196
10197 (defun verilog-forward-close-paren ()
10198 "Find the close parenthesis that match the current point.
10199 Ignore other close parenthesis with matching open parens."
10200 (let ((parens 1))
10201 (while (> parens 0)
10202 (unless (verilog-re-search-forward-quick "[()]" nil t)
10203 (error "%s: Mismatching ()" (verilog-point-text)))
10204 (cond ((= (preceding-char) ?\( )
10205 (setq parens (1+ parens)))
10206 ((= (preceding-char) ?\) )
10207 (setq parens (1- parens)))))))
10208
10209 (defun verilog-backward-open-paren ()
10210 "Find the open parenthesis that match the current point.
10211 Ignore other open parenthesis with matching close parens."
10212 (let ((parens 1))
10213 (while (> parens 0)
10214 (unless (verilog-re-search-backward-quick "[()]" nil t)
10215 (error "%s: Mismatching ()" (verilog-point-text)))
10216 (cond ((= (following-char) ?\) )
10217 (setq parens (1+ parens)))
10218 ((= (following-char) ?\( )
10219 (setq parens (1- parens)))))))
10220
10221 (defun verilog-backward-open-bracket ()
10222 "Find the open bracket that match the current point.
10223 Ignore other open bracket with matching close bracket."
10224 (let ((parens 1))
10225 (while (> parens 0)
10226 (unless (verilog-re-search-backward-quick "[][]" nil t)
10227 (error "%s: Mismatching []" (verilog-point-text)))
10228 (cond ((= (following-char) ?\] )
10229 (setq parens (1+ parens)))
10230 ((= (following-char) ?\[ )
10231 (setq parens (1- parens)))))))
10232
10233 (defun verilog-delete-to-paren ()
10234 "Delete the automatic inst/sense/arg created by autos.
10235 Deletion stops at the matching end parenthesis, outside comments."
10236 (delete-region (point)
10237 (save-excursion
10238 (verilog-backward-open-paren)
10239 (verilog-forward-sexp-ign-cmt 1) ;; Moves to paren that closes argdecl's
10240 (backward-char 1)
10241 (point))))
10242
10243 (defun verilog-auto-star-safe ()
10244 "Return if a .* AUTOINST is safe to delete or expand.
10245 It was created by the AUTOS themselves, or by the user."
10246 (and verilog-auto-star-expand
10247 (looking-at
10248 (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
10249
10250 (defun verilog-delete-auto-star-all ()
10251 "Delete a .* AUTOINST, if it is safe."
10252 (when (verilog-auto-star-safe)
10253 (verilog-delete-to-paren)))
10254
10255 (defun verilog-delete-auto-star-implicit ()
10256 "Delete all .* implicit connections created by `verilog-auto-star'.
10257 This function will be called automatically at save unless
10258 `verilog-auto-star-save' is set, any non-templated expanded pins will be
10259 removed."
10260 (interactive)
10261 (let (paren-pt indent have-close-paren)
10262 (save-excursion
10263 (goto-char (point-min))
10264 ;; We need to match these even outside of comments.
10265 ;; For reasonable performance, we don't check if inside comments, sorry.
10266 (while (re-search-forward "// Implicit \\.\\*" nil t)
10267 (setq paren-pt (point))
10268 (beginning-of-line)
10269 (setq have-close-paren
10270 (save-excursion
10271 (when (search-forward ");" paren-pt t)
10272 (setq indent (current-indentation))
10273 t)))
10274 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
10275 (when have-close-paren
10276 ;; Delete extra commentary
10277 (save-excursion
10278 (while (progn
10279 (forward-line -1)
10280 (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
10281 (delete-region (match-beginning 0) (match-end 0))))
10282 ;; If it is simple, we can put the ); on the same line as the last text
10283 (let ((rtn-pt (point)))
10284 (save-excursion
10285 (while (progn (backward-char 1)
10286 (looking-at "[ \t\n\f]")))
10287 (when (looking-at ",")
10288 (delete-region (+ 1 (point)) rtn-pt))))
10289 (when (bolp)
10290 (indent-to indent))
10291 (insert ");\n")
10292 ;; Still need to kill final comma - always is one as we put one after the .*
10293 (re-search-backward ",")
10294 (delete-char 1))))))
10295
10296 (defun verilog-delete-auto ()
10297 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
10298 Use \\[verilog-auto] to re-insert the updated AUTOs.
10299
10300 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
10301 called before and after this function, respectively."
10302 (interactive)
10303 (save-excursion
10304 (if (buffer-file-name)
10305 (find-file-noselect (buffer-file-name))) ;; To check we have latest version
10306 (verilog-save-no-change-functions
10307 (verilog-save-scan-cache
10308 ;; Allow user to customize
10309 (verilog-run-hooks 'verilog-before-delete-auto-hook)
10310
10311 ;; Remove those that have multi-line insertions, possibly with parameters
10312 ;; We allow anything beginning with AUTO, so that users can add their own
10313 ;; patterns
10314 (verilog-auto-re-search-do
10315 (concat "/\\*AUTO[A-Za-z0-9_]+"
10316 ;; Optional parens or quoted parameter or .* for (((...)))
10317 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
10318 "\\*/")
10319 'verilog-delete-autos-lined)
10320 ;; Remove those that are in parenthesis
10321 (verilog-auto-re-search-do
10322 (concat "/\\*"
10323 (eval-when-compile
10324 (verilog-regexp-words
10325 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
10326 "AUTOSENSE")))
10327 "\\*/")
10328 'verilog-delete-to-paren)
10329 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
10330 (verilog-auto-re-search-do "\\.\\*"
10331 'verilog-delete-auto-star-all)
10332 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
10333 (goto-char (point-min))
10334 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
10335 (replace-match ""))
10336
10337 ;; Final customize
10338 (verilog-run-hooks 'verilog-delete-auto-hook)))))
10339 \f
10340 ;;
10341 ;; Auto inject
10342 ;;
10343
10344 (defun verilog-inject-auto ()
10345 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
10346
10347 Any always @ blocks with sensitivity lists that match computed lists will
10348 be replaced with /*AS*/ comments.
10349
10350 Any cells will get /*AUTOINST*/ added to the end of the pin list.
10351 Pins with have identical names will be deleted.
10352
10353 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
10354 support adding new ports. You may wish to delete older ports yourself.
10355
10356 For example:
10357
10358 module ExampInject (i, o);
10359 input i;
10360 input j;
10361 output o;
10362 always @ (i or j)
10363 o = i | j;
10364 InstModule instName
10365 (.foobar(baz),
10366 j(j));
10367 endmodule
10368
10369 Typing \\[verilog-inject-auto] will make this into:
10370
10371 module ExampInject (i, o/*AUTOARG*/
10372 // Inputs
10373 j);
10374 input i;
10375 output o;
10376 always @ (/*AS*/i or j)
10377 o = i | j;
10378 InstModule instName
10379 (.foobar(baz),
10380 /*AUTOINST*/
10381 // Outputs
10382 j(j));
10383 endmodule"
10384 (interactive)
10385 (verilog-auto t))
10386
10387 (defun verilog-inject-arg ()
10388 "Inject AUTOARG into new code. See `verilog-inject-auto'."
10389 ;; Presume one module per file.
10390 (save-excursion
10391 (goto-char (point-min))
10392 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
10393 (let ((endmodp (save-excursion
10394 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
10395 (point))))
10396 ;; See if there's already a comment .. inside a comment so not verilog-re-search
10397 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
10398 (verilog-re-search-forward-quick ";" nil t)
10399 (backward-char 1)
10400 (verilog-backward-syntactic-ws-quick)
10401 (backward-char 1) ; Moves to paren that closes argdecl's
10402 (when (looking-at ")")
10403 (verilog-insert "/*AUTOARG*/")))))))
10404
10405 (defun verilog-inject-sense ()
10406 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
10407 (save-excursion
10408 (goto-char (point-min))
10409 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
10410 (let* ((start-pt (point))
10411 (modi (verilog-modi-current))
10412 (moddecls (verilog-modi-get-decls modi))
10413 pre-sigs
10414 got-sigs)
10415 (backward-char 1)
10416 (verilog-forward-sexp-ign-cmt 1)
10417 (backward-char 1) ;; End )
10418 (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
10419 (setq pre-sigs (verilog-signals-from-signame
10420 (verilog-read-signals start-pt (point)))
10421 got-sigs (verilog-auto-sense-sigs moddecls nil))
10422 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
10423 (verilog-signals-not-in got-sigs pre-sigs)))
10424 (delete-region start-pt (point))
10425 (verilog-insert "/*AS*/")))))))
10426
10427 (defun verilog-inject-inst ()
10428 "Inject AUTOINST into new code. See `verilog-inject-auto'."
10429 (save-excursion
10430 (goto-char (point-min))
10431 ;; It's hard to distinguish modules; we'll instead search for pins.
10432 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_\$]+\\s *(\\s *[a-zA-Z0-9`_\$]+\\s *)" nil t)
10433 (verilog-backward-open-paren) ;; Inst start
10434 (cond
10435 ((= (preceding-char) ?\#) ;; #(...) parameter section, not pin. Skip.
10436 (forward-char 1)
10437 (verilog-forward-close-paren)) ;; Parameters done
10438 (t
10439 (forward-char 1)
10440 (let ((indent-pt (+ (current-column)))
10441 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
10442 (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
10443 (goto-char end-pt)) ;; Already there, continue search with next instance
10444 (t
10445 ;; Delete identical interconnect
10446 (let ((case-fold-search nil)) ;; So we don't convert upper-to-lower, etc
10447 (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_\$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
10448 (delete-region (match-beginning 0) (match-end 0))
10449 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ;; Keep it correct
10450 (while (or (looking-at "[ \t\n\f,]+")
10451 (looking-at "//[^\n]*"))
10452 (delete-region (match-beginning 0) (match-end 0))
10453 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
10454 (verilog-forward-close-paren)
10455 (backward-char 1)
10456 ;; Not verilog-re-search, as we don't want to strip comments
10457 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
10458 (delete-region (match-beginning 0) (match-end 0)))
10459 (verilog-insert "\n")
10460 (verilog-insert-indent "/*AUTOINST*/")))))))))
10461 \f
10462 ;;
10463 ;; Auto diff
10464 ;;
10465
10466 (defun verilog-diff-buffers-p (b1 b2 &optional whitespace)
10467 "Return nil if buffers B1 and B2 have same contents.
10468 Else, return point in B1 that first mismatches.
10469 If optional WHITESPACE true, ignore whitespace."
10470 (save-excursion
10471 (let* ((case-fold-search nil) ;; compare-buffer-substrings cares
10472 (p1 (with-current-buffer b1 (goto-char (point-min))))
10473 (p2 (with-current-buffer b2 (goto-char (point-min))))
10474 (maxp1 (with-current-buffer b1 (point-max)))
10475 (maxp2 (with-current-buffer b2 (point-max)))
10476 (op1 -1) (op2 -1)
10477 progress size)
10478 (while (not (and (eq p1 op1) (eq p2 op2)))
10479 ;; If both windows have whitespace optionally skip over it.
10480 (when whitespace
10481 ;; skip-syntax-* doesn't count \n
10482 (with-current-buffer b1
10483 (goto-char p1)
10484 (skip-chars-forward " \t\n\r\f\v")
10485 (setq p1 (point)))
10486 (with-current-buffer b2
10487 (goto-char p2)
10488 (skip-chars-forward " \t\n\r\f\v")
10489 (setq p2 (point))))
10490 (setq size (min (- maxp1 p1) (- maxp2 p2)))
10491 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
10492 b1 p1 (+ size p1)))
10493 (setq progress (if (zerop progress) size (1- (abs progress))))
10494 (setq op1 p1 op2 p2
10495 p1 (+ p1 progress)
10496 p2 (+ p2 progress)))
10497 ;; Return value
10498 (if (and (eq p1 maxp1) (eq p2 maxp2))
10499 nil p1))))
10500
10501 (defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
10502 "View the differences between file F1 and buffer B2.
10503 This requires the external program `diff-command' to be in your `exec-path',
10504 and uses `diff-switches' in which you may want to have \"-u\" flag.
10505 Ignores WHITESPACE if t, and writes output to stdout if SHOW."
10506 ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
10507 ;; call `diff' as `diff' has different calling semantics on different
10508 ;; versions of Emacs.
10509 (if (not (file-exists-p f1))
10510 (message "Buffer %s has no associated file on disc" (buffer-name b2))
10511 (with-temp-buffer "*Verilog-Diff*"
10512 (let ((outbuf (current-buffer))
10513 (f2 (make-temp-file "vm-diff-auto-")))
10514 (unwind-protect
10515 (progn
10516 (with-current-buffer b2
10517 (save-restriction
10518 (widen)
10519 (write-region (point-min) (point-max) f2 nil 'nomessage)))
10520 (call-process diff-command nil outbuf t
10521 diff-switches ;; User may want -u in diff-switches
10522 (if whitespace "-b" "")
10523 f1 f2)
10524 ;; Print out results. Alternatively we could have call-processed
10525 ;; ourself, but this way we can reuse diff switches
10526 (when show
10527 (with-current-buffer outbuf (message "%s" (buffer-string))))))
10528 (sit-for 0)
10529 (when (file-exists-p f2)
10530 (delete-file f2))))))
10531
10532 (defun verilog-diff-report (b1 b2 diffpt)
10533 "Report differences detected with `verilog-diff-auto'.
10534 Differences are between buffers B1 and B2, starting at point
10535 DIFFPT. This function is called via `verilog-diff-function'."
10536 (let ((name1 (with-current-buffer b1 (buffer-file-name))))
10537 (verilog-warn "%s:%d: Difference in AUTO expansion found"
10538 name1 (with-current-buffer b1
10539 (count-lines (point-min) diffpt)))
10540 (cond (noninteractive
10541 (verilog-diff-file-with-buffer name1 b2 t t))
10542 (t
10543 (ediff-buffers b1 b2)))))
10544
10545 (defun verilog-diff-auto ()
10546 "Expand AUTOs in a temporary buffer and indicate any change.
10547 Whitespace is ignored when detecting differences, but once a
10548 difference is detected, whitespace differences may be shown.
10549
10550 To call this from the command line, see \\[verilog-batch-diff-auto].
10551
10552 The action on differences is selected with
10553 `verilog-diff-function'. The default is `verilog-diff-report'
10554 which will report an error and run `ediff' in interactive mode,
10555 or `diff' in batch mode."
10556 (interactive)
10557 (let ((b1 (current-buffer)) b2 diffpt
10558 (name1 (buffer-file-name))
10559 (newname "*Verilog-Diff*"))
10560 (save-excursion
10561 (when (get-buffer newname)
10562 (kill-buffer newname))
10563 (setq b2 (let (buffer-file-name) ;; Else clone is upset
10564 (clone-buffer newname)))
10565 (with-current-buffer b2
10566 ;; auto requires the filename, but can't have same filename in two
10567 ;; buffers; so override both b1 and b2's names
10568 (let ((buffer-file-name name1))
10569 (unwind-protect
10570 (progn
10571 (with-current-buffer b1 (setq buffer-file-name nil))
10572 (verilog-auto)
10573 (when (not verilog-auto-star-save)
10574 (verilog-delete-auto-star-implicit)))
10575 ;; Restore name if unwind
10576 (with-current-buffer b1 (setq buffer-file-name name1)))))
10577 ;;
10578 (setq diffpt (verilog-diff-buffers-p b1 b2 t))
10579 (cond ((not diffpt)
10580 (unless noninteractive (message "AUTO expansion identical"))
10581 (kill-buffer newname)) ;; Nice to cleanup after oneself
10582 (t
10583 (funcall verilog-diff-function b1 b2 diffpt)))
10584 ;; Return result of compare
10585 diffpt)))
10586
10587 \f
10588 ;;
10589 ;; Auto save
10590 ;;
10591
10592 (defun verilog-auto-save-check ()
10593 "On saving see if we need auto update."
10594 (cond ((not verilog-auto-save-policy)) ; disabled
10595 ((not (save-excursion
10596 (save-match-data
10597 (let ((case-fold-search nil))
10598 (goto-char (point-min))
10599 (re-search-forward "AUTO" nil t))))))
10600 ((eq verilog-auto-save-policy 'force)
10601 (verilog-auto))
10602 ((not (buffer-modified-p)))
10603 ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
10604 ((eq verilog-auto-save-policy 'detect)
10605 (verilog-auto))
10606 (t
10607 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
10608 (verilog-auto))
10609 ;; Don't ask again if didn't update
10610 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
10611 (when (not verilog-auto-star-save)
10612 (verilog-delete-auto-star-implicit))
10613 nil) ;; Always return nil -- we don't write the file ourselves
10614
10615 (defun verilog-auto-read-locals ()
10616 "Return file local variable segment at bottom of file."
10617 (save-excursion
10618 (goto-char (point-max))
10619 (if (re-search-backward "Local Variables:" nil t)
10620 (buffer-substring-no-properties (point) (point-max))
10621 "")))
10622
10623 (defun verilog-auto-reeval-locals (&optional force)
10624 "Read file local variable segment at bottom of file if it has changed.
10625 If FORCE, always reread it."
10626 (let ((curlocal (verilog-auto-read-locals)))
10627 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
10628 (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
10629 ;; Note this may cause this function to be recursively invoked,
10630 ;; because hack-local-variables may call (verilog-mode)
10631 ;; The above when statement will prevent it from recursing forever.
10632 (hack-local-variables)
10633 t)))
10634 \f
10635 ;;
10636 ;; Auto creation
10637 ;;
10638
10639 (defun verilog-auto-arg-ports (sigs message indent-pt)
10640 "Print a list of ports for AUTOARG.
10641 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
10642 (when sigs
10643 (when verilog-auto-arg-sort
10644 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10645 (insert "\n")
10646 (indent-to indent-pt)
10647 (insert message)
10648 (insert "\n")
10649 (let ((space ""))
10650 (indent-to indent-pt)
10651 (while sigs
10652 (cond ((equal verilog-auto-arg-format 'single)
10653 (indent-to indent-pt)
10654 (setq space "\n"))
10655 ;; verilog-auto-arg-format 'packed
10656 ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
10657 (insert "\n")
10658 (indent-to indent-pt)
10659 (setq space " "))
10660 (t
10661 (insert space)
10662 (setq space " ")))
10663 (insert (verilog-sig-name (car sigs)) ",")
10664 (setq sigs (cdr sigs))))))
10665
10666 (defun verilog-auto-arg ()
10667 "Expand AUTOARG statements.
10668 Replace the argument declarations at the beginning of the
10669 module with ones automatically derived from input and output
10670 statements. This can be dangerous if the module is instantiated
10671 using position-based connections, so use only name-based when
10672 instantiating the resulting module. Long lines are split based
10673 on the `fill-column', see \\[set-fill-column].
10674
10675 Limitations:
10676 Concatenation and outputting partial buses is not supported.
10677
10678 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10679
10680 For example:
10681
10682 module ExampArg (/*AUTOARG*/);
10683 input i;
10684 output o;
10685 endmodule
10686
10687 Typing \\[verilog-auto] will make this into:
10688
10689 module ExampArg (/*AUTOARG*/
10690 // Outputs
10691 o,
10692 // Inputs
10693 i
10694 );
10695 input i;
10696 output o;
10697 endmodule
10698
10699 The argument declarations may be printed in declaration order to
10700 best suit order based instantiations, or alphabetically, based on
10701 the `verilog-auto-arg-sort' variable.
10702
10703 Formatting is controlled with `verilog-auto-arg-format' variable.
10704
10705 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
10706 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
10707 conservative guess on adding a comma for the first signal, if you have
10708 any ifdefs or complicated expressions before the AUTOARG you will need
10709 to choose the comma yourself.
10710
10711 Avoid declaring ports manually, as it makes code harder to maintain."
10712 (save-excursion
10713 (let* ((modi (verilog-modi-current))
10714 (moddecls (verilog-modi-get-decls modi))
10715 (skip-pins (aref (verilog-read-arg-pins) 0)))
10716 (verilog-repair-open-comma)
10717 (verilog-auto-arg-ports (verilog-signals-not-in
10718 (verilog-decls-get-outputs moddecls)
10719 skip-pins)
10720 "// Outputs"
10721 verilog-indent-level-declaration)
10722 (verilog-auto-arg-ports (verilog-signals-not-in
10723 (verilog-decls-get-inouts moddecls)
10724 skip-pins)
10725 "// Inouts"
10726 verilog-indent-level-declaration)
10727 (verilog-auto-arg-ports (verilog-signals-not-in
10728 (verilog-decls-get-inputs moddecls)
10729 skip-pins)
10730 "// Inputs"
10731 verilog-indent-level-declaration)
10732 (verilog-repair-close-comma)
10733 (unless (eq (char-before) ?/ )
10734 (insert "\n"))
10735 (indent-to verilog-indent-level-declaration))))
10736
10737 (defun verilog-auto-assign-modport ()
10738 "Expand AUTOASSIGNMODPORT statements, as part of \\[verilog-auto].
10739 Take input/output/inout statements from the specified interface
10740 and modport and use to build assignments into the modport, for
10741 making verification modules that connect to UVM interfaces.
10742
10743 The first parameter is the name of an interface.
10744
10745 The second parameter is a regexp of modports to read from in
10746 that interface.
10747
10748 The third parameter is the instance name to use to dot reference into.
10749
10750 The optional fourth parameter is a regular expression, and only
10751 signals matching the regular expression will be included.
10752
10753 Limitations:
10754
10755 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
10756
10757 Inouts are not supported, as assignments must be unidirectional.
10758
10759 If a signal is part of the interface header and in both a
10760 modport and the interface itself, it will not be listed. (As
10761 this would result in a syntax error when the connections are
10762 made.)
10763
10764 See the example in `verilog-auto-inout-modport'."
10765 (save-excursion
10766 (let* ((params (verilog-read-auto-params 3 4))
10767 (submod (nth 0 params))
10768 (modport-re (nth 1 params))
10769 (inst-name (nth 2 params))
10770 (regexp (nth 3 params))
10771 direction-re submodi) ;; direction argument not supported until requested
10772 ;; Lookup position, etc of co-module
10773 ;; Note this may raise an error
10774 (when (setq submodi (verilog-modi-lookup submod t))
10775 (let* ((indent-pt (current-indentation))
10776 (submoddecls (verilog-modi-get-decls submodi))
10777 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
10778 (sig-list-i (verilog-signals-in ;; Decls doesn't have data types, must resolve
10779 (verilog-decls-get-vars submoddecls)
10780 (verilog-signals-not-in
10781 (verilog-decls-get-inputs submodportdecls)
10782 (verilog-decls-get-ports submoddecls))))
10783 (sig-list-o (verilog-signals-in ;; Decls doesn't have data types, must resolve
10784 (verilog-decls-get-vars submoddecls)
10785 (verilog-signals-not-in
10786 (verilog-decls-get-outputs submodportdecls)
10787 (verilog-decls-get-ports submoddecls)))))
10788 (forward-line 1)
10789 (setq sig-list-i (verilog-signals-edit-wire-reg
10790 (verilog-signals-matching-dir-re
10791 (verilog-signals-matching-regexp sig-list-i regexp)
10792 "input" direction-re))
10793 sig-list-o (verilog-signals-edit-wire-reg
10794 (verilog-signals-matching-dir-re
10795 (verilog-signals-matching-regexp sig-list-o regexp)
10796 "output" direction-re)))
10797 (setq sig-list-i (sort (copy-alist sig-list-i) `verilog-signals-sort-compare))
10798 (setq sig-list-o (sort (copy-alist sig-list-o) `verilog-signals-sort-compare))
10799 (when (or sig-list-i sig-list-o)
10800 (verilog-insert-indent "// Beginning of automatic assignments from modport\n")
10801 ;; Don't sort them so an upper AUTOINST will match the main module
10802 (let ((sigs sig-list-o))
10803 (while sigs
10804 (verilog-insert-indent "assign " (verilog-sig-name (car sigs))
10805 " = " inst-name
10806 "." (verilog-sig-name (car sigs)) ";\n")
10807 (setq sigs (cdr sigs))))
10808 (let ((sigs sig-list-i))
10809 (while sigs
10810 (verilog-insert-indent "assign " inst-name
10811 "." (verilog-sig-name (car sigs))
10812 " = " (verilog-sig-name (car sigs)) ";\n")
10813 (setq sigs (cdr sigs))))
10814 (verilog-insert-indent "// End of automatics\n")))))))
10815
10816 (defun verilog-auto-inst-port-map (_port-st)
10817 nil)
10818
10819 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
10820 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10821 (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
10822 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10823 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
10824 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
10825 (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10826 (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10827
10828 (defun verilog-auto-inst-port (port-st indent-pt tpl-list tpl-num for-star par-values)
10829 "Print out an instantiation connection for this PORT-ST.
10830 Insert to INDENT-PT, use template TPL-LIST.
10831 @ are instantiation numbers, replaced with TPL-NUM.
10832 @\"(expression @)\" are evaluated, with @ as a variable.
10833 If FOR-STAR add comment it is a .* expansion.
10834 If PAR-VALUES replace final strings with these parameter values."
10835 (let* ((port (verilog-sig-name port-st))
10836 (tpl-ass (or (assoc port (car tpl-list))
10837 (verilog-auto-inst-port-map port-st)))
10838 ;; vl-* are documented for user use
10839 (vl-name (verilog-sig-name port-st))
10840 (vl-width (verilog-sig-width port-st))
10841 (vl-modport (verilog-sig-modport port-st))
10842 (vl-mbits (if (verilog-sig-multidim port-st)
10843 (verilog-sig-multidim-string port-st) ""))
10844 (vl-bits (if (or verilog-auto-inst-vector
10845 (not (assoc port vector-skip-list))
10846 (not (equal (verilog-sig-bits port-st)
10847 (verilog-sig-bits (assoc port vector-skip-list)))))
10848 (or (verilog-sig-bits port-st) "")
10849 ""))
10850 (case-fold-search nil)
10851 (check-values par-values)
10852 tpl-net dflt-bits)
10853 ;; Replace parameters in bit-width
10854 (when (and check-values
10855 (not (equal vl-bits "")))
10856 (while check-values
10857 (setq vl-bits (verilog-string-replace-matches
10858 (concat "\\<" (nth 0 (car check-values)) "\\>")
10859 (concat "(" (nth 1 (car check-values)) ")")
10860 t t vl-bits)
10861 vl-mbits (verilog-string-replace-matches
10862 (concat "\\<" (nth 0 (car check-values)) "\\>")
10863 (concat "(" (nth 1 (car check-values)) ")")
10864 t t vl-mbits)
10865 check-values (cdr check-values)))
10866 (setq vl-bits (verilog-simplify-range-expression vl-bits)
10867 vl-mbits (verilog-simplify-range-expression vl-mbits)
10868 vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
10869 ;; Default net value if not found
10870 (setq dflt-bits (if (and (verilog-sig-bits port-st)
10871 (or (verilog-sig-multidim port-st)
10872 (verilog-sig-memory port-st)))
10873 (concat "/*" vl-mbits vl-bits "*/")
10874 (concat vl-bits))
10875 tpl-net (concat port
10876 (if vl-modport (concat "." vl-modport) "")
10877 dflt-bits))
10878 ;; Find template
10879 (cond (tpl-ass ; Template of exact port name
10880 (setq tpl-net (nth 1 tpl-ass)))
10881 ((nth 1 tpl-list) ; Wildcards in template, search them
10882 (let ((wildcards (nth 1 tpl-list)))
10883 (while wildcards
10884 (when (string-match (nth 0 (car wildcards)) port)
10885 (setq tpl-ass (car wildcards) ; so allow @ parsing
10886 tpl-net (replace-match (nth 1 (car wildcards))
10887 t nil port)))
10888 (setq wildcards (cdr wildcards))))))
10889 ;; Parse Templated variable
10890 (when tpl-ass
10891 ;; Evaluate @"(lispcode)"
10892 (when (string-match "@\".*[^\\]\"" tpl-net)
10893 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
10894 (setq tpl-net
10895 (concat
10896 (substring tpl-net 0 (match-beginning 0))
10897 (save-match-data
10898 (let* ((expr (match-string 1 tpl-net))
10899 (value
10900 (progn
10901 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
10902 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
10903 (prin1 (eval (car (read-from-string expr)))
10904 (lambda (_ch) ())))))
10905 (if (numberp value) (setq value (number-to-string value)))
10906 value))
10907 (substring tpl-net (match-end 0))))))
10908 ;; Replace @ and [] magic variables in final output
10909 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
10910 (setq tpl-net (verilog-string-replace-matches "\\[\\]\\[\\]" dflt-bits nil nil tpl-net))
10911 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
10912 ;; Insert it
10913 (indent-to indent-pt)
10914 (insert "." port)
10915 (unless (and verilog-auto-inst-dot-name
10916 (equal port tpl-net))
10917 (indent-to verilog-auto-inst-column)
10918 (insert "(" tpl-net ")"))
10919 (insert ",")
10920 (cond (tpl-ass
10921 (verilog-read-auto-template-hit tpl-ass)
10922 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10923 verilog-auto-inst-column))
10924 ;; verilog-insert requires the complete comment in one call - including the newline
10925 (cond ((equal verilog-auto-inst-template-numbers `lhs)
10926 (verilog-insert " // Templated"
10927 " LHS: " (nth 0 tpl-ass)
10928 "\n"))
10929 (verilog-auto-inst-template-numbers
10930 (verilog-insert " // Templated"
10931 " T" (int-to-string (nth 2 tpl-ass))
10932 " L" (int-to-string (nth 3 tpl-ass))
10933 "\n"))
10934 (t
10935 (verilog-insert " // Templated\n"))))
10936 (for-star
10937 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10938 verilog-auto-inst-column))
10939 (verilog-insert " // Implicit .\*\n")) ;For some reason the . or * must be escaped...
10940 (t
10941 (insert "\n")))))
10942 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
10943 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
10944 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
10945
10946 (defun verilog-auto-inst-port-list (sig-list indent-pt tpl-list tpl-num for-star par-values)
10947 "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
10948 (when verilog-auto-inst-sort
10949 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
10950 (mapc (lambda (port)
10951 (verilog-auto-inst-port port indent-pt
10952 tpl-list tpl-num for-star par-values))
10953 sig-list))
10954
10955 (defun verilog-auto-inst-first ()
10956 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
10957 ;; Do we need a trailing comma?
10958 ;; There maybe an ifdef or something similar before us. What a mess. Thus
10959 ;; to avoid trouble we only insert on preceding ) or *.
10960 ;; Insert first port on new line
10961 (insert "\n") ;; Must insert before search, so point will move forward if insert comma
10962 (save-excursion
10963 (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
10964 (when (looking-at ")\\|\\*") ;; Generally don't insert, unless we are fairly sure
10965 (forward-char 1)
10966 (insert ","))))
10967
10968 (defun verilog-auto-star ()
10969 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
10970
10971 If `verilog-auto-star-expand' is set, .* pins are treated if they were
10972 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
10973 will also ignore any .* that are not last in your pin list (this prevents
10974 it from deleting pins following the .* when it expands the AUTOINST.)
10975
10976 On writing your file, unless `verilog-auto-star-save' is set, any
10977 non-templated expanded pins will be removed. You may do this at any time
10978 with \\[verilog-delete-auto-star-implicit].
10979
10980 If you are converting a module to use .* for the first time, you may wish
10981 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
10982
10983 See `verilog-auto-inst' for examples, templates, and more information."
10984 (when (verilog-auto-star-safe)
10985 (verilog-auto-inst)))
10986
10987 (defun verilog-auto-inst ()
10988 "Expand AUTOINST statements, as part of \\[verilog-auto].
10989 Replace the pin connections to an instantiation or interface
10990 declaration with ones automatically derived from the module or
10991 interface header of the instantiated item.
10992
10993 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
10994 and delete them before saving unless `verilog-auto-star-save' is set.
10995 See `verilog-auto-star' for more information.
10996
10997 The pins are printed in declaration order or alphabetically,
10998 based on the `verilog-auto-inst-sort' variable.
10999
11000 Limitations:
11001 Module names must be resolvable to filenames by adding a
11002 `verilog-library-extensions', and being found in the same directory, or
11003 by changing the variable `verilog-library-flags' or
11004 `verilog-library-directories'. Macros `modname are translated through the
11005 vh-{name} Emacs variable, if that is not found, it just ignores the `.
11006
11007 In templates you must have one signal per line, ending in a ), or ));,
11008 and have proper () nesting, including a final ); to end the template.
11009
11010 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11011
11012 SystemVerilog multidimensional input/output has only experimental support.
11013
11014 SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
11015
11016 Parameters referenced by the instantiation will remain symbolic, unless
11017 `verilog-auto-inst-param-value' is set.
11018
11019 Gate primitives (and/or) may have AUTOINST for the purpose of
11020 AUTOWIRE declarations, etc. Gates are the only case when
11021 position based connections are passed.
11022
11023 The array part of arrayed instances are ignored; this may
11024 result in undesirable default AUTOINST connections; use a
11025 template instead.
11026
11027 For example, first take the submodule InstModule.v:
11028
11029 module InstModule (o,i);
11030 output [31:0] o;
11031 input i;
11032 wire [31:0] o = {32{i}};
11033 endmodule
11034
11035 This is then used in an upper level module:
11036
11037 module ExampInst (o,i);
11038 output o;
11039 input i;
11040 InstModule instName
11041 (/*AUTOINST*/);
11042 endmodule
11043
11044 Typing \\[verilog-auto] will make this into:
11045
11046 module ExampInst (o,i);
11047 output o;
11048 input i;
11049 InstModule instName
11050 (/*AUTOINST*/
11051 // Outputs
11052 .ov (ov[31:0]),
11053 // Inputs
11054 .i (i));
11055 endmodule
11056
11057 Where the list of inputs and outputs came from the inst module.
11058 \f
11059 Exceptions:
11060
11061 Unless you are instantiating a module multiple times, or the module is
11062 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
11063 It just makes for unmaintainable code. To sanitize signal names, try
11064 vrename from URL `http://www.veripool.org'.
11065
11066 When you need to violate this suggestion there are two ways to list
11067 exceptions, placing them before the AUTOINST, or using templates.
11068
11069 Any ports defined before the /*AUTOINST*/ are not included in the list of
11070 automatics. This is similar to making a template as described below, but
11071 is restricted to simple connections just like you normally make. Also note
11072 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
11073 you have the appropriate // Input or // Output comment, and exactly the
11074 same line formatting as AUTOINST itself uses.
11075
11076 InstModule instName
11077 (// Inputs
11078 .i (my_i_dont_mess_with_it),
11079 /*AUTOINST*/
11080 // Outputs
11081 .ov (ov[31:0]));
11082
11083 \f
11084 Templates:
11085
11086 For multiple instantiations based upon a single template, create a
11087 commented out template:
11088
11089 /* InstModule AUTO_TEMPLATE (
11090 .sig3 (sigz[]),
11091 );
11092 */
11093
11094 Templates go ABOVE the instantiation(s). When an instantiation is
11095 expanded `verilog-mode' simply searches up for the closest template.
11096 Thus you can have multiple templates for the same module, just alternate
11097 between the template for an instantiation and the instantiation itself.
11098 (For backward compatibility if no template is found above, it
11099 will also look below, but do not use this behavior in new designs.)
11100
11101 The module name must be the same as the name of the module in the
11102 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
11103 words and capitalized. Only signals that must be different for each
11104 instantiation need to be listed.
11105
11106 Inside a template, a [] in a connection name (with nothing else
11107 inside the brackets) will be replaced by the same bus subscript
11108 as it is being connected to, or the [] will be removed if it is
11109 a single bit signal.
11110
11111 Inside a template, a [][] in a connection name will behave
11112 similarly to a [] for scalar or single-dimensional connection;
11113 for a multidimensional connection it will print a comment
11114 similar to that printed when a template is not used. Generally
11115 it is a good idea to do this for all connections in a template,
11116 as then they will work for any width signal, and with AUTOWIRE.
11117 See PTL_BUS becoming PTL_BUSNEW below.
11118
11119 Inside a template, a [] in a connection name (with nothing else inside
11120 the brackets) will be replaced by the same bus subscript as it is being
11121 connected to, or the [] will be removed if it is a single bit signal.
11122 Generally it is a good idea to do this for all connections in a template,
11123 as then they will work for any width signal, and with AUTOWIRE. See
11124 PTL_BUS becoming PTL_BUSNEW below.
11125
11126 If you have a complicated template, set `verilog-auto-inst-template-numbers'
11127 to see which regexps are matching. Don't leave that mode set after
11128 debugging is completed though, it will result in lots of extra differences
11129 and merge conflicts.
11130
11131 Setting `verilog-auto-template-warn-unused' will report errors
11132 if any template lines are unused.
11133
11134 For example:
11135
11136 /* InstModule AUTO_TEMPLATE (
11137 .ptl_bus (ptl_busnew[]),
11138 );
11139 */
11140 InstModule ms2m (/*AUTOINST*/);
11141
11142 Typing \\[verilog-auto] will make this into:
11143
11144 InstModule ms2m (/*AUTOINST*/
11145 // Outputs
11146 .NotInTemplate (NotInTemplate),
11147 .ptl_bus (ptl_busnew[3:0]), // Templated
11148 ....
11149
11150 \f
11151 Multiple Module Templates:
11152
11153 The same template lines can be applied to multiple modules with
11154 the syntax as follows:
11155
11156 /* InstModuleA AUTO_TEMPLATE
11157 InstModuleB AUTO_TEMPLATE
11158 InstModuleC AUTO_TEMPLATE
11159 InstModuleD AUTO_TEMPLATE (
11160 .ptl_bus (ptl_busnew[]),
11161 );
11162 */
11163
11164 Note there is only one AUTO_TEMPLATE opening parenthesis.
11165 \f
11166 @ Templates:
11167
11168 It is common to instantiate a cell multiple times, so templates make it
11169 trivial to substitute part of the cell name into the connection name.
11170
11171 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
11172 .sig1 (sigx[@]),
11173 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
11174 );
11175 */
11176
11177 If no regular expression is provided immediately after the AUTO_TEMPLATE
11178 keyword, then the @ character in any connection names will be replaced
11179 with the instantiation number; the first digits found in the cell's
11180 instantiation name.
11181
11182 If a regular expression is provided, the @ character will be replaced
11183 with the first \(\) grouping that matches against the cell name. Using a
11184 regexp of \"\\([0-9]+\\)\" provides identical values for @ as when no
11185 regexp is provided. If you use multiple layers of parenthesis,
11186 \"test\\([^0-9]+\\)_\\([0-9]+\\)\" would replace @ with non-number
11187 characters after test and before _, whereas
11188 \"\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)\" would replace @ with the entire
11189 match.
11190
11191 For example:
11192
11193 /* InstModule AUTO_TEMPLATE (
11194 .ptl_mapvalidx (ptl_mapvalid[@]),
11195 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
11196 );
11197 */
11198 InstModule ms2m (/*AUTOINST*/);
11199
11200 Typing \\[verilog-auto] will make this into:
11201
11202 InstModule ms2m (/*AUTOINST*/
11203 // Outputs
11204 .ptl_mapvalidx (ptl_mapvalid[2]),
11205 .ptl_mapvalidp1x (ptl_mapvalid[3]));
11206
11207 Note the @ character was replaced with the 2 from \"ms2m\".
11208
11209 Alternatively, using a regular expression for @:
11210
11211 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
11212 .ptl_mapvalidx (@_ptl_mapvalid),
11213 .ptl_mapvalidp1x (ptl_mapvalid_@),
11214 );
11215 */
11216 InstModule ms2_FOO (/*AUTOINST*/);
11217 InstModule ms2_BAR (/*AUTOINST*/);
11218
11219 Typing \\[verilog-auto] will make this into:
11220
11221 InstModule ms2_FOO (/*AUTOINST*/
11222 // Outputs
11223 .ptl_mapvalidx (FOO_ptl_mapvalid),
11224 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
11225 InstModule ms2_BAR (/*AUTOINST*/
11226 // Outputs
11227 .ptl_mapvalidx (BAR_ptl_mapvalid),
11228 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
11229
11230 \f
11231 Regexp Templates:
11232
11233 A template entry of the form
11234
11235 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
11236
11237 will apply an Emacs style regular expression search for any port beginning
11238 in pci_req followed by numbers and ending in _l and connecting that to
11239 the pci_req_jtag_[] net, with the bus subscript coming from what matches
11240 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
11241
11242 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
11243 does the same thing. (Note a @ in the connection/replacement text is
11244 completely different -- still use \\1 there!) Thus this is the same as
11245 the above template:
11246
11247 .pci_req@_l (pci_req_jtag_[\\1]),
11248
11249 Here's another example to remove the _l, useful when naming conventions
11250 specify _ alone to mean active low. Note the use of [] to keep the bus
11251 subscript:
11252
11253 .\\(.*\\)_l (\\1_[]),
11254 \f
11255 Lisp Templates:
11256
11257 First any regular expression template is expanded.
11258
11259 If the syntax @\"( ... )\" is found in a connection, the expression in
11260 quotes will be evaluated as a Lisp expression, with @ replaced by the
11261 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
11262 4 into the brackets. Quote all double-quotes inside the expression with
11263 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
11264 regexp template backslash the backslash quote (\\\\\"...\\\\\").
11265
11266 There are special variables defined that are useful in these
11267 Lisp functions:
11268
11269 vl-name Name portion of the input/output port.
11270 vl-bits Bus bits portion of the input/output port ('[2:0]').
11271 vl-mbits Multidimensional array bits for port ('[2:0][3:0]').
11272 vl-width Width of the input/output port ('3' for [2:0]).
11273 May be a (...) expression if bits isn't a constant.
11274 vl-dir Direction of the pin input/output/inout/interface.
11275 vl-modport The modport, if an interface with a modport.
11276 vl-cell-type Module name/type of the cell ('InstModule').
11277 vl-cell-name Instance name of the cell ('instName').
11278
11279 Normal Lisp variables may be used in expressions. See
11280 `verilog-read-defines' which can set vh-{definename} variables for use
11281 here. Also, any comments of the form:
11282
11283 /*AUTO_LISP(setq foo 1)*/
11284
11285 will evaluate any Lisp expression inside the parenthesis between the
11286 beginning of the buffer and the point of the AUTOINST. This allows
11287 functions to be defined or variables to be changed between instantiations.
11288 (See also `verilog-auto-insert-lisp' if you want the output from your
11289 lisp function to be inserted.)
11290
11291 Note that when using lisp expressions errors may occur when @ is not a
11292 number; you may need to use the standard Emacs Lisp functions
11293 `number-to-string' and `string-to-number'.
11294
11295 After the evaluation is completed, @ substitution and [] substitution
11296 occur.
11297
11298 For more information see the \\[verilog-faq] and forums at URL
11299 `http://www.veripool.org'."
11300 (save-excursion
11301 ;; Find beginning
11302 (let* ((pt (point))
11303 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
11304 (indent-pt (save-excursion (verilog-backward-open-paren)
11305 (1+ (current-column))))
11306 (verilog-auto-inst-column (max verilog-auto-inst-column
11307 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11308 (modi (verilog-modi-current))
11309 (moddecls (verilog-modi-get-decls modi))
11310 (vector-skip-list (unless verilog-auto-inst-vector
11311 (verilog-decls-get-signals moddecls)))
11312 submod submodi submoddecls
11313 inst skip-pins tpl-list tpl-num did-first par-values)
11314
11315 ;; Find module name that is instantiated
11316 (setq submod (verilog-read-inst-module)
11317 inst (verilog-read-inst-name)
11318 vl-cell-type submod
11319 vl-cell-name inst
11320 skip-pins (aref (verilog-read-inst-pins) 0))
11321
11322 ;; Parse any AUTO_LISP() before here
11323 (verilog-read-auto-lisp (point-min) pt)
11324
11325 ;; Read parameters (after AUTO_LISP)
11326 (setq par-values (and verilog-auto-inst-param-value
11327 (verilog-read-inst-param-value)))
11328
11329 ;; Lookup position, etc of submodule
11330 ;; Note this may raise an error
11331 (when (and (not (member submod verilog-gate-keywords))
11332 (setq submodi (verilog-modi-lookup submod t)))
11333 (setq submoddecls (verilog-modi-get-decls submodi))
11334 ;; If there's a number in the instantiation, it may be an argument to the
11335 ;; automatic variable instantiation program.
11336 (let* ((tpl-info (verilog-read-auto-template submod))
11337 (tpl-regexp (aref tpl-info 0)))
11338 (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
11339 (match-string 1 inst)
11340 "")
11341 tpl-list (aref tpl-info 1)))
11342 ;; Find submodule's signals and dump
11343 (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
11344 (verilog-signals-not-in
11345 (verilog-decls-get-vars submoddecls)
11346 skip-pins)))
11347 (vl-dir "interfaced"))
11348 (when (and sig-list
11349 verilog-auto-inst-interfaced-ports)
11350 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11351 ;; Note these are searched for in verilog-read-sub-decls.
11352 (verilog-insert-indent "// Interfaced\n")
11353 (verilog-auto-inst-port-list sig-list indent-pt
11354 tpl-list tpl-num for-star par-values)))
11355 (let ((sig-list (verilog-signals-not-in
11356 (verilog-decls-get-interfaces submoddecls)
11357 skip-pins))
11358 (vl-dir "interface"))
11359 (when sig-list
11360 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11361 ;; Note these are searched for in verilog-read-sub-decls.
11362 (verilog-insert-indent "// Interfaces\n")
11363 (verilog-auto-inst-port-list sig-list indent-pt
11364 tpl-list tpl-num for-star par-values)))
11365 (let ((sig-list (verilog-signals-not-in
11366 (verilog-decls-get-outputs submoddecls)
11367 skip-pins))
11368 (vl-dir "output"))
11369 (when sig-list
11370 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11371 (verilog-insert-indent "// Outputs\n")
11372 (verilog-auto-inst-port-list sig-list indent-pt
11373 tpl-list tpl-num for-star par-values)))
11374 (let ((sig-list (verilog-signals-not-in
11375 (verilog-decls-get-inouts submoddecls)
11376 skip-pins))
11377 (vl-dir "inout"))
11378 (when sig-list
11379 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11380 (verilog-insert-indent "// Inouts\n")
11381 (verilog-auto-inst-port-list sig-list indent-pt
11382 tpl-list tpl-num for-star par-values)))
11383 (let ((sig-list (verilog-signals-not-in
11384 (verilog-decls-get-inputs submoddecls)
11385 skip-pins))
11386 (vl-dir "input"))
11387 (when sig-list
11388 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11389 (verilog-insert-indent "// Inputs\n")
11390 (verilog-auto-inst-port-list sig-list indent-pt
11391 tpl-list tpl-num for-star par-values)))
11392 ;; Kill extra semi
11393 (save-excursion
11394 (cond (did-first
11395 (re-search-backward "," pt t)
11396 (delete-char 1)
11397 (insert ");")
11398 (search-forward "\n") ;; Added by inst-port
11399 (delete-char -1)
11400 (if (search-forward ")" nil t) ;; From user, moved up a line
11401 (delete-char -1))
11402 (if (search-forward ";" nil t) ;; Don't error if user had syntax error and forgot it
11403 (delete-char -1)))))))))
11404
11405 (defun verilog-auto-inst-param ()
11406 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
11407 Replace the parameter connections to an instantiation with ones
11408 automatically derived from the module header of the instantiated netlist.
11409
11410 See \\[verilog-auto-inst] for limitations, and templates to customize the
11411 output.
11412
11413 For example, first take the submodule InstModule.v:
11414
11415 module InstModule (o,i);
11416 parameter PAR;
11417 endmodule
11418
11419 This is then used in an upper level module:
11420
11421 module ExampInst (o,i);
11422 parameter PAR;
11423 InstModule #(/*AUTOINSTPARAM*/)
11424 instName (/*AUTOINST*/);
11425 endmodule
11426
11427 Typing \\[verilog-auto] will make this into:
11428
11429 module ExampInst (o,i);
11430 output o;
11431 input i;
11432 InstModule #(/*AUTOINSTPARAM*/
11433 // Parameters
11434 .PAR (PAR));
11435 instName (/*AUTOINST*/);
11436 endmodule
11437
11438 Where the list of parameter connections come from the inst module.
11439 \f
11440 Templates:
11441
11442 You can customize the parameter connections using AUTO_TEMPLATEs,
11443 just as you would with \\[verilog-auto-inst]."
11444 (save-excursion
11445 ;; Find beginning
11446 (let* ((pt (point))
11447 (indent-pt (save-excursion (verilog-backward-open-paren)
11448 (1+ (current-column))))
11449 (verilog-auto-inst-column (max verilog-auto-inst-column
11450 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11451 (modi (verilog-modi-current))
11452 (moddecls (verilog-modi-get-decls modi))
11453 (vector-skip-list (unless verilog-auto-inst-vector
11454 (verilog-decls-get-signals moddecls)))
11455 submod submodi submoddecls
11456 inst skip-pins tpl-list tpl-num did-first)
11457 ;; Find module name that is instantiated
11458 (setq submod (save-excursion
11459 ;; Get to the point where AUTOINST normally is to read the module
11460 (verilog-re-search-forward-quick "[(;]" nil nil)
11461 (verilog-read-inst-module))
11462 inst (save-excursion
11463 ;; Get to the point where AUTOINST normally is to read the module
11464 (verilog-re-search-forward-quick "[(;]" nil nil)
11465 (verilog-read-inst-name))
11466 vl-cell-type submod
11467 vl-cell-name inst
11468 skip-pins (aref (verilog-read-inst-pins) 0))
11469
11470 ;; Parse any AUTO_LISP() before here
11471 (verilog-read-auto-lisp (point-min) pt)
11472
11473 ;; Lookup position, etc of submodule
11474 ;; Note this may raise an error
11475 (when (setq submodi (verilog-modi-lookup submod t))
11476 (setq submoddecls (verilog-modi-get-decls submodi))
11477 ;; If there's a number in the instantiation, it may be an argument to the
11478 ;; automatic variable instantiation program.
11479 (let* ((tpl-info (verilog-read-auto-template submod))
11480 (tpl-regexp (aref tpl-info 0)))
11481 (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
11482 (match-string 1 inst)
11483 "")
11484 tpl-list (aref tpl-info 1)))
11485 ;; Find submodule's signals and dump
11486 (let ((sig-list (verilog-signals-not-in
11487 (verilog-decls-get-gparams submoddecls)
11488 skip-pins))
11489 (vl-dir "parameter"))
11490 (when sig-list
11491 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11492 ;; Note these are searched for in verilog-read-sub-decls.
11493 (verilog-insert-indent "// Parameters\n")
11494 (verilog-auto-inst-port-list sig-list indent-pt
11495 tpl-list tpl-num nil nil)))
11496 ;; Kill extra semi
11497 (save-excursion
11498 (cond (did-first
11499 (re-search-backward "," pt t)
11500 (delete-char 1)
11501 (insert ")")
11502 (search-forward "\n") ;; Added by inst-port
11503 (delete-char -1)
11504 (if (search-forward ")" nil t) ;; From user, moved up a line
11505 (delete-char -1)))))))))
11506
11507 (defun verilog-auto-reg ()
11508 "Expand AUTOREG statements, as part of \\[verilog-auto].
11509 Make reg statements for any output that isn't already declared,
11510 and isn't a wire output from a block. `verilog-auto-wire-type'
11511 may be used to change the datatype of the declarations.
11512
11513 Limitations:
11514 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11515
11516 This does NOT work on memories, declare those yourself.
11517
11518 An example:
11519
11520 module ExampReg (o,i);
11521 output o;
11522 input i;
11523 /*AUTOREG*/
11524 always o = i;
11525 endmodule
11526
11527 Typing \\[verilog-auto] will make this into:
11528
11529 module ExampReg (o,i);
11530 output o;
11531 input i;
11532 /*AUTOREG*/
11533 // Beginning of automatic regs (for this module's undeclared outputs)
11534 reg o;
11535 // End of automatics
11536 always o = i;
11537 endmodule"
11538 (save-excursion
11539 ;; Point must be at insertion point.
11540 (let* ((indent-pt (current-indentation))
11541 (modi (verilog-modi-current))
11542 (moddecls (verilog-modi-get-decls modi))
11543 (modsubdecls (verilog-modi-get-sub-decls modi))
11544 (sig-list (verilog-signals-not-in
11545 (verilog-decls-get-outputs moddecls)
11546 (append (verilog-signals-with ;; ignore typed signals
11547 'verilog-sig-type
11548 (verilog-decls-get-outputs moddecls))
11549 (verilog-decls-get-vars moddecls)
11550 (verilog-decls-get-assigns moddecls)
11551 (verilog-decls-get-consts moddecls)
11552 (verilog-decls-get-gparams moddecls)
11553 (verilog-subdecls-get-interfaced modsubdecls)
11554 (verilog-subdecls-get-outputs modsubdecls)
11555 (verilog-subdecls-get-inouts modsubdecls)))))
11556 (when sig-list
11557 (verilog-forward-or-insert-line)
11558 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
11559 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11560 (verilog-insert-indent "// End of automatics\n")))))
11561
11562 (defun verilog-auto-reg-input ()
11563 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
11564 Make reg statements instantiation inputs that aren't already declared.
11565 This is useful for making a top level shell for testing the module that is
11566 to be instantiated.
11567
11568 Limitations:
11569 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
11570
11571 This does NOT work on memories, declare those yourself.
11572
11573 An example (see `verilog-auto-inst' for what else is going on here):
11574
11575 module ExampRegInput (o,i);
11576 output o;
11577 input i;
11578 /*AUTOREGINPUT*/
11579 InstModule instName
11580 (/*AUTOINST*/);
11581 endmodule
11582
11583 Typing \\[verilog-auto] will make this into:
11584
11585 module ExampRegInput (o,i);
11586 output o;
11587 input i;
11588 /*AUTOREGINPUT*/
11589 // Beginning of automatic reg inputs (for undeclared ...
11590 reg [31:0] iv; // From inst of inst.v
11591 // End of automatics
11592 InstModule instName
11593 (/*AUTOINST*/
11594 // Outputs
11595 .o (o[31:0]),
11596 // Inputs
11597 .iv (iv));
11598 endmodule"
11599 (save-excursion
11600 ;; Point must be at insertion point.
11601 (let* ((indent-pt (current-indentation))
11602 (modi (verilog-modi-current))
11603 (moddecls (verilog-modi-get-decls modi))
11604 (modsubdecls (verilog-modi-get-sub-decls modi))
11605 (sig-list (verilog-signals-combine-bus
11606 (verilog-signals-not-in
11607 (append (verilog-subdecls-get-inputs modsubdecls)
11608 (verilog-subdecls-get-inouts modsubdecls))
11609 (append (verilog-decls-get-signals moddecls)
11610 (verilog-decls-get-assigns moddecls))))))
11611 (when sig-list
11612 (verilog-forward-or-insert-line)
11613 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
11614 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11615 (verilog-insert-indent "// End of automatics\n")))))
11616
11617 (defun verilog-auto-logic-setup ()
11618 "Prepare variables due to AUTOLOGIC."
11619 (unless verilog-auto-wire-type
11620 (set (make-local-variable 'verilog-auto-wire-type)
11621 "logic")))
11622
11623 (defun verilog-auto-logic ()
11624 "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
11625 Make wire statements using the SystemVerilog logic keyword.
11626 This is currently equivalent to:
11627
11628 /*AUTOWIRE*/
11629
11630 with the below at the bottom of the file
11631
11632 // Local Variables:
11633 // verilog-auto-logic-type:\"logic\"
11634 // End:
11635
11636 In the future AUTOLOGIC may declare additional identifiers,
11637 while AUTOWIRE will not."
11638 (save-excursion
11639 (verilog-auto-logic-setup)
11640 (verilog-auto-wire)))
11641
11642 (defun verilog-auto-wire ()
11643 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
11644 Make wire statements for instantiations outputs that aren't
11645 already declared. `verilog-auto-wire-type' may be used to change
11646 the datatype of the declarations.
11647
11648 Limitations:
11649 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
11650 and all buses must have widths, such as those from AUTOINST, or using []
11651 in AUTO_TEMPLATEs.
11652
11653 This does NOT work on memories or SystemVerilog .name connections,
11654 declare those yourself.
11655
11656 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
11657 determine how to bus together. This occurs when you have ports with
11658 non-numeric or non-sequential bus subscripts. If Verilog mode
11659 mis-guessed, you'll have to declare them yourself.
11660
11661 An example (see `verilog-auto-inst' for what else is going on here):
11662
11663 module ExampWire (o,i);
11664 output o;
11665 input i;
11666 /*AUTOWIRE*/
11667 InstModule instName
11668 (/*AUTOINST*/);
11669 endmodule
11670
11671 Typing \\[verilog-auto] will make this into:
11672
11673 module ExampWire (o,i);
11674 output o;
11675 input i;
11676 /*AUTOWIRE*/
11677 // Beginning of automatic wires
11678 wire [31:0] ov; // From inst of inst.v
11679 // End of automatics
11680 InstModule instName
11681 (/*AUTOINST*/
11682 // Outputs
11683 .ov (ov[31:0]),
11684 // Inputs
11685 .i (i));
11686 wire o = | ov;
11687 endmodule"
11688 (save-excursion
11689 ;; Point must be at insertion point.
11690 (let* ((indent-pt (current-indentation))
11691 (modi (verilog-modi-current))
11692 (moddecls (verilog-modi-get-decls modi))
11693 (modsubdecls (verilog-modi-get-sub-decls modi))
11694 (sig-list (verilog-signals-combine-bus
11695 (verilog-signals-not-in
11696 (append (verilog-subdecls-get-outputs modsubdecls)
11697 (verilog-subdecls-get-inouts modsubdecls))
11698 (verilog-decls-get-signals moddecls)))))
11699 (when sig-list
11700 (verilog-forward-or-insert-line)
11701 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
11702 (verilog-insert-definition modi sig-list "wire" indent-pt nil)
11703 (verilog-insert-indent "// End of automatics\n")
11704 ;; We used to optionally call verilog-pretty-declarations and
11705 ;; verilog-pretty-expr here, but it's too slow on huge modules,
11706 ;; plus makes everyone's module change. Finally those call
11707 ;; syntax-ppss which is broken when change hooks are disabled.
11708 ))))
11709
11710 (defun verilog-auto-output ()
11711 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
11712 Make output statements for any output signal from an /*AUTOINST*/ that
11713 isn't an input to another AUTOINST. This is useful for modules which
11714 only instantiate other modules.
11715
11716 Limitations:
11717 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11718
11719 If placed inside the parenthesis of a module declaration, it creates
11720 Verilog 2001 style, else uses Verilog 1995 style.
11721
11722 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11723 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11724
11725 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11726
11727 Signals matching `verilog-auto-output-ignore-regexp' are not included.
11728
11729 An example (see `verilog-auto-inst' for what else is going on here):
11730
11731 module ExampOutput (ov,i);
11732 input i;
11733 /*AUTOOUTPUT*/
11734 InstModule instName
11735 (/*AUTOINST*/);
11736 endmodule
11737
11738 Typing \\[verilog-auto] will make this into:
11739
11740 module ExampOutput (ov,i);
11741 input i;
11742 /*AUTOOUTPUT*/
11743 // Beginning of automatic outputs (from unused autoinst outputs)
11744 output [31:0] ov; // From inst of inst.v
11745 // End of automatics
11746 InstModule instName
11747 (/*AUTOINST*/
11748 // Outputs
11749 .ov (ov[31:0]),
11750 // Inputs
11751 .i (i));
11752 endmodule
11753
11754 You may also provide an optional regular expression, in which case only
11755 signals matching the regular expression will be included. For example the
11756 same expansion will result from only extracting outputs starting with ov:
11757
11758 /*AUTOOUTPUT(\"^ov\")*/"
11759 (save-excursion
11760 ;; Point must be at insertion point.
11761 (let* ((indent-pt (current-indentation))
11762 (params (verilog-read-auto-params 0 1))
11763 (regexp (nth 0 params))
11764 (v2k (verilog-in-paren-quick))
11765 (modi (verilog-modi-current))
11766 (moddecls (verilog-modi-get-decls modi))
11767 (modsubdecls (verilog-modi-get-sub-decls modi))
11768 (sig-list (verilog-signals-not-in
11769 (verilog-subdecls-get-outputs modsubdecls)
11770 (append (verilog-decls-get-outputs moddecls)
11771 (verilog-decls-get-inouts moddecls)
11772 (verilog-decls-get-inputs moddecls)
11773 (verilog-subdecls-get-inputs modsubdecls)
11774 (verilog-subdecls-get-inouts modsubdecls)))))
11775 (when regexp
11776 (setq sig-list (verilog-signals-matching-regexp
11777 sig-list regexp)))
11778 (setq sig-list (verilog-signals-not-matching-regexp
11779 sig-list verilog-auto-output-ignore-regexp))
11780 (verilog-forward-or-insert-line)
11781 (when v2k (verilog-repair-open-comma))
11782 (when sig-list
11783 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
11784 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
11785 (verilog-insert-indent "// End of automatics\n"))
11786 (when v2k (verilog-repair-close-comma)))))
11787
11788 (defun verilog-auto-output-every ()
11789 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
11790 Make output statements for any signals that aren't primary inputs or
11791 outputs already. This makes every signal in the design an output. This is
11792 useful to get Synopsys to preserve every signal in the design, since it
11793 won't optimize away the outputs.
11794
11795 An example:
11796
11797 module ExampOutputEvery (o,i,tempa,tempb);
11798 output o;
11799 input i;
11800 /*AUTOOUTPUTEVERY*/
11801 wire tempa = i;
11802 wire tempb = tempa;
11803 wire o = tempb;
11804 endmodule
11805
11806 Typing \\[verilog-auto] will make this into:
11807
11808 module ExampOutputEvery (o,i,tempa,tempb);
11809 output o;
11810 input i;
11811 /*AUTOOUTPUTEVERY*/
11812 // Beginning of automatic outputs (every signal)
11813 output tempb;
11814 output tempa;
11815 // End of automatics
11816 wire tempa = i;
11817 wire tempb = tempa;
11818 wire o = tempb;
11819 endmodule"
11820 (save-excursion
11821 ;;Point must be at insertion point
11822 (let* ((indent-pt (current-indentation))
11823 (v2k (verilog-in-paren-quick))
11824 (modi (verilog-modi-current))
11825 (moddecls (verilog-modi-get-decls modi))
11826 (sig-list (verilog-signals-combine-bus
11827 (verilog-signals-not-in
11828 (verilog-decls-get-signals moddecls)
11829 (verilog-decls-get-ports moddecls)))))
11830 (verilog-forward-or-insert-line)
11831 (when v2k (verilog-repair-open-comma))
11832 (when sig-list
11833 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
11834 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
11835 (verilog-insert-indent "// End of automatics\n"))
11836 (when v2k (verilog-repair-close-comma)))))
11837
11838 (defun verilog-auto-input ()
11839 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
11840 Make input statements for any input signal into an /*AUTOINST*/ that
11841 isn't declared elsewhere inside the module. This is useful for modules which
11842 only instantiate other modules.
11843
11844 Limitations:
11845 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11846
11847 If placed inside the parenthesis of a module declaration, it creates
11848 Verilog 2001 style, else uses Verilog 1995 style.
11849
11850 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11851 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11852
11853 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11854
11855 Signals matching `verilog-auto-input-ignore-regexp' are not included.
11856
11857 An example (see `verilog-auto-inst' for what else is going on here):
11858
11859 module ExampInput (ov,i);
11860 output [31:0] ov;
11861 /*AUTOINPUT*/
11862 InstModule instName
11863 (/*AUTOINST*/);
11864 endmodule
11865
11866 Typing \\[verilog-auto] will make this into:
11867
11868 module ExampInput (ov,i);
11869 output [31:0] ov;
11870 /*AUTOINPUT*/
11871 // Beginning of automatic inputs (from unused autoinst inputs)
11872 input i; // From inst of inst.v
11873 // End of automatics
11874 InstModule instName
11875 (/*AUTOINST*/
11876 // Outputs
11877 .ov (ov[31:0]),
11878 // Inputs
11879 .i (i));
11880 endmodule
11881
11882 You may also provide an optional regular expression, in which case only
11883 signals matching the regular expression will be included. For example the
11884 same expansion will result from only extracting inputs starting with i:
11885
11886 /*AUTOINPUT(\"^i\")*/"
11887 (save-excursion
11888 (let* ((indent-pt (current-indentation))
11889 (params (verilog-read-auto-params 0 1))
11890 (regexp (nth 0 params))
11891 (v2k (verilog-in-paren-quick))
11892 (modi (verilog-modi-current))
11893 (moddecls (verilog-modi-get-decls modi))
11894 (modsubdecls (verilog-modi-get-sub-decls modi))
11895 (sig-list (verilog-signals-not-in
11896 (verilog-subdecls-get-inputs modsubdecls)
11897 (append (verilog-decls-get-inputs moddecls)
11898 (verilog-decls-get-inouts moddecls)
11899 (verilog-decls-get-outputs moddecls)
11900 (verilog-decls-get-vars moddecls)
11901 (verilog-decls-get-consts moddecls)
11902 (verilog-decls-get-gparams moddecls)
11903 (verilog-subdecls-get-interfaced modsubdecls)
11904 (verilog-subdecls-get-outputs modsubdecls)
11905 (verilog-subdecls-get-inouts modsubdecls)))))
11906 (when regexp
11907 (setq sig-list (verilog-signals-matching-regexp
11908 sig-list regexp)))
11909 (setq sig-list (verilog-signals-not-matching-regexp
11910 sig-list verilog-auto-input-ignore-regexp))
11911 (verilog-forward-or-insert-line)
11912 (when v2k (verilog-repair-open-comma))
11913 (when sig-list
11914 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
11915 (verilog-insert-definition modi sig-list "input" indent-pt v2k)
11916 (verilog-insert-indent "// End of automatics\n"))
11917 (when v2k (verilog-repair-close-comma)))))
11918
11919 (defun verilog-auto-inout ()
11920 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
11921 Make inout statements for any inout signal in an /*AUTOINST*/ that
11922 isn't declared elsewhere inside the module.
11923
11924 Limitations:
11925 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11926
11927 If placed inside the parenthesis of a module declaration, it creates
11928 Verilog 2001 style, else uses Verilog 1995 style.
11929
11930 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11931 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11932
11933 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11934
11935 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
11936
11937 An example (see `verilog-auto-inst' for what else is going on here):
11938
11939 module ExampInout (ov,i);
11940 input i;
11941 /*AUTOINOUT*/
11942 InstModule instName
11943 (/*AUTOINST*/);
11944 endmodule
11945
11946 Typing \\[verilog-auto] will make this into:
11947
11948 module ExampInout (ov,i);
11949 input i;
11950 /*AUTOINOUT*/
11951 // Beginning of automatic inouts (from unused autoinst inouts)
11952 inout [31:0] ov; // From inst of inst.v
11953 // End of automatics
11954 InstModule instName
11955 (/*AUTOINST*/
11956 // Inouts
11957 .ov (ov[31:0]),
11958 // Inputs
11959 .i (i));
11960 endmodule
11961
11962 You may also provide an optional regular expression, in which case only
11963 signals matching the regular expression will be included. For example the
11964 same expansion will result from only extracting inouts starting with i:
11965
11966 /*AUTOINOUT(\"^i\")*/"
11967 (save-excursion
11968 ;; Point must be at insertion point.
11969 (let* ((indent-pt (current-indentation))
11970 (params (verilog-read-auto-params 0 1))
11971 (regexp (nth 0 params))
11972 (v2k (verilog-in-paren-quick))
11973 (modi (verilog-modi-current))
11974 (moddecls (verilog-modi-get-decls modi))
11975 (modsubdecls (verilog-modi-get-sub-decls modi))
11976 (sig-list (verilog-signals-not-in
11977 (verilog-subdecls-get-inouts modsubdecls)
11978 (append (verilog-decls-get-outputs moddecls)
11979 (verilog-decls-get-inouts moddecls)
11980 (verilog-decls-get-inputs moddecls)
11981 (verilog-subdecls-get-inputs modsubdecls)
11982 (verilog-subdecls-get-outputs modsubdecls)))))
11983 (when regexp
11984 (setq sig-list (verilog-signals-matching-regexp
11985 sig-list regexp)))
11986 (setq sig-list (verilog-signals-not-matching-regexp
11987 sig-list verilog-auto-inout-ignore-regexp))
11988 (verilog-forward-or-insert-line)
11989 (when v2k (verilog-repair-open-comma))
11990 (when sig-list
11991 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
11992 (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
11993 (verilog-insert-indent "// End of automatics\n"))
11994 (when v2k (verilog-repair-close-comma)))))
11995
11996 (defun verilog-auto-inout-module (&optional complement all-in)
11997 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
11998 Take input/output/inout statements from the specified module and insert
11999 into the current module. This is useful for making null templates and
12000 shell modules which need to have identical I/O with another module.
12001 Any I/O which are already defined in this module will not be redefined.
12002 For the complement of this function, see `verilog-auto-inout-comp',
12003 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12004
12005 Limitations:
12006 If placed inside the parenthesis of a module declaration, it creates
12007 Verilog 2001 style, else uses Verilog 1995 style.
12008
12009 Concatenation and outputting partial buses is not supported.
12010
12011 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12012
12013 Signals are not inserted in the same order as in the original module,
12014 though they will appear to be in the same order to an AUTOINST
12015 instantiating either module.
12016
12017 Signals declared as \"output reg\" or \"output wire\" etc will
12018 lose the wire/reg declaration so that shell modules may
12019 generate those outputs differently. However, \"output logic\"
12020 is propagated.
12021
12022 An example:
12023
12024 module ExampShell (/*AUTOARG*/);
12025 /*AUTOINOUTMODULE(\"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 /*AUTOINOUTMODULE(\"ExampMain\")*/
12038 // Beginning of automatic in/out/inouts (from specific module)
12039 output o;
12040 inout io;
12041 input i;
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 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
12050
12051 You may also provide an optional second regular expression, in
12052 which case only signals which have that pin direction and data
12053 type will be included. This matches against everything before
12054 the signal name in the declaration, for example against
12055 \"input\" (single bit), \"output logic\" (direction and type) or
12056 \"output [1:0]\" (direction and implicit type). You also
12057 probably want to skip spaces in your regexp.
12058
12059 For example, the below will result in matching the output \"o\"
12060 against the previous example's module:
12061
12062 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/"
12063 (save-excursion
12064 (let* ((params (verilog-read-auto-params 1 3))
12065 (submod (nth 0 params))
12066 (regexp (nth 1 params))
12067 (direction-re (nth 2 params))
12068 submodi)
12069 ;; Lookup position, etc of co-module
12070 ;; Note this may raise an error
12071 (when (setq submodi (verilog-modi-lookup submod t))
12072 (let* ((indent-pt (current-indentation))
12073 (v2k (verilog-in-paren-quick))
12074 (modi (verilog-modi-current))
12075 (moddecls (verilog-modi-get-decls modi))
12076 (submoddecls (verilog-modi-get-decls submodi))
12077 (sig-list-i (verilog-signals-not-in
12078 (cond (all-in
12079 (append
12080 (verilog-decls-get-inputs submoddecls)
12081 (verilog-decls-get-inouts submoddecls)
12082 (verilog-decls-get-outputs submoddecls)))
12083 (complement
12084 (verilog-decls-get-outputs submoddecls))
12085 (t (verilog-decls-get-inputs submoddecls)))
12086 (append (verilog-decls-get-inputs moddecls))))
12087 (sig-list-o (verilog-signals-not-in
12088 (cond (all-in nil)
12089 (complement
12090 (verilog-decls-get-inputs submoddecls))
12091 (t (verilog-decls-get-outputs submoddecls)))
12092 (append (verilog-decls-get-outputs moddecls))))
12093 (sig-list-io (verilog-signals-not-in
12094 (cond (all-in nil)
12095 (t (verilog-decls-get-inouts submoddecls)))
12096 (append (verilog-decls-get-inouts moddecls))))
12097 (sig-list-if (verilog-signals-not-in
12098 (verilog-decls-get-interfaces submoddecls)
12099 (append (verilog-decls-get-interfaces moddecls)))))
12100 (forward-line 1)
12101 (setq sig-list-i (verilog-signals-edit-wire-reg
12102 (verilog-signals-matching-dir-re
12103 (verilog-signals-matching-regexp sig-list-i regexp)
12104 "input" direction-re))
12105 sig-list-o (verilog-signals-edit-wire-reg
12106 (verilog-signals-matching-dir-re
12107 (verilog-signals-matching-regexp sig-list-o regexp)
12108 "output" direction-re))
12109 sig-list-io (verilog-signals-edit-wire-reg
12110 (verilog-signals-matching-dir-re
12111 (verilog-signals-matching-regexp sig-list-io regexp)
12112 "inout" direction-re))
12113 sig-list-if (verilog-signals-matching-dir-re
12114 (verilog-signals-matching-regexp sig-list-if regexp)
12115 "interface" direction-re))
12116 (when v2k (verilog-repair-open-comma))
12117 (when (or sig-list-i sig-list-o sig-list-io sig-list-if)
12118 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
12119 ;; Don't sort them so an upper AUTOINST will match the main module
12120 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12121 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12122 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12123 (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
12124 (verilog-insert-indent "// End of automatics\n"))
12125 (when v2k (verilog-repair-close-comma)))))))
12126
12127 (defun verilog-auto-inout-comp ()
12128 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
12129 Take input/output/inout statements from the specified module and
12130 insert the inverse into the current module (inputs become outputs
12131 and vice-versa.) This is useful for making test and stimulus
12132 modules which need to have complementing I/O with another module.
12133 Any I/O which are already defined in this module will not be
12134 redefined. For the complement of this function, see
12135 `verilog-auto-inout-module'.
12136
12137 Limitations:
12138 If placed inside the parenthesis of a module declaration, it creates
12139 Verilog 2001 style, else uses Verilog 1995 style.
12140
12141 Concatenation and outputting partial buses is not supported.
12142
12143 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12144
12145 Signals are not inserted in the same order as in the original module,
12146 though they will appear to be in the same order to an AUTOINST
12147 instantiating either module.
12148
12149 An example:
12150
12151 module ExampShell (/*AUTOARG*/);
12152 /*AUTOINOUTCOMP(\"ExampMain\")*/
12153 endmodule
12154
12155 module ExampMain (i,o,io);
12156 input i;
12157 output o;
12158 inout io;
12159 endmodule
12160
12161 Typing \\[verilog-auto] will make this into:
12162
12163 module ExampShell (/*AUTOARG*/i,o,io);
12164 /*AUTOINOUTCOMP(\"ExampMain\")*/
12165 // Beginning of automatic in/out/inouts (from specific module)
12166 output i;
12167 inout io;
12168 input o;
12169 // End of automatics
12170 endmodule
12171
12172 You may also provide an optional regular expression, in which case only
12173 signals matching the regular expression will be included. For example the
12174 same expansion will result from only extracting signals starting with i:
12175
12176 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
12177 (verilog-auto-inout-module t nil))
12178
12179 (defun verilog-auto-inout-in ()
12180 "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
12181 Take input/output/inout statements from the specified module and
12182 insert them as all inputs into the current module. This is
12183 useful for making monitor modules which need to see all signals
12184 as inputs based on another module. Any I/O which are already
12185 defined in this module will not be redefined. See also
12186 `verilog-auto-inout-module'.
12187
12188 Limitations:
12189 If placed inside the parenthesis of a module declaration, it creates
12190 Verilog 2001 style, else uses Verilog 1995 style.
12191
12192 Concatenation and outputting partial buses is not supported.
12193
12194 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12195
12196 Signals are not inserted in the same order as in the original module,
12197 though they will appear to be in the same order to an AUTOINST
12198 instantiating either module.
12199
12200 An example:
12201
12202 module ExampShell (/*AUTOARG*/);
12203 /*AUTOINOUTIN(\"ExampMain\")*/
12204 endmodule
12205
12206 module ExampMain (i,o,io);
12207 input i;
12208 output o;
12209 inout io;
12210 endmodule
12211
12212 Typing \\[verilog-auto] will make this into:
12213
12214 module ExampShell (/*AUTOARG*/i,o,io);
12215 /*AUTOINOUTIN(\"ExampMain\")*/
12216 // Beginning of automatic in/out/inouts (from specific module)
12217 input i;
12218 input io;
12219 input o;
12220 // End of automatics
12221 endmodule
12222
12223 You may also provide an optional regular expression, in which case only
12224 signals matching the regular expression will be included. For example the
12225 same expansion will result from only extracting signals starting with i:
12226
12227 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
12228 (verilog-auto-inout-module nil t))
12229
12230 (defun verilog-auto-inout-param ()
12231 "Expand AUTOINOUTPARAM statements, as part of \\[verilog-auto].
12232 Take input/output/inout statements from the specified module and insert
12233 into the current module. This is useful for making null templates and
12234 shell modules which need to have identical I/O with another module.
12235 Any I/O which are already defined in this module will not be redefined.
12236 For the complement of this function, see `verilog-auto-inout-comp',
12237 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12238
12239 Limitations:
12240 If placed inside the parenthesis of a module declaration, it creates
12241 Verilog 2001 style, else uses Verilog 1995 style.
12242
12243 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12244
12245 Parameters are inserted in the same order as in the original module.
12246
12247 Parameters do not have values, which is SystemVerilog 2009 syntax.
12248
12249 An example:
12250
12251 module ExampShell ();
12252 /*AUTOINOUTPARAM(\"ExampMain\")*/
12253 endmodule
12254
12255 module ExampMain ();
12256 parameter PARAM = 22;
12257 endmodule
12258
12259 Typing \\[verilog-auto] will make this into:
12260
12261 module ExampShell (/*AUTOARG*/i,o,io);
12262 /*AUTOINOUTPARAM(\"ExampMain\")*/
12263 // Beginning of automatic parameters (from specific module)
12264 parameter PARAM;
12265 // End of automatics
12266 endmodule
12267
12268 You may also provide an optional regular expression, in which case only
12269 parameters matching the regular expression will be included. For example the
12270 same expansion will result from only extracting parameters starting with i:
12271
12272 /*AUTOINOUTPARAM(\"ExampMain\",\"^i\")*/"
12273 (save-excursion
12274 (let* ((params (verilog-read-auto-params 1 2))
12275 (submod (nth 0 params))
12276 (regexp (nth 1 params))
12277 submodi)
12278 ;; Lookup position, etc of co-module
12279 ;; Note this may raise an error
12280 (when (setq submodi (verilog-modi-lookup submod t))
12281 (let* ((indent-pt (current-indentation))
12282 (v2k (verilog-in-paren-quick))
12283 (modi (verilog-modi-current))
12284 (moddecls (verilog-modi-get-decls modi))
12285 (submoddecls (verilog-modi-get-decls submodi))
12286 (sig-list-p (verilog-signals-not-in
12287 (verilog-decls-get-gparams submoddecls)
12288 (append (verilog-decls-get-gparams moddecls)))))
12289 (forward-line 1)
12290 (setq sig-list-p (verilog-signals-matching-regexp sig-list-p regexp))
12291 (when v2k (verilog-repair-open-comma))
12292 (when sig-list-p
12293 (verilog-insert-indent "// Beginning of automatic parameters (from specific module)\n")
12294 ;; Don't sort them so an upper AUTOINST will match the main module
12295 (verilog-insert-definition modi sig-list-p "parameter" indent-pt v2k t)
12296 (verilog-insert-indent "// End of automatics\n"))
12297 (when v2k (verilog-repair-close-comma)))))))
12298
12299 (defun verilog-auto-inout-modport ()
12300 "Expand AUTOINOUTMODPORT statements, as part of \\[verilog-auto].
12301 Take input/output/inout statements from the specified interface
12302 and modport and insert into the current module. This is useful
12303 for making verification modules that connect to UVM interfaces.
12304
12305 The first parameter is the name of an interface.
12306
12307 The second parameter is a regexp of modports to read from in
12308 that interface.
12309
12310 The optional third parameter is a regular expression, and only
12311 signals matching the regular expression will be included.
12312
12313 Limitations:
12314 If placed inside the parenthesis of a module declaration, it creates
12315 Verilog 2001 style, else uses Verilog 1995 style.
12316
12317 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
12318
12319 As with other autos, any inputs/outputs declared in the module
12320 will suppress the AUTO from redeclaring an inputs/outputs by
12321 the same name.
12322
12323 An example:
12324
12325 interface ExampIf
12326 ( input logic clk );
12327 logic req_val;
12328 logic [7:0] req_dat;
12329 clocking mon_clkblk @(posedge clk);
12330 input req_val;
12331 input req_dat;
12332 endclocking
12333 modport mp(clocking mon_clkblk);
12334 endinterface
12335
12336 module ExampMain
12337 ( input clk,
12338 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12339 // Beginning of automatic in/out/inouts (from modport)
12340 input [7:0] req_dat,
12341 input req_val
12342 // End of automatics
12343 );
12344 /*AUTOASSIGNMODPORT(\"ExampIf\" \"mp\")*/
12345 endmodule
12346
12347 Typing \\[verilog-auto] will make this into:
12348
12349 ...
12350 module ExampMain
12351 ( input clk,
12352 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12353 // Beginning of automatic in/out/inouts (from modport)
12354 input req_dat,
12355 input req_val
12356 // End of automatics
12357 );
12358
12359 If the modport is part of a UVM monitor/driver class, this
12360 creates a wrapper module that may be used to instantiate the
12361 driver/monitor using AUTOINST in the testbench."
12362 (save-excursion
12363 (let* ((params (verilog-read-auto-params 2 3))
12364 (submod (nth 0 params))
12365 (modport-re (nth 1 params))
12366 (regexp (nth 2 params))
12367 direction-re submodi) ;; direction argument not supported until requested
12368 ;; Lookup position, etc of co-module
12369 ;; Note this may raise an error
12370 (when (setq submodi (verilog-modi-lookup submod t))
12371 (let* ((indent-pt (current-indentation))
12372 (v2k (verilog-in-paren-quick))
12373 (modi (verilog-modi-current))
12374 (moddecls (verilog-modi-get-decls modi))
12375 (submoddecls (verilog-modi-get-decls submodi))
12376 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
12377 (sig-list-i (verilog-signals-in ;; Decls doesn't have data types, must resolve
12378 (verilog-decls-get-vars submoddecls)
12379 (verilog-signals-not-in
12380 (verilog-decls-get-inputs submodportdecls)
12381 (append (verilog-decls-get-ports submoddecls)
12382 (verilog-decls-get-ports moddecls)))))
12383 (sig-list-o (verilog-signals-in ;; Decls doesn't have data types, must resolve
12384 (verilog-decls-get-vars submoddecls)
12385 (verilog-signals-not-in
12386 (verilog-decls-get-outputs submodportdecls)
12387 (append (verilog-decls-get-ports submoddecls)
12388 (verilog-decls-get-ports moddecls)))))
12389 (sig-list-io (verilog-signals-in ;; Decls doesn't have data types, must resolve
12390 (verilog-decls-get-vars submoddecls)
12391 (verilog-signals-not-in
12392 (verilog-decls-get-inouts submodportdecls)
12393 (append (verilog-decls-get-ports submoddecls)
12394 (verilog-decls-get-ports moddecls))))))
12395 (forward-line 1)
12396 (setq sig-list-i (verilog-signals-edit-wire-reg
12397 (verilog-signals-matching-dir-re
12398 (verilog-signals-matching-regexp sig-list-i regexp)
12399 "input" direction-re))
12400 sig-list-o (verilog-signals-edit-wire-reg
12401 (verilog-signals-matching-dir-re
12402 (verilog-signals-matching-regexp sig-list-o regexp)
12403 "output" direction-re))
12404 sig-list-io (verilog-signals-edit-wire-reg
12405 (verilog-signals-matching-dir-re
12406 (verilog-signals-matching-regexp sig-list-io regexp)
12407 "inout" direction-re)))
12408 (when v2k (verilog-repair-open-comma))
12409 (when (or sig-list-i sig-list-o sig-list-io)
12410 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from modport)\n")
12411 ;; Don't sort them so an upper AUTOINST will match the main module
12412 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12413 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12414 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12415 (verilog-insert-indent "// End of automatics\n"))
12416 (when v2k (verilog-repair-close-comma)))))))
12417
12418 (defun verilog-auto-insert-lisp ()
12419 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
12420 The Lisp code provided is called, and the Lisp code calls
12421 `insert` to insert text into the current file beginning on the
12422 line after the AUTOINSERTLISP.
12423
12424 See also AUTO_LISP, which takes a Lisp expression and evaluates
12425 it during `verilog-auto-inst' but does not insert any text.
12426
12427 An example:
12428
12429 module ExampInsertLisp;
12430 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
12431 endmodule
12432
12433 // For this example we declare the function in the
12434 // module's file itself. Often you'd define it instead
12435 // in a site-start.el or init file.
12436 /*
12437 Local Variables:
12438 eval:
12439 (defun my-verilog-insert-hello (who)
12440 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
12441 End:
12442 */
12443
12444 Typing \\[verilog-auto] will call my-verilog-insert-hello and
12445 expand the above into:
12446
12447 // Beginning of automatic insert lisp
12448 initial $write(\"hello world\");
12449 // End of automatics
12450
12451 You can also call an external program and insert the returned
12452 text:
12453
12454 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
12455 // Beginning of automatic insert lisp
12456 //hello
12457 // End of automatics"
12458 (save-excursion
12459 ;; Point is at end of /*AUTO...*/
12460 (let* ((indent-pt (current-indentation))
12461 (cmd-end-pt (save-excursion (search-backward ")")
12462 (forward-char)
12463 (point))) ;; Closing paren
12464 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
12465 (backward-sexp 1) ;; Inside comment
12466 (point))) ;; Beginning paren
12467 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
12468 (verilog-forward-or-insert-line)
12469 ;; Some commands don't move point (like insert-file) so we always
12470 ;; add the begin/end comments, then delete it if not needed
12471 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
12472 (verilog-insert-indent "// End of automatics\n")
12473 (forward-line -1)
12474 (eval (read cmd))
12475 (forward-line -1)
12476 (setq verilog-scan-cache-tick nil) ;; Clear cache; inserted unknown text
12477 (verilog-delete-empty-auto-pair))))
12478
12479 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
12480 "Return list of signals for current AUTOSENSE block."
12481 (let* ((sigss (verilog-read-always-signals))
12482 (sig-list (verilog-signals-not-params
12483 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
12484 (append (and (not verilog-auto-sense-include-inputs)
12485 (verilog-alw-get-outputs-delayed sigss))
12486 (and (not verilog-auto-sense-include-inputs)
12487 (verilog-alw-get-outputs-immediate sigss))
12488 (verilog-alw-get-temps sigss)
12489 (verilog-decls-get-consts moddecls)
12490 (verilog-decls-get-gparams moddecls)
12491 presense-sigs)))))
12492 sig-list))
12493
12494 (defun verilog-auto-sense ()
12495 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
12496 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
12497 with one automatically derived from all inputs declared in the always
12498 statement. Signals that are generated within the same always block are NOT
12499 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
12500 Long lines are split based on the `fill-column', see \\[set-fill-column].
12501
12502 Limitations:
12503 Verilog does not allow memories (multidimensional arrays) in sensitivity
12504 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
12505
12506 Constant signals:
12507 AUTOSENSE cannot always determine if a `define is a constant or a signal
12508 (it could be in an include file for example). If a `define or other signal
12509 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
12510 declaration anywhere in the module (parenthesis are required):
12511
12512 /* AUTO_CONSTANT ( `this_is_really_constant_dont_autosense_it ) */
12513
12514 Better yet, use a parameter, which will be understood to be constant
12515 automatically.
12516
12517 OOps!
12518 If AUTOSENSE makes a mistake, please report it. (First try putting
12519 a begin/end after your always!) As a workaround, if a signal that
12520 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
12521 If a signal should be in the sensitivity list wasn't, placing it before
12522 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
12523 autos are updated (or added if it occurs there already).
12524
12525 An example:
12526
12527 always @ (/*AS*/) begin
12528 /* AUTO_CONSTANT (`constant) */
12529 outin = ina | inb | `constant;
12530 out = outin;
12531 end
12532
12533 Typing \\[verilog-auto] will make this into:
12534
12535 always @ (/*AS*/ina or inb) begin
12536 /* AUTO_CONSTANT (`constant) */
12537 outin = ina | inb | `constant;
12538 out = outin;
12539 end
12540
12541 Note in Verilog 2001, you can often get the same result from the new @*
12542 operator. (This was added to the language in part due to AUTOSENSE!)
12543
12544 always @* begin
12545 outin = ina | inb | `constant;
12546 out = outin;
12547 end"
12548 (save-excursion
12549 ;; Find beginning
12550 (let* ((start-pt (save-excursion
12551 (verilog-re-search-backward-quick "(" nil t)
12552 (point)))
12553 (indent-pt (save-excursion
12554 (or (and (goto-char start-pt) (1+ (current-column)))
12555 (current-indentation))))
12556 (modi (verilog-modi-current))
12557 (moddecls (verilog-modi-get-decls modi))
12558 (sig-memories (verilog-signals-memory
12559 (verilog-decls-get-vars moddecls)))
12560 sig-list not-first presense-sigs)
12561 ;; Read signals in always, eliminate outputs from sense list
12562 (setq presense-sigs (verilog-signals-from-signame
12563 (save-excursion
12564 (verilog-read-signals start-pt (point)))))
12565 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
12566 (when sig-memories
12567 (let ((tlen (length sig-list)))
12568 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
12569 (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
12570 (if (and presense-sigs ;; Add a "or" if not "(.... or /*AUTOSENSE*/"
12571 (save-excursion (goto-char (point))
12572 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12573 (verilog-re-search-backward-quick "\\s-" start-pt t)
12574 (while (looking-at "\\s-`endif")
12575 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12576 (verilog-re-search-backward-quick "\\s-" start-pt t))
12577 (not (looking-at "\\s-or\\b"))))
12578 (setq not-first t))
12579 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12580 (while sig-list
12581 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
12582 (insert "\n")
12583 (indent-to indent-pt)
12584 (if not-first (insert "or ")))
12585 (not-first (insert " or ")))
12586 (insert (verilog-sig-name (car sig-list)))
12587 (setq sig-list (cdr sig-list)
12588 not-first t)))))
12589
12590 (defun verilog-auto-reset ()
12591 "Expand AUTORESET statements, as part of \\[verilog-auto].
12592 Replace the /*AUTORESET*/ comment with code to initialize all
12593 registers set elsewhere in the always block.
12594
12595 Limitations:
12596 AUTORESET will not clear memories.
12597
12598 AUTORESET uses <= if the signal has a <= assignment in the block,
12599 else it uses =.
12600
12601 If <= is used, all = assigned variables are ignored if
12602 `verilog-auto-reset-blocking-in-non' is nil; they are presumed
12603 to be temporaries.
12604
12605 /*AUTORESET*/ presumes that any signals mentioned between the previous
12606 begin/case/if statement and the AUTORESET comment are being reset manually
12607 and should not be automatically reset. This includes omitting any signals
12608 used on the right hand side of assignments.
12609
12610 By default, AUTORESET will include the width of the signal in the
12611 autos, SystemVerilog designs may want to change this. To control
12612 this behavior, see `verilog-auto-reset-widths'. In some cases
12613 AUTORESET must use a '0 assignment and it will print NOWIDTH; use
12614 `verilog-auto-reset-widths' unbased to prevent this.
12615
12616 AUTORESET ties signals to deasserted, which is presumed to be zero.
12617 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12618 them to a one.
12619
12620 AUTORESET may try to reset arrays or structures that cannot be
12621 reset by a simple assignment, resulting in compile errors. This
12622 is a feature to be taken as a hint that you need to reset these
12623 signals manually (or put them into a \"`ifdef NEVER signal<=`0;
12624 `endif\" so Verilog-Mode ignores them.)
12625
12626 An example:
12627
12628 always @(posedge clk or negedge reset_l) begin
12629 if (!reset_l) begin
12630 c <= 1;
12631 /*AUTORESET*/
12632 end
12633 else begin
12634 a <= in_a;
12635 b <= in_b;
12636 c <= in_c;
12637 end
12638 end
12639
12640 Typing \\[verilog-auto] will make this into:
12641
12642 always @(posedge core_clk or negedge reset_l) begin
12643 if (!reset_l) begin
12644 c <= 1;
12645 /*AUTORESET*/
12646 // Beginning of autoreset for uninitialized flops
12647 a <= 0;
12648 b = 0; // if `verilog-auto-reset-blocking-in-non' true
12649 // End of automatics
12650 end
12651 else begin
12652 a <= in_a;
12653 b = in_b;
12654 c <= in_c;
12655 end
12656 end"
12657
12658 (interactive)
12659 (save-excursion
12660 ;; Find beginning
12661 (let* ((indent-pt (current-indentation))
12662 (modi (verilog-modi-current))
12663 (moddecls (verilog-modi-get-decls modi))
12664 (all-list (verilog-decls-get-signals moddecls))
12665 sigss sig-list dly-list prereset-sigs)
12666 ;; Read signals in always, eliminate outputs from reset list
12667 (setq prereset-sigs (verilog-signals-from-signame
12668 (save-excursion
12669 (verilog-read-signals
12670 (save-excursion
12671 (verilog-re-search-backward-quick "\\(@\\|\\<begin\\>\\|\\<if\\>\\|\\<case\\>\\)" nil t)
12672 (point))
12673 (point)))))
12674 (save-excursion
12675 (verilog-re-search-backward-quick "@" nil t)
12676 (setq sigss (verilog-read-always-signals)))
12677 (setq dly-list (verilog-alw-get-outputs-delayed sigss))
12678 (setq sig-list (verilog-signals-not-in (append
12679 (verilog-alw-get-outputs-delayed sigss)
12680 (when (or (not (verilog-alw-get-uses-delayed sigss))
12681 verilog-auto-reset-blocking-in-non)
12682 (verilog-alw-get-outputs-immediate sigss)))
12683 (append
12684 (verilog-alw-get-temps sigss)
12685 prereset-sigs)))
12686 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12687 (when sig-list
12688 (insert "\n");
12689 (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
12690 (while sig-list
12691 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ;; As sig-list has no widths
12692 (car sig-list))))
12693 (indent-to indent-pt)
12694 (insert (verilog-sig-name sig)
12695 (if (assoc (verilog-sig-name sig) dly-list)
12696 (concat " <= " verilog-assignment-delay)
12697 " = ")
12698 (verilog-sig-tieoff sig)
12699 ";\n")
12700 (setq sig-list (cdr sig-list))))
12701 (verilog-insert-indent "// End of automatics")))))
12702
12703 (defun verilog-auto-tieoff ()
12704 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
12705 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
12706 signals to deasserted.
12707
12708 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
12709 input/output list as another module, but no internals. Specifically, it
12710 finds all outputs in the module, and if that input is not otherwise declared
12711 as a register or wire, creates a tieoff.
12712
12713 AUTORESET ties signals to deasserted, which is presumed to be zero.
12714 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12715 them to a one.
12716
12717 You can add signals you do not want included in AUTOTIEOFF with
12718 `verilog-auto-tieoff-ignore-regexp'.
12719
12720 `verilog-auto-wire-type' may be used to change the datatype of
12721 the declarations.
12722
12723 `verilog-auto-reset-widths' may be used to change how the tieoff
12724 value's width is generated.
12725
12726 An example of making a stub for another module:
12727
12728 module ExampStub (/*AUTOINST*/);
12729 /*AUTOINOUTPARAM(\"Foo\")*/
12730 /*AUTOINOUTMODULE(\"Foo\")*/
12731 /*AUTOTIEOFF*/
12732 // verilator lint_off UNUSED
12733 wire _unused_ok = &{1'b0,
12734 /*AUTOUNUSED*/
12735 1'b0};
12736 // verilator lint_on UNUSED
12737 endmodule
12738
12739 Typing \\[verilog-auto] will make this into:
12740
12741 module ExampStub (/*AUTOINST*/...);
12742 /*AUTOINOUTPARAM(\"Foo\")*/
12743 /*AUTOINOUTMODULE(\"Foo\")*/
12744 // Beginning of autotieoff
12745 output [2:0] foo;
12746 // End of automatics
12747
12748 /*AUTOTIEOFF*/
12749 // Beginning of autotieoff
12750 wire [2:0] foo = 3'b0;
12751 // End of automatics
12752 ...
12753 endmodule"
12754 (interactive)
12755 (save-excursion
12756 ;; Find beginning
12757 (let* ((indent-pt (current-indentation))
12758 (modi (verilog-modi-current))
12759 (moddecls (verilog-modi-get-decls modi))
12760 (modsubdecls (verilog-modi-get-sub-decls modi))
12761 (sig-list (verilog-signals-not-in
12762 (verilog-decls-get-outputs moddecls)
12763 (append (verilog-decls-get-vars moddecls)
12764 (verilog-decls-get-assigns moddecls)
12765 (verilog-decls-get-consts moddecls)
12766 (verilog-decls-get-gparams moddecls)
12767 (verilog-subdecls-get-interfaced modsubdecls)
12768 (verilog-subdecls-get-outputs modsubdecls)
12769 (verilog-subdecls-get-inouts modsubdecls)))))
12770 (setq sig-list (verilog-signals-not-matching-regexp
12771 sig-list verilog-auto-tieoff-ignore-regexp))
12772 (when sig-list
12773 (verilog-forward-or-insert-line)
12774 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
12775 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
12776 (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
12777 (while sig-list
12778 (let ((sig (car sig-list)))
12779 (cond ((equal verilog-auto-tieoff-declaration "assign")
12780 (indent-to indent-pt)
12781 (insert "assign " (verilog-sig-name sig)))
12782 (t
12783 (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
12784 (indent-to (max 48 (+ indent-pt 40)))
12785 (insert "= " (verilog-sig-tieoff sig)
12786 ";\n")
12787 (setq sig-list (cdr sig-list))))
12788 (verilog-insert-indent "// End of automatics\n")))))
12789
12790 (defun verilog-auto-undef ()
12791 "Expand AUTOUNDEF statements, as part of \\[verilog-auto].
12792 Take any `defines since the last AUTOUNDEF in the current file
12793 and create `undefs for them. This is used to insure that
12794 file-local defines do not pollute the global `define name space.
12795
12796 Limitations:
12797 AUTOUNDEF presumes any identifier following `define is the
12798 name of a define. Any `ifdefs are ignored.
12799
12800 AUTOUNDEF suppresses creating an `undef for any define that was
12801 `undefed before the AUTOUNDEF. This may be used to work around
12802 the ignoring of `ifdefs as shown below.
12803
12804 An example:
12805
12806 `define XX_FOO
12807 `define M_BAR(x)
12808 `define M_BAZ
12809 ...
12810 `ifdef NEVER
12811 `undef M_BAZ // Emacs will see this and not `undef M_BAZ
12812 `endif
12813 ...
12814 /*AUTOUNDEF*/
12815
12816 Typing \\[verilog-auto] will make this into:
12817
12818 ...
12819 /*AUTOUNDEF*/
12820 // Beginning of automatic undefs
12821 `undef XX_FOO
12822 `undef M_BAR
12823 // End of automatics
12824
12825 You may also provide an optional regular expression, in which case only
12826 defines the regular expression will be undefed."
12827 (save-excursion
12828 (let* ((params (verilog-read-auto-params 0 1))
12829 (regexp (nth 0 params))
12830 (indent-pt (current-indentation))
12831 (end-pt (point))
12832 defs def)
12833 (save-excursion
12834 ;; Scan from start of file, or last AUTOUNDEF
12835 (or (verilog-re-search-backward-quick "/\\*AUTOUNDEF\\>" end-pt t)
12836 (goto-char (point-min)))
12837 (while (verilog-re-search-forward-quick
12838 "`\\(define\\|undef\\)\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" end-pt t)
12839 (cond ((equal (match-string-no-properties 1) "define")
12840 (setq def (match-string-no-properties 2))
12841 (when (and (or (not regexp)
12842 (string-match regexp def))
12843 (not (member def defs))) ;; delete-dups not in 21.1
12844 (setq defs (cons def defs))))
12845 (t
12846 (setq defs (delete (match-string-no-properties 2) defs))))))
12847 ;; Insert
12848 (setq defs (sort defs 'string<))
12849 (when defs
12850 (verilog-forward-or-insert-line)
12851 (verilog-insert-indent "// Beginning of automatic undefs\n")
12852 (while defs
12853 (verilog-insert-indent "`undef " (car defs) "\n")
12854 (setq defs (cdr defs)))
12855 (verilog-insert-indent "// End of automatics\n")))))
12856
12857 (defun verilog-auto-unused ()
12858 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
12859 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
12860 input and inout signals.
12861
12862 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
12863 input/output list as another module, but no internals. Specifically, it
12864 finds all inputs and inouts in the module, and if that input is not otherwise
12865 used, adds it to a comma separated list.
12866
12867 The comma separated list is intended to be used to create a _unused_ok
12868 signal. Using the exact name \"_unused_ok\" for name of the temporary
12869 signal is recommended as it will insure maximum forward compatibility, it
12870 also makes lint warnings easy to understand; ignore any unused warnings
12871 with \"unused\" in the signal name.
12872
12873 To reduce simulation time, the _unused_ok signal should be forced to a
12874 constant to prevent wiggling. The easiest thing to do is use a
12875 reduction-and with 1'b0 as shown.
12876
12877 This way all unused signals are in one place, making it convenient to add
12878 your tool's specific pragmas around the assignment to disable any unused
12879 warnings.
12880
12881 You can add signals you do not want included in AUTOUNUSED with
12882 `verilog-auto-unused-ignore-regexp'.
12883
12884 An example of making a stub for another module:
12885
12886 module ExampStub (/*AUTOINST*/);
12887 /*AUTOINOUTPARAM(\"Examp\")*/
12888 /*AUTOINOUTMODULE(\"Examp\")*/
12889 /*AUTOTIEOFF*/
12890 // verilator lint_off UNUSED
12891 wire _unused_ok = &{1'b0,
12892 /*AUTOUNUSED*/
12893 1'b0};
12894 // verilator lint_on UNUSED
12895 endmodule
12896
12897 Typing \\[verilog-auto] will make this into:
12898
12899 ...
12900 // verilator lint_off UNUSED
12901 wire _unused_ok = &{1'b0,
12902 /*AUTOUNUSED*/
12903 // Beginning of automatics
12904 unused_input_a,
12905 unused_input_b,
12906 unused_input_c,
12907 // End of automatics
12908 1'b0};
12909 // verilator lint_on UNUSED
12910 endmodule"
12911 (interactive)
12912 (save-excursion
12913 ;; Find beginning
12914 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
12915 (modi (verilog-modi-current))
12916 (moddecls (verilog-modi-get-decls modi))
12917 (modsubdecls (verilog-modi-get-sub-decls modi))
12918 (sig-list (verilog-signals-not-in
12919 (append (verilog-decls-get-inputs moddecls)
12920 (verilog-decls-get-inouts moddecls))
12921 (append (verilog-subdecls-get-inputs modsubdecls)
12922 (verilog-subdecls-get-inouts modsubdecls)))))
12923 (setq sig-list (verilog-signals-not-matching-regexp
12924 sig-list verilog-auto-unused-ignore-regexp))
12925 (when sig-list
12926 (verilog-forward-or-insert-line)
12927 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
12928 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
12929 (while sig-list
12930 (let ((sig (car sig-list)))
12931 (indent-to indent-pt)
12932 (insert (verilog-sig-name sig) ",\n")
12933 (setq sig-list (cdr sig-list))))
12934 (verilog-insert-indent "// End of automatics\n")))))
12935
12936 (defun verilog-enum-ascii (signm elim-regexp)
12937 "Convert an enum name SIGNM to an ascii string for insertion.
12938 Remove user provided prefix ELIM-REGEXP."
12939 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
12940 (let ((case-fold-search t))
12941 ;; All upper becomes all lower for readability
12942 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
12943
12944 (defun verilog-auto-ascii-enum ()
12945 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
12946 Create a register to contain the ASCII decode of an enumerated signal type.
12947 This will allow trace viewers to show the ASCII name of states.
12948
12949 First, parameters are built into an enumeration using the synopsys enum
12950 comment. The comment must be between the keyword and the symbol.
12951 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
12952
12953 Next, registers which that enum applies to are also tagged with the same
12954 enum.
12955
12956 Finally, an AUTOASCIIENUM command is used.
12957
12958 The first parameter is the name of the signal to be decoded.
12959
12960 The second parameter is the name to store the ASCII code into. For the
12961 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
12962 a signal that is just for simulation, and the magic characters _ascii
12963 tell viewers like Dinotrace to display in ASCII format.
12964
12965 The third optional parameter is a string which will be removed
12966 from the state names. It defaults to \"\" which removes nothing.
12967
12968 The fourth optional parameter is \"onehot\" to force one-hot
12969 decoding. If unspecified, if and only if the first parameter
12970 width is 2^(number of states in enum) and does NOT match the
12971 width of the enum, the signal is assumed to be a one-hot
12972 decode. Otherwise, it's a normal encoded state vector.
12973
12974 `verilog-auto-wire-type' may be used to change the datatype of
12975 the declarations.
12976
12977 \"auto enum\" may be used in place of \"synopsys enum\".
12978
12979 An example:
12980
12981 //== State enumeration
12982 parameter [2:0] // synopsys enum state_info
12983 SM_IDLE = 3'b000,
12984 SM_SEND = 3'b001,
12985 SM_WAIT1 = 3'b010;
12986 //== State variables
12987 reg [2:0] /* synopsys enum state_info */
12988 state_r; /* synopsys state_vector state_r */
12989 reg [2:0] /* synopsys enum state_info */
12990 state_e1;
12991
12992 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12993
12994 Typing \\[verilog-auto] will make this into:
12995
12996 ... same front matter ...
12997
12998 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12999 // Beginning of automatic ASCII enum decoding
13000 reg [39:0] state_ascii_r; // Decode of state_r
13001 always @(state_r) begin
13002 case ({state_r})
13003 SM_IDLE: state_ascii_r = \"idle \";
13004 SM_SEND: state_ascii_r = \"send \";
13005 SM_WAIT1: state_ascii_r = \"wait1\";
13006 default: state_ascii_r = \"%Erro\";
13007 endcase
13008 end
13009 // End of automatics"
13010 (save-excursion
13011 (let* ((params (verilog-read-auto-params 2 4))
13012 (undecode-name (nth 0 params))
13013 (ascii-name (nth 1 params))
13014 (elim-regexp (and (nth 2 params)
13015 (not (equal (nth 2 params) ""))
13016 (nth 2 params)))
13017 (one-hot-flag (nth 3 params))
13018 ;;
13019 (indent-pt (current-indentation))
13020 (modi (verilog-modi-current))
13021 (moddecls (verilog-modi-get-decls modi))
13022 ;;
13023 (sig-list-consts (append (verilog-decls-get-consts moddecls)
13024 (verilog-decls-get-gparams moddecls)))
13025 (sig-list-all (verilog-decls-get-iovars moddecls))
13026 ;;
13027 (undecode-sig (or (assoc undecode-name sig-list-all)
13028 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
13029 (undecode-enum (or (verilog-sig-enum undecode-sig)
13030 (error "%s: Signal %s does not have an enum tag" (verilog-point-text) undecode-name)))
13031 ;;
13032 (enum-sigs (verilog-signals-not-in
13033 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
13034 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum))
13035 nil))
13036 ;;
13037 (one-hot (or
13038 (string-match "onehot" (or one-hot-flag ""))
13039 (and ;; width(enum) != width(sig)
13040 (or (not (verilog-sig-bits (car enum-sigs)))
13041 (not (equal (verilog-sig-width (car enum-sigs))
13042 (verilog-sig-width undecode-sig))))
13043 ;; count(enums) == width(sig)
13044 (equal (number-to-string (length enum-sigs))
13045 (verilog-sig-width undecode-sig)))))
13046 (enum-chars 0)
13047 (ascii-chars 0))
13048 ;;
13049 ;; Find number of ascii chars needed
13050 (let ((tmp-sigs enum-sigs))
13051 (while tmp-sigs
13052 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
13053 ascii-chars (max ascii-chars (length (verilog-enum-ascii
13054 (verilog-sig-name (car tmp-sigs))
13055 elim-regexp)))
13056 tmp-sigs (cdr tmp-sigs))))
13057 ;;
13058 (verilog-forward-or-insert-line)
13059 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
13060 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
13061 (concat "Decode of " undecode-name) nil nil))))
13062 (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
13063 ;;
13064 (verilog-insert-indent "always @(" undecode-name ") begin\n")
13065 (setq indent-pt (+ indent-pt verilog-indent-level))
13066 (verilog-insert-indent "case ({" undecode-name "})\n")
13067 (setq indent-pt (+ indent-pt verilog-case-indent))
13068 ;;
13069 (let ((tmp-sigs enum-sigs)
13070 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
13071 (+ (if one-hot 9 1) (max 8 enum-chars))
13072 ascii-name ascii-chars))
13073 (errname (substring "%Error" 0 (min 6 ascii-chars))))
13074 (while tmp-sigs
13075 (verilog-insert-indent
13076 (concat
13077 (format chrfmt
13078 (concat (if one-hot "(")
13079 ;; Use enum-sigs length as that's numeric
13080 ;; verilog-sig-width undecode-sig might not be.
13081 (if one-hot (number-to-string (length enum-sigs)))
13082 ;; We use a shift instead of var[index]
13083 ;; so that a non-one hot value will show as error.
13084 (if one-hot "'b1<<")
13085 (verilog-sig-name (car tmp-sigs))
13086 (if one-hot ")") ":")
13087 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
13088 elim-regexp))))
13089 (setq tmp-sigs (cdr tmp-sigs)))
13090 (verilog-insert-indent (format chrfmt "default:" errname)))
13091 ;;
13092 (setq indent-pt (- indent-pt verilog-case-indent))
13093 (verilog-insert-indent "endcase\n")
13094 (setq indent-pt (- indent-pt verilog-indent-level))
13095 (verilog-insert-indent "end\n"
13096 "// End of automatics\n"))))
13097
13098 (defun verilog-auto-templated-rel ()
13099 "Replace Templated relative line numbers with absolute line numbers.
13100 Internal use only. This hacks around the line numbers in AUTOINST Templates
13101 being different from the final output's line numbering."
13102 (let ((templateno 0) (template-line (list 0)) (buf-line 1))
13103 ;; Find line number each template is on
13104 ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
13105 (goto-char (point-min))
13106 (while (not (eobp))
13107 (when (looking-at ".*AUTO_TEMPLATE")
13108 (setq templateno (1+ templateno))
13109 (setq template-line (cons buf-line template-line)))
13110 (setq buf-line (1+ buf-line))
13111 (forward-line 1))
13112 (setq template-line (nreverse template-line))
13113 ;; Replace T# L# with absolute line number
13114 (goto-char (point-min))
13115 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
13116 (replace-match
13117 (concat " Templated "
13118 (int-to-string (+ (nth (string-to-number (match-string 1))
13119 template-line)
13120 (string-to-number (match-string 2)))))
13121 t t))))
13122
13123 (defun verilog-auto-template-lint ()
13124 "Check AUTO_TEMPLATEs for unused lines.
13125 Enable with `verilog-auto-template-warn-unused'."
13126 (let ((name1 (or (buffer-file-name) (buffer-name))))
13127 (save-excursion
13128 (goto-char (point-min))
13129 (while (re-search-forward
13130 "^\\s-*/?\\*?\\s-*[a-zA-Z0-9`_$]+\\s-+AUTO_TEMPLATE" nil t)
13131 (let* ((tpl-info (verilog-read-auto-template-middle))
13132 (tpl-list (aref tpl-info 1))
13133 (tlines (append (nth 0 tpl-list) (nth 1 tpl-list)))
13134 tpl-ass)
13135 (while tlines
13136 (setq tpl-ass (car tlines)
13137 tlines (cdr tlines))
13138 ;;;
13139 (unless (or (not (eval-when-compile (fboundp 'make-hash-table))) ;; Not supported, no warning
13140 (not verilog-auto-template-hits)
13141 (gethash (vector (nth 2 tpl-ass) (nth 3 tpl-ass))
13142 verilog-auto-template-hits))
13143 (verilog-warn-error "%s:%d: AUTO_TEMPLATE line unused: \".%s (%s)\""
13144 name1
13145 (+ (elt tpl-ass 3) ;; Template line number
13146 (count-lines (point-min) (point)))
13147 (elt tpl-ass 0) (elt tpl-ass 1))
13148 )))))))
13149
13150 \f
13151 ;;
13152 ;; Auto top level
13153 ;;
13154
13155 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing an arg
13156 "Expand AUTO statements.
13157 Look for any /*AUTO...*/ commands in the code, as used in
13158 instantiations or argument headers. Update the list of signals
13159 following the /*AUTO...*/ command.
13160
13161 Use \\[verilog-delete-auto] to remove the AUTOs.
13162
13163 Use \\[verilog-diff-auto] to see differences in AUTO expansion.
13164
13165 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
13166
13167 Use \\[verilog-faq] for a pointer to frequently asked questions.
13168
13169 For new users, we recommend setting `verilog-case-fold' to nil
13170 and `verilog-auto-arg-sort' to t.
13171
13172 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
13173 called before and after this function, respectively.
13174
13175 For example:
13176 module ModuleName (/*AUTOARG*/);
13177 /*AUTOINPUT*/
13178 /*AUTOOUTPUT*/
13179 /*AUTOWIRE*/
13180 /*AUTOREG*/
13181 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
13182
13183 You can also update the AUTOs from the shell using:
13184 emacs --batch <filenames.v> -f verilog-batch-auto
13185 Or fix indentation with:
13186 emacs --batch <filenames.v> -f verilog-batch-indent
13187 Likewise, you can delete or inject AUTOs with:
13188 emacs --batch <filenames.v> -f verilog-batch-delete-auto
13189 emacs --batch <filenames.v> -f verilog-batch-inject-auto
13190 Or check if AUTOs have the same expansion
13191 emacs --batch <filenames.v> -f verilog-batch-diff-auto
13192
13193 Using \\[describe-function], see also:
13194 `verilog-auto-arg' for AUTOARG module instantiations
13195 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
13196 `verilog-auto-assign-modport' for AUTOASSIGNMODPORT assignment to/from modport
13197 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
13198 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
13199 `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
13200 `verilog-auto-inout-modport' for AUTOINOUTMODPORT i/o from an interface modport
13201 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
13202 `verilog-auto-inout-param' for AUTOINOUTPARAM copying params from elsewhere
13203 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
13204 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
13205 `verilog-auto-inst' for AUTOINST instantiation pins
13206 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
13207 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
13208 `verilog-auto-logic' for AUTOLOGIC declaring logic signals
13209 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
13210 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
13211 `verilog-auto-reg' for AUTOREG registers
13212 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
13213 `verilog-auto-reset' for AUTORESET flop resets
13214 `verilog-auto-sense' for AUTOSENSE or AS always sensitivity lists
13215 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
13216 `verilog-auto-undef' for AUTOUNDEF `undef of local `defines
13217 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
13218 `verilog-auto-wire' for AUTOWIRE instantiation wires
13219
13220 `verilog-read-defines' for reading `define values
13221 `verilog-read-includes' for reading `includes
13222
13223 If you have bugs with these autos, please file an issue at
13224 URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
13225 Wilson Snyder (wsnyder@wsnyder.org)."
13226 (interactive)
13227 (unless noninteractive (message "Updating AUTOs..."))
13228 (if (fboundp 'dinotrace-unannotate-all)
13229 (dinotrace-unannotate-all))
13230 (verilog-save-font-mods
13231 (let ((oldbuf (if (not (buffer-modified-p))
13232 (buffer-string)))
13233 (case-fold-search verilog-case-fold)
13234 ;; Cache directories; we don't write new files, so can't change
13235 (verilog-dir-cache-preserving t)
13236 ;; Cache current module
13237 (verilog-modi-cache-current-enable t)
13238 (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
13239 verilog-modi-cache-current)
13240 (unwind-protect
13241 ;; Disable change hooks for speed
13242 ;; This let can't be part of above let; must restore
13243 ;; after-change-functions before font-lock resumes
13244 (verilog-save-no-change-functions
13245 (verilog-save-scan-cache
13246 (save-excursion
13247 ;; Wipe cache; otherwise if we AUTOed a block above this one,
13248 ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
13249 (setq verilog-modi-cache-list nil)
13250 ;; Local state
13251 (setq verilog-auto-template-hits nil)
13252 ;; If we're not in verilog-mode, change syntax table so parsing works right
13253 (unless (eq major-mode `verilog-mode) (verilog-mode))
13254 ;; Allow user to customize
13255 (verilog-run-hooks 'verilog-before-auto-hook)
13256 ;; Try to save the user from needing to revert-file to reread file local-variables
13257 (verilog-auto-reeval-locals)
13258 (verilog-read-auto-lisp-present)
13259 (verilog-read-auto-lisp (point-min) (point-max))
13260 (verilog-getopt-flags)
13261 ;; From here on out, we can cache anything we read from disk
13262 (verilog-preserve-dir-cache
13263 ;; These two may seem obvious to do always, but on large includes it can be way too slow
13264 (when verilog-auto-read-includes
13265 (verilog-read-includes)
13266 (verilog-read-defines nil nil t))
13267 ;; Setup variables due to SystemVerilog expansion
13268 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic-setup)
13269 ;; This particular ordering is important
13270 ;; INST: Lower modules correct, no internal dependencies, FIRST
13271 (verilog-preserve-modi-cache
13272 ;; Clear existing autos else we'll be screwed by existing ones
13273 (verilog-delete-auto)
13274 ;; Injection if appropriate
13275 (when inject
13276 (verilog-inject-inst)
13277 (verilog-inject-sense)
13278 (verilog-inject-arg))
13279 ;;
13280 ;; Do user inserts first, so their code can insert AUTOs
13281 ;; We may provide an AUTOINSERTLISPLAST if another cleanup pass is needed
13282 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
13283 'verilog-auto-insert-lisp)
13284 ;; Expand instances before need the signals the instances input/output
13285 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
13286 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
13287 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
13288 ;; Doesn't matter when done, but combine it with a common changer
13289 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
13290 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
13291 ;; Must be done before autoin/out as creates a reg
13292 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM(.*?)\\*/" 'verilog-auto-ascii-enum)
13293 ;;
13294 ;; first in/outs from other files
13295 (verilog-auto-re-search-do "/\\*AUTOINOUTMODPORT(.*?)\\*/" 'verilog-auto-inout-modport)
13296 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE(.*?)\\*/" 'verilog-auto-inout-module)
13297 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP(.*?)\\*/" 'verilog-auto-inout-comp)
13298 (verilog-auto-re-search-do "/\\*AUTOINOUTIN(.*?)\\*/" 'verilog-auto-inout-in)
13299 (verilog-auto-re-search-do "/\\*AUTOINOUTPARAM(.*?)\\*/" 'verilog-auto-inout-param)
13300 ;; next in/outs which need previous sucked inputs first
13301 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((.*?)\\)?\\*/" 'verilog-auto-output)
13302 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((.*?)\\)?\\*/" 'verilog-auto-input)
13303 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((.*?)\\)?\\*/" 'verilog-auto-inout)
13304 ;; Then tie off those in/outs
13305 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
13306 ;; These can be anywhere after AUTOINSERTLISP
13307 (verilog-auto-re-search-do "/\\*AUTOUNDEF\\((.*?)\\)?\\*/" 'verilog-auto-undef)
13308 ;; Wires/regs must be after inputs/outputs
13309 (verilog-auto-re-search-do "/\\*AUTOASSIGNMODPORT(.*?)\\*/" 'verilog-auto-assign-modport)
13310 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
13311 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
13312 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
13313 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
13314 ;; outputevery needs AUTOOUTPUTs done first
13315 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\*/" 'verilog-auto-output-every)
13316 ;; After we've created all new variables
13317 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
13318 ;; Must be after all inputs outputs are generated
13319 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
13320 ;; Fix line numbers (comments only)
13321 (when verilog-auto-inst-template-numbers
13322 (verilog-auto-templated-rel))
13323 (when verilog-auto-template-warn-unused
13324 (verilog-auto-template-lint))))
13325 ;;
13326 (verilog-run-hooks 'verilog-auto-hook)
13327 ;;
13328 (when verilog-auto-delete-trailing-whitespace
13329 (verilog-delete-trailing-whitespace))
13330 ;;
13331 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
13332 ;;
13333 ;; If end result is same as when started, clear modified flag
13334 (cond ((and oldbuf (equal oldbuf (buffer-string)))
13335 (set-buffer-modified-p nil)
13336 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
13337 (t (unless noninteractive (message "Updating AUTOs...done"))))
13338 ;; End of after-change protection
13339 )))
13340 ;; Unwind forms
13341 ;; Currently handled in verilog-save-font-mods
13342 ))))
13343 \f
13344
13345 ;;
13346 ;; Skeleton based code insertion
13347 ;;
13348 (defvar verilog-template-map
13349 (let ((map (make-sparse-keymap)))
13350 (define-key map "a" 'verilog-sk-always)
13351 (define-key map "b" 'verilog-sk-begin)
13352 (define-key map "c" 'verilog-sk-case)
13353 (define-key map "f" 'verilog-sk-for)
13354 (define-key map "g" 'verilog-sk-generate)
13355 (define-key map "h" 'verilog-sk-header)
13356 (define-key map "i" 'verilog-sk-initial)
13357 (define-key map "j" 'verilog-sk-fork)
13358 (define-key map "m" 'verilog-sk-module)
13359 (define-key map "o" 'verilog-sk-ovm-class)
13360 (define-key map "p" 'verilog-sk-primitive)
13361 (define-key map "r" 'verilog-sk-repeat)
13362 (define-key map "s" 'verilog-sk-specify)
13363 (define-key map "t" 'verilog-sk-task)
13364 (define-key map "u" 'verilog-sk-uvm-object)
13365 (define-key map "w" 'verilog-sk-while)
13366 (define-key map "x" 'verilog-sk-casex)
13367 (define-key map "z" 'verilog-sk-casez)
13368 (define-key map "?" 'verilog-sk-if)
13369 (define-key map ":" 'verilog-sk-else-if)
13370 (define-key map "/" 'verilog-sk-comment)
13371 (define-key map "A" 'verilog-sk-assign)
13372 (define-key map "F" 'verilog-sk-function)
13373 (define-key map "I" 'verilog-sk-input)
13374 (define-key map "O" 'verilog-sk-output)
13375 (define-key map "S" 'verilog-sk-state-machine)
13376 (define-key map "=" 'verilog-sk-inout)
13377 (define-key map "U" 'verilog-sk-uvm-component)
13378 (define-key map "W" 'verilog-sk-wire)
13379 (define-key map "R" 'verilog-sk-reg)
13380 (define-key map "D" 'verilog-sk-define-signal)
13381 map)
13382 "Keymap used in Verilog mode for smart template operations.")
13383
13384
13385 ;;
13386 ;; Place the templates into Verilog Mode. They may be inserted under any key.
13387 ;; C-c C-t will be the default. If you use templates a lot, you
13388 ;; may want to consider moving the binding to another key in your init
13389 ;; file.
13390 ;;
13391 ;; Note \C-c and letter are reserved for users
13392 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
13393
13394 ;;; ---- statement skeletons ------------------------------------------
13395
13396 (define-skeleton verilog-sk-prompt-condition
13397 "Prompt for the loop condition."
13398 "[condition]: " str )
13399
13400 (define-skeleton verilog-sk-prompt-init
13401 "Prompt for the loop init statement."
13402 "[initial statement]: " str )
13403
13404 (define-skeleton verilog-sk-prompt-inc
13405 "Prompt for the loop increment statement."
13406 "[increment statement]: " str )
13407
13408 (define-skeleton verilog-sk-prompt-name
13409 "Prompt for the name of something."
13410 "[name]: " str)
13411
13412 (define-skeleton verilog-sk-prompt-clock
13413 "Prompt for the name of something."
13414 "name and edge of clock(s): " str)
13415
13416 (defvar verilog-sk-reset nil)
13417 (defun verilog-sk-prompt-reset ()
13418 "Prompt for the name of a state machine reset."
13419 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
13420
13421
13422 (define-skeleton verilog-sk-prompt-state-selector
13423 "Prompt for the name of a state machine selector."
13424 "name of selector (eg {a,b,c,d}): " str )
13425
13426 (define-skeleton verilog-sk-prompt-output
13427 "Prompt for the name of something."
13428 "output: " str)
13429
13430 (define-skeleton verilog-sk-prompt-msb
13431 "Prompt for most significant bit specification."
13432 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
13433
13434 (define-skeleton verilog-sk-prompt-lsb
13435 "Prompt for least significant bit specification."
13436 "lsb:" str )
13437
13438 (defvar verilog-sk-p nil)
13439 (define-skeleton verilog-sk-prompt-width
13440 "Prompt for a width specification."
13441 ()
13442 (progn
13443 (setq verilog-sk-p (point))
13444 (verilog-sk-prompt-msb)
13445 (if (> (point) verilog-sk-p) "] " " ")))
13446
13447 (defun verilog-sk-header ()
13448 "Insert a descriptive header at the top of the file.
13449 See also `verilog-header' for an alternative format."
13450 (interactive "*")
13451 (save-excursion
13452 (goto-char (point-min))
13453 (verilog-sk-header-tmpl)))
13454
13455 (define-skeleton verilog-sk-header-tmpl
13456 "Insert a comment block containing the module title, author, etc."
13457 "[Description]: "
13458 "// -*- Mode: Verilog -*-"
13459 "\n// Filename : " (buffer-name)
13460 "\n// Description : " str
13461 "\n// Author : " (user-full-name)
13462 "\n// Created On : " (current-time-string)
13463 "\n// Last Modified By: " (user-full-name)
13464 "\n// Last Modified On: " (current-time-string)
13465 "\n// Update Count : 0"
13466 "\n// Status : Unknown, Use with caution!"
13467 "\n")
13468
13469 (define-skeleton verilog-sk-module
13470 "Insert a module definition."
13471 ()
13472 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
13473 > _ \n
13474 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
13475
13476 ;;; ------------------------------------------------------------------------
13477 ;;; Define a default OVM class, with macros and new()
13478 ;;; ------------------------------------------------------------------------
13479
13480 (define-skeleton verilog-sk-ovm-class
13481 "Insert a class definition"
13482 ()
13483 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13484 > _ \n
13485 > "`ovm_object_utils_begin(" name ")" \n
13486 > (- verilog-indent-level) " `ovm_object_utils_end" \n
13487 > _ \n
13488 > "function new(name=\"" name "\");" \n
13489 > "super.new(name);" \n
13490 > (- verilog-indent-level) "endfunction" \n
13491 > _ \n
13492 > "endclass" (progn (electric-verilog-terminate-line) nil))
13493
13494 (define-skeleton verilog-sk-uvm-object
13495 "Insert a class definition"
13496 ()
13497 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13498 > _ \n
13499 > "`uvm_object_utils_begin(" name ")" \n
13500 > (- verilog-indent-level) "`uvm_object_utils_end" \n
13501 > _ \n
13502 > "function new(name=\"" name "\");" \n
13503 > "super.new(name);" \n
13504 > (- verilog-indent-level) "endfunction" \n
13505 > _ \n
13506 > "endclass" (progn (electric-verilog-terminate-line) nil))
13507
13508 (define-skeleton verilog-sk-uvm-component
13509 "Insert a class definition"
13510 ()
13511 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13512 > _ \n
13513 > "`uvm_component_utils_begin(" name ")" \n
13514 > (- verilog-indent-level) "`uvm_component_utils_end" \n
13515 > _ \n
13516 > "function new(name=\"\", uvm_component parent);" \n
13517 > "super.new(name, parent);" \n
13518 > (- verilog-indent-level) "endfunction" \n
13519 > _ \n
13520 > "endclass" (progn (electric-verilog-terminate-line) nil))
13521
13522 (define-skeleton verilog-sk-primitive
13523 "Insert a task definition."
13524 ()
13525 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
13526 > _ \n
13527 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
13528
13529 (define-skeleton verilog-sk-task
13530 "Insert a task definition."
13531 ()
13532 > "task " '(verilog-sk-prompt-name) & ?; \n
13533 > _ \n
13534 > "begin" \n
13535 > \n
13536 > (- verilog-indent-level-behavioral) "end" \n
13537 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
13538
13539 (define-skeleton verilog-sk-function
13540 "Insert a function definition."
13541 ()
13542 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
13543 > _ \n
13544 > "begin" \n
13545 > \n
13546 > (- verilog-indent-level-behavioral) "end" \n
13547 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
13548
13549 (define-skeleton verilog-sk-always
13550 "Insert always block. Uses the minibuffer to prompt
13551 for sensitivity list."
13552 ()
13553 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13554 > _ \n
13555 > (- verilog-indent-level-behavioral) "end" \n >
13556 )
13557
13558 (define-skeleton verilog-sk-initial
13559 "Insert an initial block."
13560 ()
13561 > "initial begin\n"
13562 > _ \n
13563 > (- verilog-indent-level-behavioral) "end" \n > )
13564
13565 (define-skeleton verilog-sk-specify
13566 "Insert specify block. "
13567 ()
13568 > "specify\n"
13569 > _ \n
13570 > (- verilog-indent-level-behavioral) "endspecify" \n > )
13571
13572 (define-skeleton verilog-sk-generate
13573 "Insert generate block. "
13574 ()
13575 > "generate\n"
13576 > _ \n
13577 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
13578
13579 (define-skeleton verilog-sk-begin
13580 "Insert begin end block. Uses the minibuffer to prompt for name."
13581 ()
13582 > "begin" '(verilog-sk-prompt-name) \n
13583 > _ \n
13584 > (- verilog-indent-level-behavioral) "end" )
13585
13586 (define-skeleton verilog-sk-fork
13587 "Insert a fork join block."
13588 ()
13589 > "fork\n"
13590 > "begin" \n
13591 > _ \n
13592 > (- verilog-indent-level-behavioral) "end" \n
13593 > "begin" \n
13594 > \n
13595 > (- verilog-indent-level-behavioral) "end" \n
13596 > (- verilog-indent-level-behavioral) "join" \n
13597 > )
13598
13599
13600 (define-skeleton verilog-sk-case
13601 "Build skeleton case statement, prompting for the selector expression,
13602 and the case items."
13603 "[selector expression]: "
13604 > "case (" str ") " \n
13605 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13606 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13607
13608 (define-skeleton verilog-sk-casex
13609 "Build skeleton casex statement, prompting for the selector expression,
13610 and the case items."
13611 "[selector expression]: "
13612 > "casex (" str ") " \n
13613 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13614 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13615
13616 (define-skeleton verilog-sk-casez
13617 "Build skeleton casez statement, prompting for the selector expression,
13618 and the case items."
13619 "[selector expression]: "
13620 > "casez (" str ") " \n
13621 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13622 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13623
13624 (define-skeleton verilog-sk-if
13625 "Insert a skeleton if statement."
13626 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
13627 > _ \n
13628 > (- verilog-indent-level-behavioral) "end " \n )
13629
13630 (define-skeleton verilog-sk-else-if
13631 "Insert a skeleton else if statement."
13632 > (verilog-indent-line) "else if ("
13633 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
13634 > _ \n
13635 > "end" (progn (electric-verilog-terminate-line) nil))
13636
13637 (define-skeleton verilog-sk-datadef
13638 "Common routine to get data definition."
13639 ()
13640 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
13641
13642 (define-skeleton verilog-sk-input
13643 "Insert an input definition."
13644 ()
13645 > "input [" '(verilog-sk-datadef))
13646
13647 (define-skeleton verilog-sk-output
13648 "Insert an output definition."
13649 ()
13650 > "output [" '(verilog-sk-datadef))
13651
13652 (define-skeleton verilog-sk-inout
13653 "Insert an inout definition."
13654 ()
13655 > "inout [" '(verilog-sk-datadef))
13656
13657 (defvar verilog-sk-signal nil)
13658 (define-skeleton verilog-sk-def-reg
13659 "Insert a reg definition."
13660 ()
13661 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations-auto) )
13662
13663 (defun verilog-sk-define-signal ()
13664 "Insert a definition of signal under point at top of module."
13665 (interactive "*")
13666 (let* ((sig-re "[a-zA-Z0-9_]*")
13667 (v1 (buffer-substring
13668 (save-excursion
13669 (skip-chars-backward sig-re)
13670 (point))
13671 (save-excursion
13672 (skip-chars-forward sig-re)
13673 (point)))))
13674 (if (not (member v1 verilog-keywords))
13675 (save-excursion
13676 (setq verilog-sk-signal v1)
13677 (verilog-beg-of-defun)
13678 (verilog-end-of-statement)
13679 (verilog-forward-syntactic-ws)
13680 (verilog-sk-def-reg)
13681 (message "signal at point is %s" v1))
13682 (message "object at point (%s) is a keyword" v1))))
13683
13684 (define-skeleton verilog-sk-wire
13685 "Insert a wire definition."
13686 ()
13687 > "wire [" '(verilog-sk-datadef))
13688
13689 (define-skeleton verilog-sk-reg
13690 "Insert a reg definition."
13691 ()
13692 > "reg [" '(verilog-sk-datadef))
13693
13694 (define-skeleton verilog-sk-assign
13695 "Insert a skeleton assign statement."
13696 ()
13697 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
13698
13699 (define-skeleton verilog-sk-while
13700 "Insert a skeleton while loop statement."
13701 ()
13702 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
13703 > _ \n
13704 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13705
13706 (define-skeleton verilog-sk-repeat
13707 "Insert a skeleton repeat loop statement."
13708 ()
13709 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
13710 > _ \n
13711 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13712
13713 (define-skeleton verilog-sk-for
13714 "Insert a skeleton while loop statement."
13715 ()
13716 > "for ("
13717 '(verilog-sk-prompt-init) "; "
13718 '(verilog-sk-prompt-condition) "; "
13719 '(verilog-sk-prompt-inc)
13720 ") begin" \n
13721 > _ \n
13722 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13723
13724 (define-skeleton verilog-sk-comment
13725 "Inserts three comment lines, making a display comment."
13726 ()
13727 > "/*\n"
13728 > "* " _ \n
13729 > "*/")
13730
13731 (define-skeleton verilog-sk-state-machine
13732 "Insert a state machine definition."
13733 "Name of state variable: "
13734 '(setq input "state")
13735 > "// State registers for " str | -23 \n
13736 '(setq verilog-sk-state str)
13737 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
13738 '(setq input nil)
13739 > \n
13740 > "// State FF for " verilog-sk-state \n
13741 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
13742 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
13743 > verilog-sk-state " = next_" verilog-sk-state ?; \n
13744 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
13745 > \n
13746 > "// Next State Logic for " verilog-sk-state \n
13747 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13748 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
13749 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
13750 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
13751 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
13752 \f
13753
13754 ;;
13755 ;; Include file loading with mouse/return event
13756 ;;
13757 ;; idea & first impl.: M. Rouat (eldo-mode.el)
13758 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
13759
13760 (if (featurep 'xemacs)
13761 (require 'overlay))
13762
13763 (defconst verilog-include-file-regexp
13764 "^`include\\s-+\"\\([^\n\"]*\\)\""
13765 "Regexp that matches the include file.")
13766
13767 (defvar verilog-mode-mouse-map
13768 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
13769 (set-keymap-parent map verilog-mode-map)
13770 ;; mouse button bindings
13771 (define-key map "\r" 'verilog-load-file-at-point)
13772 (if (featurep 'xemacs)
13773 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
13774 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
13775 (if (featurep 'xemacs)
13776 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
13777 (define-key map [S-mouse-2] 'mouse-yank-at-click))
13778 map)
13779 "Map containing mouse bindings for `verilog-mode'.")
13780
13781
13782 (defun verilog-highlight-region (beg end _old-len)
13783 "Colorize included files and modules in the (changed?) region.
13784 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
13785 (when (or verilog-highlight-includes
13786 verilog-highlight-modules)
13787 (save-excursion
13788 (save-match-data ;; A query-replace may call this function - do not disturb
13789 (verilog-save-buffer-state
13790 (verilog-save-scan-cache
13791 (let (end-point)
13792 (goto-char end)
13793 (setq end-point (point-at-eol))
13794 (goto-char beg)
13795 (beginning-of-line) ; scan entire line
13796 ;; delete overlays existing on this line
13797 (let ((overlays (overlays-in (point) end-point)))
13798 (while overlays
13799 (if (and
13800 (overlay-get (car overlays) 'detachable)
13801 (or (overlay-get (car overlays) 'verilog-include-file)
13802 (overlay-get (car overlays) 'verilog-inst-module)))
13803 (delete-overlay (car overlays)))
13804 (setq overlays (cdr overlays))))
13805 ;;
13806 ;; make new include overlays
13807 (when verilog-highlight-includes
13808 (while (search-forward-regexp verilog-include-file-regexp end-point t)
13809 (goto-char (match-beginning 1))
13810 (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
13811 (overlay-put ov 'start-closed 't)
13812 (overlay-put ov 'end-closed 't)
13813 (overlay-put ov 'evaporate 't)
13814 (overlay-put ov 'verilog-include-file 't)
13815 (overlay-put ov 'mouse-face 'highlight)
13816 (overlay-put ov 'local-map verilog-mode-mouse-map))))
13817 ;;
13818 ;; make new module overlays
13819 (goto-char beg)
13820 ;; This scanner is syntax-fragile, so don't get bent
13821 (when verilog-highlight-modules
13822 (condition-case nil
13823 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
13824 (save-excursion
13825 (goto-char (match-beginning 0))
13826 (unless (verilog-inside-comment-or-string-p)
13827 (verilog-read-inst-module-matcher) ;; sets match 0
13828 (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
13829 (overlay-put ov 'start-closed 't)
13830 (overlay-put ov 'end-closed 't)
13831 (overlay-put ov 'evaporate 't)
13832 (overlay-put ov 'verilog-inst-module 't)
13833 (overlay-put ov 'mouse-face 'highlight)
13834 (overlay-put ov 'local-map verilog-mode-mouse-map)))))
13835 (error nil)))
13836 ;;
13837 ;; Future highlights:
13838 ;; variables - make an Occur buffer of where referenced
13839 ;; pins - make an Occur buffer of the sig in the declaration module
13840 )))))))
13841
13842 (defun verilog-highlight-buffer ()
13843 "Colorize included files and modules across the whole buffer."
13844 ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
13845 (interactive)
13846 ;; delete and remake overlays
13847 (verilog-highlight-region (point-min) (point-max) nil))
13848
13849 ;; Deprecated, but was interactive, so we'll keep it around
13850 (defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
13851
13852 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
13853 ;; so define this function to do more or less the same as ffap-at-mouse
13854 ;; but first resolve filename...
13855 (defun verilog-load-file-at-mouse (event)
13856 "Load file under button 2 click's EVENT.
13857 Files are checked based on `verilog-library-flags'."
13858 (interactive "@e")
13859 (save-excursion ;; implement a Verilog specific ffap-at-mouse
13860 (mouse-set-point event)
13861 (verilog-load-file-at-point t)))
13862
13863 ;; ffap isn't usable for Verilog mode. It uses library paths.
13864 ;; so define this function to do more or less the same as ffap
13865 ;; but first resolve filename...
13866 (defun verilog-load-file-at-point (&optional warn)
13867 "Load file under point.
13868 If WARN, throw warning if not found.
13869 Files are checked based on `verilog-library-flags'."
13870 (interactive)
13871 (save-excursion ;; implement a Verilog specific ffap
13872 (let ((overlays (overlays-in (point) (point)))
13873 hit)
13874 (while (and overlays (not hit))
13875 (when (overlay-get (car overlays) 'verilog-inst-module)
13876 (verilog-goto-defun-file (buffer-substring
13877 (overlay-start (car overlays))
13878 (overlay-end (car overlays))))
13879 (setq hit t))
13880 (setq overlays (cdr overlays)))
13881 ;; Include?
13882 (beginning-of-line)
13883 (when (and (not hit)
13884 (looking-at verilog-include-file-regexp))
13885 (if (and (car (verilog-library-filenames
13886 (match-string 1) (buffer-file-name)))
13887 (file-readable-p (car (verilog-library-filenames
13888 (match-string 1) (buffer-file-name)))))
13889 (find-file (car (verilog-library-filenames
13890 (match-string 1) (buffer-file-name))))
13891 (when warn
13892 (message
13893 "File '%s' isn't readable, use shift-mouse2 to paste in this field"
13894 (match-string 1))))))))
13895
13896 ;;
13897 ;; Bug reporting
13898 ;;
13899
13900 (defun verilog-faq ()
13901 "Tell the user their current version, and where to get the FAQ etc."
13902 (interactive)
13903 (with-output-to-temp-buffer "*verilog-mode help*"
13904 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
13905 (princ "\n")
13906 (princ "For new releases, see http://www.verilog.com\n")
13907 (princ "\n")
13908 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
13909 (princ "\n")
13910 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
13911 (princ "\n")))
13912
13913 (autoload 'reporter-submit-bug-report "reporter")
13914 (defvar reporter-prompt-for-summary-p)
13915
13916 (defun verilog-submit-bug-report ()
13917 "Submit via mail a bug report on verilog-mode.el."
13918 (interactive)
13919 (let ((reporter-prompt-for-summary-p t))
13920 (reporter-submit-bug-report
13921 "mac@verilog.com, wsnyder@wsnyder.org"
13922 (concat "verilog-mode v" verilog-mode-version)
13923 '(
13924 verilog-active-low-regexp
13925 verilog-after-save-font-hook
13926 verilog-align-ifelse
13927 verilog-assignment-delay
13928 verilog-auto-arg-sort
13929 verilog-auto-declare-nettype
13930 verilog-auto-delete-trailing-whitespace
13931 verilog-auto-endcomments
13932 verilog-auto-hook
13933 verilog-auto-ignore-concat
13934 verilog-auto-indent-on-newline
13935 verilog-auto-inout-ignore-regexp
13936 verilog-auto-input-ignore-regexp
13937 verilog-auto-inst-column
13938 verilog-auto-inst-dot-name
13939 verilog-auto-inst-interfaced-ports
13940 verilog-auto-inst-param-value
13941 verilog-auto-inst-sort
13942 verilog-auto-inst-template-numbers
13943 verilog-auto-inst-vector
13944 verilog-auto-lineup
13945 verilog-auto-newline
13946 verilog-auto-output-ignore-regexp
13947 verilog-auto-read-includes
13948 verilog-auto-reset-blocking-in-non
13949 verilog-auto-reset-widths
13950 verilog-auto-save-policy
13951 verilog-auto-sense-defines-constant
13952 verilog-auto-sense-include-inputs
13953 verilog-auto-star-expand
13954 verilog-auto-star-save
13955 verilog-auto-template-warn-unused
13956 verilog-auto-tieoff-declaration
13957 verilog-auto-tieoff-ignore-regexp
13958 verilog-auto-unused-ignore-regexp
13959 verilog-auto-wire-type
13960 verilog-before-auto-hook
13961 verilog-before-delete-auto-hook
13962 verilog-before-getopt-flags-hook
13963 verilog-before-save-font-hook
13964 verilog-cache-enabled
13965 verilog-case-fold
13966 verilog-case-indent
13967 verilog-cexp-indent
13968 verilog-compiler
13969 verilog-coverage
13970 verilog-delete-auto-hook
13971 verilog-getopt-flags-hook
13972 verilog-highlight-grouping-keywords
13973 verilog-highlight-includes
13974 verilog-highlight-modules
13975 verilog-highlight-p1800-keywords
13976 verilog-highlight-translate-off
13977 verilog-indent-begin-after-if
13978 verilog-indent-declaration-macros
13979 verilog-indent-level
13980 verilog-indent-level-behavioral
13981 verilog-indent-level-declaration
13982 verilog-indent-level-directive
13983 verilog-indent-level-module
13984 verilog-indent-lists
13985 verilog-library-directories
13986 verilog-library-extensions
13987 verilog-library-files
13988 verilog-library-flags
13989 verilog-linter
13990 verilog-minimum-comment-distance
13991 verilog-mode-hook
13992 verilog-mode-release-emacs
13993 verilog-mode-version
13994 verilog-preprocessor
13995 verilog-simulator
13996 verilog-tab-always-indent
13997 verilog-tab-to-comment
13998 verilog-typedef-regexp
13999 verilog-warn-fatal
14000 )
14001 nil nil
14002 (concat "Hi Mac,
14003
14004 I want to report a bug.
14005
14006 Before I go further, I want to say that Verilog mode has changed my life.
14007 I save so much time, my files are colored nicely, my co workers respect
14008 my coding ability... until now. I'd really appreciate anything you
14009 could do to help me out with this minor deficiency in the product.
14010
14011 I've taken a look at the Verilog-Mode FAQ at
14012 http://www.veripool.org/verilog-mode-faq.html.
14013
14014 And, I've considered filing the bug on the issue tracker at
14015 http://www.veripool.org/verilog-mode-bugs
14016 since I realize that public bugs are easier for you to track,
14017 and for others to search, but would prefer to email.
14018
14019 So, to reproduce the bug, start a fresh Emacs via " invocation-name "
14020 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
14021 the code included below.
14022
14023 Given those lines, I expected [[Fill in here]] to happen;
14024 but instead, [[Fill in here]] happens!.
14025
14026 == The code: =="))))
14027
14028 (provide 'verilog-mode)
14029
14030 ;; Local Variables:
14031 ;; checkdoc-permit-comma-termination-flag:t
14032 ;; checkdoc-force-docstrings-flag:nil
14033 ;; End:
14034
14035 ;;; verilog-mode.el ends here