GDB change for dynamically generated code (tiny change)
[bpt/emacs.git] / lisp / progmodes / verilog-mode.el
CommitLineData
6341f357 1;; verilog-mode.el --- major mode for editing verilog source in Emacs
6341f357 2
acaf905b 3;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
6341f357 4
a03c2342
WS
5;; Author: Michael McNamara (mac@verilog.com),
6;; Wilson Snyder (wsnyder@wsnyder.org)
7;; Please see our web sites:
8;; http://www.verilog.com
9;; http://www.veripool.org
6341f357 10;;
6341f357
DN
11;; Keywords: languages
12
241760a3
SM
13;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
14;; file on 19/3/2008, and the maintainer agreed that when a bug is
15;; filed in the Emacs bug reporting system against this file, a copy
16;; of the bug report be sent to the maintainer's email address.
17
60618039
DN
18;; This code supports Emacs 21.1 and later
19;; And XEmacs 21.1 and later
20;; Please do not make changes that break Emacs 21. Thanks!
21;;
22;;
23
637b4d38
GM
24;; This file is part of GNU Emacs.
25
b1fc2b50 26;; GNU Emacs is free software: you can redistribute it and/or modify
6341f357 27;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
28;; the Free Software Foundation, either version 3 of the License, or
29;; (at your option) any later version.
6341f357 30
637b4d38 31;; GNU Emacs is distributed in the hope that it will be useful,
6341f357
DN
32;; but WITHOUT ANY WARRANTY; without even the implied warranty of
33;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34;; GNU General Public License for more details.
35
36;; You should have received a copy of the GNU General Public License
b1fc2b50 37;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6341f357
DN
38
39;;; Commentary:
40
60618039 41;; This mode borrows heavily from the Pascal-mode and the cc-mode of Emacs
6341f357
DN
42
43;; USAGE
44;; =====
45
60618039 46;; A major mode for editing Verilog HDL source code. When you have
6341f357
DN
47;; entered Verilog mode, you may get more info by pressing C-h m. You
48;; may also get online help describing various functions by: C-h f
49;; <Name of function you want described>
50
51;; KNOWN BUGS / BUG REPORTS
52;; =======================
53
54;; Verilog is a rapidly evolving language, and hence this mode is
60618039 55;; under continuous development. Hence this is beta code, and likely
a3a8b002
DN
56;; has bugs. Please report any issues to the issue tracker at
57;; http://www.veripool.org/verilog-mode
6341f357
DN
58;; Please use verilog-submit-bug-report to submit a report; type C-c
59;; C-b to invoke this and as a result I will have a much easier time
60;; of reproducing the bug you find, and hence fixing it.
61
62;; INSTALLING THE MODE
63;; ===================
64
65;; An older version of this mode may be already installed as a part of
66;; your environment, and one method of updating would be to update
60618039 67;; your Emacs environment. Sometimes this is difficult for local
6341f357
DN
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
37ea4b9b 76;; automatic Verilog mode, put this file in your load path, and put
6341f357
DN
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 )
fd9ea9d3 81; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
6341f357 82
a03c2342
WS
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;;
6341f357 86;; If you want to customize Verilog mode to fit your needs better,
a03c2342 87;; you may add the below lines (the values of the variables presented
60618039 88;; here are the defaults). Note also that if you use an Emacs that
6341f357 89;; supports custom, it's probably better to use the custom menu to
9489a450 90;; edit these. If working as a member of a large team these settings
a03c2342
WS
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.
6341f357 94;;
a03c2342
WS
95; ;; Enable syntax highlighting of **all** languages
96; (global-font-lock-mode t)
97;
6341f357
DN
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
a3a8b002 111; verilog-auto-lineup 'declarations
6341f357
DN
112; verilog-highlight-p1800-keywords nil
113; verilog-linter "my_lint_shell_command"
114; )
115
116;; \f
117
118;;; History:
37ea4b9b 119;;
7cb1c4d7 120;; See commit history at http://www.veripool.org/verilog-mode.html
60618039
DN
121;; (This section is required to appease checkdoc.)
122
6341f357
DN
123;;; Code:
124
6341f357 125;; This variable will always hold the version number of the mode
9489a450 126(defconst verilog-mode-version "725"
1418c701 127 "Version of this Verilog mode.")
9489a450 128(defconst verilog-mode-release-date "2011-11-27-GNU"
1418c701 129 "Release date of this Verilog mode.")
60618039 130(defconst verilog-mode-release-emacs t
1418c701 131 "If non-nil, this version of Verilog mode was released with Emacs itself.")
6341f357
DN
132
133(defun verilog-version ()
134 "Inform caller of the version of this file."
135 (interactive)
7ea26faf 136 (message "Using verilog-mode version %s" verilog-mode-version))
6341f357
DN
137
138;; Insure we have certain packages, and deal with it if we don't
60618039 139;; Be sure to note which Emacs flavor and version added each feature.
6edb5716 140(eval-when-compile
7cb1c4d7 141 ;; Provide stuff if we are XEmacs
7ea26faf
DN
142 (when (featurep 'xemacs)
143 (condition-case nil
144 (require 'easymenu)
145 (error nil))
146 (condition-case nil
147 (require 'regexp-opt)
148 (error nil))
149 ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
150 (condition-case nil
151 (load "skeleton")
152 (error nil))
153 (condition-case nil
154 (if (fboundp 'when)
155 nil ;; fab
156 (defmacro when (cond &rest body)
157 (list 'if cond (cons 'progn body))))
158 (error nil))
159 (condition-case nil
160 (if (fboundp 'unless)
161 nil ;; fab
162 (defmacro unless (cond &rest body)
163 (cons 'if (cons cond (cons nil body)))))
164 (error nil))
165 (condition-case nil
166 (if (fboundp 'store-match-data)
167 nil ;; fab
168 (defmacro store-match-data (&rest args) nil))
169 (error nil))
7ea26faf
DN
170 (condition-case nil
171 (if (fboundp 'char-before)
172 nil ;; great
173 (defmacro char-before (&rest body)
174 (char-after (1- (point)))))
175 (error nil))
9489a450
MM
176 (condition-case nil
177 (if (fboundp 'when)
178 nil ;; fab
179 (defsubst point-at-bol (&optional N)
180 (save-excursion (beginning-of-line N) (point))))
181 (error nil))
182 (condition-case nil
183 (if (fboundp 'when)
184 nil ;; fab
185 (defsubst point-at-eol (&optional N)
186 (save-excursion (end-of-line N) (point))))
187 (error nil))
7ea26faf
DN
188 (condition-case nil
189 (require 'custom)
190 (error nil))
191 (condition-case nil
192 (if (fboundp 'match-string-no-properties)
193 nil ;; great
194 (defsubst match-string-no-properties (num &optional string)
195 "Return string of text matched by last search, without text properties.
6341f357
DN
196NUM specifies which parenthesized expression in the last regexp.
197 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
198Zero means the entire text matched by the whole regexp or whole string.
199STRING should be given if the last search was by `string-match' on STRING."
7ea26faf
DN
200 (if (match-beginning num)
201 (if string
202 (let ((result
203 (substring string
204 (match-beginning num) (match-end num))))
205 (set-text-properties 0 (length result) nil result)
206 result)
207 (buffer-substring-no-properties (match-beginning num)
208 (match-end num)
60618039
DN
209 (current-buffer)))))
210 )
7ea26faf
DN
211 (error nil))
212 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
213 nil ;; We've got what we needed
214 ;; We have the old custom-library, hack around it!
215 (defmacro defgroup (&rest args) nil)
216 (defmacro customize (&rest args)
217 (message
37ea4b9b 218 "Sorry, Customize is not available with this version of Emacs"))
7ea26faf
DN
219 (defmacro defcustom (var value doc &rest args)
220 `(defvar ,var ,value ,doc))
221 )
222 (if (fboundp 'defface)
223 nil ; great!
224 (defmacro defface (var values doc &rest args)
225 `(make-face ,var))
226 )
6341f357 227
7ea26faf
DN
228 (if (and (featurep 'custom) (fboundp 'customize-group))
229 nil ;; We've got what we needed
230 ;; We have an intermediate custom-library, hack around it!
231 (defmacro customize-group (var &rest args)
232 `(customize ,var))
a03c2342
WS
233 )
234
235 (unless (boundp 'inhibit-point-motion-hooks)
236 (defvar inhibit-point-motion-hooks nil))
237 (unless (boundp 'deactivate-mark)
238 (defvar deactivate-mark nil))
239 )
240 ;;
7cb1c4d7
DN
241 ;; OK, do this stuff if we are NOT XEmacs:
242 (unless (featurep 'xemacs)
243 (unless (fboundp 'region-active-p)
244 (defmacro region-active-p ()
245 `(and transient-mark-mode mark-active))))
246 )
6341f357 247
6341f357
DN
248;; Provide a regular expression optimization routine, using regexp-opt
249;; if provided by the user's elisp libraries
250(eval-and-compile
60618039
DN
251 ;; The below were disabled when GNU Emacs 22 was released;
252 ;; perhaps some still need to be there to support Emacs 21.
7ea26faf
DN
253 (if (featurep 'xemacs)
254 (if (fboundp 'regexp-opt)
255 ;; regexp-opt is defined, does it take 3 or 2 arguments?
256 (if (fboundp 'function-max-args)
257 (let ((args (function-max-args `regexp-opt)))
258 (cond
259 ((eq args 3) ;; It takes 3
260 (condition-case nil ; Hide this defun from emacses
6341f357 261 ;with just a two input regexp
7ea26faf
DN
262 (defun verilog-regexp-opt (a b)
263 "Deal with differing number of required arguments for `regexp-opt'.
6341f357 264 Call 'regexp-opt' on A and B."
60618039 265 (regexp-opt a b 't))
7ea26faf
DN
266 (error nil))
267 )
268 ((eq args 2) ;; It takes 2
269 (defun verilog-regexp-opt (a b)
270 "Call 'regexp-opt' on A and B."
271 (regexp-opt a b))
272 )
273 (t nil)))
274 ;; We can't tell; assume it takes 2
275 (defun verilog-regexp-opt (a b)
276 "Call 'regexp-opt' on A and B."
277 (regexp-opt a b))
278 )
279 ;; There is no regexp-opt, provide our own
280 (defun verilog-regexp-opt (strings &optional paren shy)
281 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
282 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
283 )
284 ;; Emacs.
285 (defalias 'verilog-regexp-opt 'regexp-opt)))
6341f357 286
a03c2342
WS
287(eval-and-compile
288 ;; Both xemacs and emacs
9489a450
MM
289 (condition-case nil
290 (require 'diff) ;; diff-command and diff-switches
291 (error nil))
292 (condition-case nil
293 (require 'compile) ;; compilation-error-regexp-alist-alist
294 (error nil))
a03c2342
WS
295 (condition-case nil
296 (unless (fboundp 'buffer-chars-modified-tick) ;; Emacs 22 added
297 (defmacro buffer-chars-modified-tick () (buffer-modified-tick)))
9489a450
MM
298 (error nil))
299 ;; Added in Emacs 24.1
300 (condition-case nil
301 (unless (fboundp 'prog-mode)
302 (define-derived-mode prog-mode fundamental-mode "Prog"))
a03c2342
WS
303 (error nil)))
304
38f584e9
DN
305(eval-when-compile
306 (defun verilog-regexp-words (a)
307 "Call 'regexp-opt' with word delimiters for the words A."
308 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
a03c2342
WS
309(defun verilog-regexp-words (a)
310 "Call 'regexp-opt' with word delimiters for the words A."
311 ;; The FAQ references this function, so user LISP sometimes calls it
312 (concat "\\<" (verilog-regexp-opt a t) "\\>"))
6341f357 313
495ab0d5 314(defun verilog-easy-menu-filter (menu)
a3a8b002 315 "Filter `easy-menu-define' MENU to support new features."
495ab0d5
DN
316 (cond ((not (featurep 'xemacs))
317 menu) ;; GNU Emacs - passthru
0d26e0b6 318 ;; XEmacs doesn't support :help. Strip it.
495ab0d5
DN
319 ;; Recursively filter the a submenu
320 ((listp menu)
321 (mapcar 'verilog-easy-menu-filter menu))
322 ;; Look for [:help "blah"] and remove
323 ((vectorp menu)
324 (let ((i 0) (out []))
325 (while (< i (length menu))
326 (if (equal `:help (aref menu i))
327 (setq i (+ 2 i))
328 (setq out (vconcat out (vector (aref menu i)))
329 i (1+ i))))
330 out))
331 (t menu))) ;; Default - ok
332;;(verilog-easy-menu-filter
333;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
334;; "----" ["MB" nil :help "Help MB"]))
335
9489a450
MM
336(defun verilog-define-abbrev (table name expansion &optional hook)
337 "Filter `define-abbrev' TABLE NAME EXPANSION and call HOOK.
338Provides SYSTEM-FLAG in newer Emacs."
339 (condition-case nil
340 (define-abbrev table name expansion hook 0 t)
341 (error
342 (define-abbrev table name expansion hook))))
343
6341f357 344(defun verilog-customize ()
495ab0d5 345 "Customize variables and other settings used by Verilog-Mode."
6341f357
DN
346 (interactive)
347 (customize-group 'verilog-mode))
348
349(defun verilog-font-customize ()
495ab0d5 350 "Customize fonts used by Verilog-Mode."
6341f357 351 (interactive)
7ea26faf
DN
352 (if (fboundp 'customize-apropos)
353 (customize-apropos "font-lock-*" 'faces)))
6341f357 354
eaf7efe9
DN
355(defun verilog-booleanp (value)
356 "Return t if VALUE is boolean.
a3a8b002
DN
357This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
358This function may be removed when Emacs 21 is no longer supported."
eaf7efe9
DN
359 (or (equal value t) (equal value nil)))
360
a3a8b002
DN
361(defun verilog-insert-last-command-event ()
362 "Insert the `last-command-event'."
363 (insert (if (featurep 'xemacs)
364 ;; XEmacs 21.5 doesn't like last-command-event
365 last-command-char
366 ;; And GNU Emacs 22 has obsoleted last-command-char
367 last-command-event)))
368
9489a450
MM
369(defvar verilog-no-change-functions nil
370 "True if `after-change-functions' is disabled.
371Use of `syntax-ppss' may break, as ppss's cache may get corrupted.")
372
373(defvar verilog-in-hooks nil
374 "True when within a `verilog-run-hooks' block.")
375
376(defmacro verilog-run-hooks (&rest hooks)
377 "Run each hook in HOOKS using `run-hooks'.
378Set `verilog-in-hooks' during this time, to assist AUTO caches."
379 `(let ((verilog-in-hooks t))
380 (run-hooks ,@hooks)))
381
382(defun verilog-syntax-ppss (&optional pos)
383 (when verilog-no-change-functions
384 (if verilog-in-hooks
385 (verilog-scan-cache-flush)
386 ;; else don't let the AUTO code itself get away with flushing the cache,
387 ;; as that'll make things very slow
388 (backtrace)
389 (error "%s: Internal problem; use of syntax-ppss when cache may be corrupt"
390 (verilog-point-text))))
391 (if (fboundp 'syntax-ppss)
392 (syntax-ppss pos)
393 (parse-partial-sexp (point-min) (or pos (point)))))
d63b01e1 394
6341f357 395(defgroup verilog-mode nil
37ea4b9b 396 "Facilitates easy editing of Verilog source text."
8e788369 397 :version "22.2"
6341f357
DN
398 :group 'languages)
399
400; (defgroup verilog-mode-fonts nil
401; "Facilitates easy customization fonts used in Verilog source text"
402; :link '(customize-apropos "font-lock-*" 'faces)
403; :group 'verilog-mode)
404
405(defgroup verilog-mode-indent nil
37ea4b9b 406 "Customize indentation and highlighting of Verilog source text."
6341f357
DN
407 :group 'verilog-mode)
408
409(defgroup verilog-mode-actions nil
37ea4b9b 410 "Customize actions on Verilog source text."
6341f357
DN
411 :group 'verilog-mode)
412
413(defgroup verilog-mode-auto nil
37ea4b9b 414 "Customize AUTO actions when expanding Verilog source text."
6341f357
DN
415 :group 'verilog-mode)
416
a03c2342
WS
417(defvar verilog-debug nil
418 "If set, enable debug messages for `verilog-mode' internals.")
419
6341f357
DN
420(defcustom verilog-linter
421 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
37ea4b9b 422 "*Unix program and arguments to call to run a lint checker on Verilog source.
6341f357
DN
423Depending on the `verilog-set-compile-command', this may be invoked when
424you type \\[compile]. When the compile completes, \\[next-error] will take
425you to the next lint error."
426 :type 'string
427 :group 'verilog-mode-actions)
60618039 428;; We don't mark it safe, as it's used as a shell command
6341f357
DN
429
430(defcustom verilog-coverage
431 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
37ea4b9b 432 "*Program and arguments to use to annotate for coverage Verilog source.
6341f357
DN
433Depending on the `verilog-set-compile-command', this may be invoked when
434you type \\[compile]. When the compile completes, \\[next-error] will take
435you to the next lint error."
436 :type 'string
437 :group 'verilog-mode-actions)
60618039 438;; We don't mark it safe, as it's used as a shell command
6341f357
DN
439
440(defcustom verilog-simulator
441 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
37ea4b9b 442 "*Program and arguments to use to interpret Verilog source.
6341f357
DN
443Depending on the `verilog-set-compile-command', this may be invoked when
444you type \\[compile]. When the compile completes, \\[next-error] will take
445you to the next lint error."
446 :type 'string
447 :group 'verilog-mode-actions)
60618039 448;; We don't mark it safe, as it's used as a shell command
6341f357
DN
449
450(defcustom verilog-compiler
451 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
37ea4b9b 452 "*Program and arguments to use to compile Verilog source.
6341f357
DN
453Depending on the `verilog-set-compile-command', this may be invoked when
454you type \\[compile]. When the compile completes, \\[next-error] will take
455you to the next lint error."
456 :type 'string
457 :group 'verilog-mode-actions)
60618039 458;; We don't mark it safe, as it's used as a shell command
6341f357 459
a03c2342
WS
460(defcustom verilog-preprocessor
461 ;; Very few tools give preprocessed output, so we'll default to Verilog-Perl
462 "vppreproc __FLAGS__ __FILE__"
463 "*Program and arguments to use to preprocess Verilog source.
464This is invoked with `verilog-preprocess', and depending on the
465`verilog-set-compile-command', may also be invoked when you type
466\\[compile]. When the compile completes, \\[next-error] will
467take you to the next lint error."
468 :type 'string
469 :group 'verilog-mode-actions)
470;; We don't mark it safe, as it's used as a shell command
471
472(defvar verilog-preprocess-history nil
473 "History for `verilog-preprocess'.")
474
6341f357
DN
475(defvar verilog-tool 'verilog-linter
476 "Which tool to use for building compiler-command.
a03c2342
WS
477Either nil, `verilog-linter, `verilog-compiler,
478`verilog-coverage, `verilog-preprocessor, or `verilog-simulator.
479Alternatively use the \"Choose Compilation Action\" menu. See
480`verilog-set-compile-command' for more information.")
6341f357
DN
481
482(defcustom verilog-highlight-translate-off nil
483 "*Non-nil means background-highlight code excluded from translation.
484That is, all code between \"// synopsys translate_off\" and
485\"// synopsys translate_on\" is highlighted using a different background color
486\(face `verilog-font-lock-translate-off-face').
487
488Note: This will slow down on-the-fly fontification (and thus editing).
489
490Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
491entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
492 :type 'boolean
493 :group 'verilog-mode-indent)
60618039 494;; Note we don't use :safe, as that would break on Emacsen before 22.0.
eaf7efe9 495(put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
6341f357 496
a3a8b002
DN
497(defcustom verilog-auto-lineup 'declarations
498 "*Type of statements to lineup across multiple lines.
499If 'all' is selected, then all line ups described below are done.
500
501If 'declaration', then just declarations are lined up with any
502preceding declarations, taking into account widths and the like,
503so or example the code:
504 reg [31:0] a;
505 reg b;
506would become
507 reg [31:0] a;
508 reg b;
509
510If 'assignment', then assignments are lined up with any preceding
511assignments, so for example the code
512 a_long_variable <= b + c;
513 d = e + f;
514would become
515 a_long_variable <= b + c;
516 d = e + f;
517
518In order to speed up editing, large blocks of statements are lined up
519only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
520are lineup only when \\[verilog-pretty-declarations] is typed."
521
522 :type '(radio (const :tag "Line up Assignments and Declarations" all)
523 (const :tag "Line up Assignment statements" assignments )
86a4c7ac 524 (const :tag "Line up Declarations" declarations)
a3a8b002
DN
525 (function :tag "Other"))
526 :group 'verilog-mode-indent )
527
6341f357
DN
528(defcustom verilog-indent-level 3
529 "*Indentation of Verilog statements with respect to containing block."
530 :group 'verilog-mode-indent
531 :type 'integer)
60618039 532(put 'verilog-indent-level 'safe-local-variable 'integerp)
6341f357
DN
533
534(defcustom verilog-indent-level-module 3
37ea4b9b 535 "*Indentation of Module level Verilog statements (eg always, initial).
6341f357
DN
536Set to 0 to get initial and always statements lined up on the left side of
537your screen."
538 :group 'verilog-mode-indent
539 :type 'integer)
60618039 540(put 'verilog-indent-level-module 'safe-local-variable 'integerp)
6341f357
DN
541
542(defcustom verilog-indent-level-declaration 3
543 "*Indentation of declarations with respect to containing block.
544Set to 0 to get them list right under containing block."
545 :group 'verilog-mode-indent
546 :type 'integer)
60618039 547(put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
6341f357
DN
548
549(defcustom verilog-indent-declaration-macros nil
550 "*How to treat macro expansions in a declaration.
551If nil, indent as:
552 input [31:0] a;
553 input `CP;
554 output c;
555If non nil, treat as:
556 input [31:0] a;
557 input `CP ;
558 output c;"
559 :group 'verilog-mode-indent
560 :type 'boolean)
eaf7efe9 561(put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
6341f357
DN
562
563(defcustom verilog-indent-lists t
564 "*How to treat indenting items in a list.
565If t (the default), indent as:
566 always @( posedge a or
567 reset ) begin
568
569If nil, treat as:
570 always @( posedge a or
571 reset ) begin"
572 :group 'verilog-mode-indent
573 :type 'boolean)
eaf7efe9 574(put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
6341f357
DN
575
576(defcustom verilog-indent-level-behavioral 3
577 "*Absolute indentation of first begin in a task or function block.
578Set to 0 to get such code to start at the left side of the screen."
579 :group 'verilog-mode-indent
580 :type 'integer)
60618039 581(put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
6341f357
DN
582
583(defcustom verilog-indent-level-directive 1
584 "*Indentation to add to each level of `ifdef declarations.
585Set to 0 to have all directives start at the left side of the screen."
586 :group 'verilog-mode-indent
587 :type 'integer)
60618039 588(put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
6341f357
DN
589
590(defcustom verilog-cexp-indent 2
591 "*Indentation of Verilog statements split across lines."
592 :group 'verilog-mode-indent
593 :type 'integer)
60618039 594(put 'verilog-cexp-indent 'safe-local-variable 'integerp)
6341f357
DN
595
596(defcustom verilog-case-indent 2
597 "*Indentation for case statements."
598 :group 'verilog-mode-indent
599 :type 'integer)
60618039 600(put 'verilog-case-indent 'safe-local-variable 'integerp)
6341f357
DN
601
602(defcustom verilog-auto-newline t
603 "*True means automatically newline after semicolons."
604 :group 'verilog-mode-indent
605 :type 'boolean)
eaf7efe9 606(put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
6341f357
DN
607
608(defcustom verilog-auto-indent-on-newline t
609 "*True means automatically indent line after newline."
610 :group 'verilog-mode-indent
611 :type 'boolean)
eaf7efe9 612(put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
6341f357
DN
613
614(defcustom verilog-tab-always-indent t
615 "*True means TAB should always re-indent the current line.
37ea4b9b 616A nil value means TAB will only reindent when at the beginning of the line."
6341f357
DN
617 :group 'verilog-mode-indent
618 :type 'boolean)
eaf7efe9 619(put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
6341f357
DN
620
621(defcustom verilog-tab-to-comment nil
622 "*True means TAB moves to the right hand column in preparation for a comment."
623 :group 'verilog-mode-actions
624 :type 'boolean)
eaf7efe9 625(put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
6341f357
DN
626
627(defcustom verilog-indent-begin-after-if t
628 "*If true, indent begin statements following if, else, while, for and repeat.
629Otherwise, line them up."
630 :group 'verilog-mode-indent
60618039 631 :type 'boolean)
eaf7efe9 632(put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
6341f357
DN
633
634
635(defcustom verilog-align-ifelse nil
636 "*If true, align `else' under matching `if'.
637Otherwise else is lined up with first character on line holding matching if."
638 :group 'verilog-mode-indent
60618039 639 :type 'boolean)
eaf7efe9 640(put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
6341f357
DN
641
642(defcustom verilog-minimum-comment-distance 10
643 "*Minimum distance (in lines) between begin and end required before a comment.
644Setting this variable to zero results in every end acquiring a comment; the
37ea4b9b 645default avoids too many redundant comments in tight quarters."
6341f357
DN
646 :group 'verilog-mode-indent
647 :type 'integer)
60618039 648(put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
6341f357 649
6341f357 650(defcustom verilog-highlight-p1800-keywords nil
60618039
DN
651 "*True means highlight words newly reserved by IEEE-1800.
652These will appear in `verilog-font-lock-p1800-face' in order to gently
653suggest changing where these words are used as variables to something else.
37ea4b9b 654A nil value means highlight these words as appropriate for the SystemVerilog
60618039 655IEEE-1800 standard. Note that changing this will require restarting Emacs
37ea4b9b 656to see the effect as font color choices are cached by Emacs."
6341f357
DN
657 :group 'verilog-mode-indent
658 :type 'boolean)
eaf7efe9 659(put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
6341f357 660
a1ebd734
DN
661(defcustom verilog-highlight-grouping-keywords nil
662 "*True means highlight grouping keywords 'begin' and 'end' more dramatically.
a3a8b002
DN
663If false, these words are in the `font-lock-type-face'; if True then they are in
664`verilog-font-lock-ams-face'. Some find that special highlighting on these
a1ebd734
DN
665grouping constructs allow the structure of the code to be understood at a glance."
666 :group 'verilog-mode-indent
667 :type 'boolean)
b1d0fc86 668(put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
a1ebd734 669
a03c2342
WS
670(defcustom verilog-highlight-modules nil
671 "*True means highlight module statements for `verilog-load-file-at-point'.
672When true, mousing over module names will allow jumping to the
673module definition. If false, this is not supported. Setting
674this is experimental, and may lead to bad performance."
675 :group 'verilog-mode-indent
676 :type 'boolean)
677(put 'verilog-highlight-modules 'safe-local-variable 'verilog-booleanp)
678
679(defcustom verilog-highlight-includes t
680 "*True means highlight module statements for `verilog-load-file-at-point'.
681When true, mousing over include file names will allow jumping to the
682file referenced. If false, this is not supported."
683 :group 'verilog-mode-indent
684 :type 'boolean)
685(put 'verilog-highlight-includes 'safe-local-variable 'verilog-booleanp)
686
9489a450
MM
687(defcustom verilog-auto-declare-nettype nil
688 "*Non-nil specifies the data type to use with `verilog-auto-input' etc.
689Set this to \"wire\" if the Verilog code uses \"`default_nettype
690none\". Note using `default_nettype none isn't recommended practice; this
691mode is experimental."
2bed3f04 692 :version "24.1"
9489a450
MM
693 :group 'verilog-mode-actions
694 :type 'boolean)
695(put 'verilog-auto-declare-nettype 'safe-local-variable `stringp)
696
697(defcustom verilog-auto-wire-type nil
698 "*Non-nil specifies the data type to use with `verilog-auto-wire' etc.
699Set this to \"logic\" for SystemVerilog code, or use `verilog-auto-logic'."
2bed3f04 700 :version "24.1"
9489a450
MM
701 :group 'verilog-mode-actions
702 :type 'boolean)
703(put 'verilog-auto-wire-type 'safe-local-variable `stringp)
704
6341f357
DN
705(defcustom verilog-auto-endcomments t
706 "*True means insert a comment /* ... */ after 'end's.
707The name of the function or case will be set between the braces."
708 :group 'verilog-mode-actions
60618039 709 :type 'boolean)
eaf7efe9 710(put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
6341f357 711
9489a450
MM
712(defcustom verilog-auto-delete-trailing-whitespace nil
713 "*True means to `delete-trailing-whitespace' in `verilog-auto'."
2bed3f04 714 :version "24.1"
9489a450
MM
715 :group 'verilog-mode-actions
716 :type 'boolean)
717(put 'verilog-auto-delete-trailing-whitespace 'safe-local-variable 'verilog-booleanp)
718
14862301
SM
719(defcustom verilog-auto-ignore-concat nil
720 "*True means ignore signals in {...} concatenations for AUTOWIRE etc.
721This will exclude signals referenced as pin connections in {...}
722from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
723for backward compatibility only and not set in new designs; it
724may be removed in future versions."
725 :group 'verilog-mode-actions
726 :type 'boolean)
727(put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
728
6341f357
DN
729(defcustom verilog-auto-read-includes nil
730 "*True means to automatically read includes before AUTOs.
731This will do a `verilog-read-defines' and `verilog-read-includes' before
732each AUTO expansion. This makes it easier to embed defines and includes,
733but can result in very slow reading times if there are many or large
734include files."
735 :group 'verilog-mode-actions
60618039 736 :type 'boolean)
eaf7efe9 737(put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
6341f357
DN
738
739(defcustom verilog-auto-save-policy nil
740 "*Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
741A value of `force' will always do a \\[verilog-auto] automatically if
742needed on every save. A value of `detect' will do \\[verilog-auto]
743automatically when it thinks necessary. A value of `ask' will query the
744user when it thinks updating is needed.
745
746You should not rely on the 'ask or 'detect policies, they are safeguards
747only. They do not detect when AUTOINSTs need to be updated because a
748sub-module's port list has changed."
749 :group 'verilog-mode-actions
750 :type '(choice (const nil) (const ask) (const detect) (const force)))
751
752(defcustom verilog-auto-star-expand t
753 "*Non-nil indicates to expand a SystemVerilog .* instance ports.
754They will be expanded in the same way as if there was a AUTOINST in the
755instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
756 :group 'verilog-mode-actions
757 :type 'boolean)
eaf7efe9 758(put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
6341f357
DN
759
760(defcustom verilog-auto-star-save nil
761 "*Non-nil indicates to save to disk SystemVerilog .* instance expansions.
37ea4b9b
JB
762A nil value indicates direct connections will be removed before saving.
763Only meaningful to those created due to `verilog-auto-star-expand' being set.
6341f357
DN
764
765Instead of setting this, you may want to use /*AUTOINST*/, which will
766always be saved."
767 :group 'verilog-mode-actions
768 :type 'boolean)
eaf7efe9 769(put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
6341f357
DN
770
771(defvar verilog-auto-update-tick nil
772 "Modification tick at which autos were last performed.")
773
774(defvar verilog-auto-last-file-locals nil
775 "Text from file-local-variables during last evaluation.")
776
9489a450
MM
777(defvar verilog-diff-function 'verilog-diff-report
778 "*Function to run when `verilog-diff-auto' detects differences.
779Function takes three arguments, the original buffer, the
780difference buffer, and the point in original buffer with the
781first difference.")
782
a3a8b002
DN
783;;; Compile support
784(require 'compile)
d88782c3 785(defvar verilog-error-regexp-added nil)
a03c2342 786
d88782c3
DN
787(defvar verilog-error-regexp-emacs-alist
788 '(
789 (verilog-xl-1
790 "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
791 (verilog-xl-2
792 "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
793 (verilog-IES
a03c2342 794 ".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)" 2 3)
d88782c3
DN
795 (verilog-surefire-1
796 "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
797 (verilog-surefire-2
798 "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
799 (verilog-verbose
800 "\
801\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
802:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
803 (verilog-xsim
804 "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
805 (verilog-vcs-1
806 "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
807 (verilog-vcs-2
808 "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
809 (verilog-vcs-3
810 "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
811 (verilog-vcs-4
812 "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
813 (verilog-verilator
814 "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
815 (verilog-leda
a03c2342 816 "^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 2)
d88782c3 817 )
a03c2342
WS
818 "List of regexps for Verilog compilers.
819See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
820
821(defvar verilog-error-regexp-xemacs-alist
822 ;; Emacs form is '((v-tool "re" 1 2) ...)
823 ;; XEmacs form is '(verilog ("re" 1 2) ...)
0d26e0b6 824 ;; So we can just map from Emacs to XEmacs
a03c2342
WS
825 (cons 'verilog (mapcar 'cdr verilog-error-regexp-emacs-alist))
826 "List of regexps for Verilog compilers.
827See `compilation-error-regexp-alist-alist' for the formatting. For XEmacs.")
6341f357
DN
828
829(defvar verilog-error-font-lock-keywords
830 '(
a03c2342
WS
831 ;; verilog-xl-1
832 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
833 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
834 ;; verilog-xl-2
835 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 bold t)
836 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 3 bold t)
837 ;; verilog-IES (nc-verilog)
838 (".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
839 (".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 3 bold t)
840 ;; verilog-surefire-1
6341f357
DN
841 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
842 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
a03c2342 843 ;; verilog-surefire-2
6341f357
DN
844 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
845 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
a03c2342 846 ;; verilog-verbose
6341f357
DN
847 ("\
848\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
849:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
850 ("\
851\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
852:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
a03c2342 853 ;; verilog-vcs-1
6341f357
DN
854 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
855 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
a03c2342 856 ;; verilog-vcs-2
6341f357
DN
857 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
858 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
a03c2342 859 ;; verilog-vcs-3
6341f357
DN
860 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
861 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
a03c2342 862 ;; verilog-vcs-4
6341f357
DN
863 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
864 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
a03c2342
WS
865 ;; verilog-verilator
866 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
867 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
868 ;; verilog-leda
869 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 bold t)
870 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 2 bold t)
6341f357 871 )
a03c2342
WS
872 "*Keywords to also highlight in Verilog *compilation* buffers.
873Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
6341f357
DN
874
875(defcustom verilog-library-flags '("")
876 "*List of standard Verilog arguments to use for /*AUTOINST*/.
877These arguments are used to find files for `verilog-auto', and match
878the flags accepted by a standard Verilog-XL simulator.
879
880 -f filename Reads more `verilog-library-flags' from the filename.
881 +incdir+dir Adds the directory to `verilog-library-directories'.
882 -Idir Adds the directory to `verilog-library-directories'.
883 -y dir Adds the directory to `verilog-library-directories'.
884 +libext+.v Adds the extensions to `verilog-library-extensions'.
885 -v filename Adds the filename to `verilog-library-files'.
886
887 filename Adds the filename to `verilog-library-files'.
888 This is not recommended, -v is a better choice.
889
890You might want these defined in each file; put at the *END* of your file
891something like:
892
893 // Local Variables:
894 // verilog-library-flags:(\"-y dir -y otherdir\")
895 // End:
896
897Verilog-mode attempts to detect changes to this local variable, but they
37ea4b9b 898are only insured to be correct when the file is first visited. Thus if you
6341f357
DN
899have problems, use \\[find-alternate-file] RET to have these take effect.
900
901See also the variables mentioned above."
902 :group 'verilog-mode-auto
903 :type '(repeat string))
60618039 904(put 'verilog-library-flags 'safe-local-variable 'listp)
6341f357
DN
905
906(defcustom verilog-library-directories '(".")
907 "*List of directories when looking for files for /*AUTOINST*/.
908The directory may be relative to the current file, or absolute.
909Environment variables are also expanded in the directory names.
910Having at least the current directory is a good idea.
911
912You might want these defined in each file; put at the *END* of your file
913something like:
914
915 // Local Variables:
916 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
917 // End:
918
919Verilog-mode attempts to detect changes to this local variable, but they
37ea4b9b 920are only insured to be correct when the file is first visited. Thus if you
6341f357
DN
921have problems, use \\[find-alternate-file] RET to have these take effect.
922
923See also `verilog-library-flags', `verilog-library-files'
924and `verilog-library-extensions'."
925 :group 'verilog-mode-auto
926 :type '(repeat file))
60618039 927(put 'verilog-library-directories 'safe-local-variable 'listp)
6341f357
DN
928
929(defcustom verilog-library-files '()
60618039
DN
930 "*List of files to search for modules.
931AUTOINST will use this when it needs to resolve a module name.
6341f357
DN
932This is a complete path, usually to a technology file with many standard
933cells defined in it.
934
935You might want these defined in each file; put at the *END* of your file
936something like:
937
938 // Local Variables:
939 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
940 // End:
941
942Verilog-mode attempts to detect changes to this local variable, but they
37ea4b9b 943are only insured to be correct when the file is first visited. Thus if you
6341f357
DN
944have problems, use \\[find-alternate-file] RET to have these take effect.
945
946See also `verilog-library-flags', `verilog-library-directories'."
947 :group 'verilog-mode-auto
948 :type '(repeat directory))
60618039 949(put 'verilog-library-files 'safe-local-variable 'listp)
6341f357 950
7cb1c4d7 951(defcustom verilog-library-extensions '(".v" ".sv")
6341f357
DN
952 "*List of extensions to use when looking for files for /*AUTOINST*/.
953See also `verilog-library-flags', `verilog-library-directories'."
954 :type '(repeat string)
955 :group 'verilog-mode-auto)
60618039 956(put 'verilog-library-extensions 'safe-local-variable 'listp)
6341f357
DN
957
958(defcustom verilog-active-low-regexp nil
959 "*If set, treat signals matching this regexp as active low.
960This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
961you will probably also need `verilog-auto-reset-widths' set."
962 :group 'verilog-mode-auto
963 :type 'string)
60618039 964(put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
6341f357
DN
965
966(defcustom verilog-auto-sense-include-inputs nil
967 "*If true, AUTOSENSE should include all inputs.
968If nil, only inputs that are NOT output signals in the same block are
969included."
60618039
DN
970 :group 'verilog-mode-auto
971 :type 'boolean)
eaf7efe9 972(put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
6341f357
DN
973
974(defcustom verilog-auto-sense-defines-constant nil
975 "*If true, AUTOSENSE should assume all defines represent constants.
976When true, the defines will not be included in sensitivity lists. To
977maintain compatibility with other sites, this should be set at the bottom
37ea4b9b 978of each Verilog file that requires it, rather than being set globally."
60618039
DN
979 :group 'verilog-mode-auto
980 :type 'boolean)
eaf7efe9 981(put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
6341f357 982
9489a450
MM
983(defcustom verilog-auto-reset-blocking-in-non t
984 "*If true, AUTORESET will reset those signals which were
985assigned with blocking assignments (=) even in a block with
986non-blocking assignments (<=).
301b181a 987
9489a450
MM
988If nil, all blocking assigned signals are ignored when any
989non-blocking assignment is in the AUTORESET block. This allows
990blocking assignments to be used for temporary values and not have
991those temporaries reset. See example in `verilog-auto-reset'."
2bed3f04 992 :version "24.1"
9489a450
MM
993 :type 'boolean
994 :group 'verilog-mode-auto)
995(put 'verilog-auto-reset-blocking-in-non 'safe-local-variable 'verilog-booleanp)
996
6341f357
DN
997(defcustom verilog-auto-reset-widths t
998 "*If true, AUTORESET should determine the width of signals.
999This is then used to set the width of the zero (32'h0 for example). This
1000is required by some lint tools that aren't smart enough to ignore widths of
1001the constant zero. This may result in ugly code when parameters determine
37ea4b9b 1002the MSB or LSB of a signal inside an AUTORESET."
6341f357
DN
1003 :type 'boolean
1004 :group 'verilog-mode-auto)
eaf7efe9 1005(put 'verilog-auto-reset-widths 'safe-local-variable 'verilog-booleanp)
6341f357
DN
1006
1007(defcustom verilog-assignment-delay ""
1008 "*Text used for delays in delayed assignments. Add a trailing space if set."
60618039
DN
1009 :group 'verilog-mode-auto
1010 :type 'string)
1011(put 'verilog-assignment-delay 'safe-local-variable 'stringp)
6341f357 1012
a3a8b002 1013(defcustom verilog-auto-arg-sort nil
4c36be58 1014 "*If set, AUTOARG signal names will be sorted, not in declaration order.
14862301
SM
1015Declaration order is advantageous with order based instantiations
1016and is the default for backward compatibility. Sorted order
1017reduces changes when declarations are moved around in a file, and
9489a450
MM
1018it's bad practice to rely on order based instantiations anyhow.
1019
1020See also `verilog-auto-inst-sort'."
a3a8b002
DN
1021 :group 'verilog-mode-auto
1022 :type 'boolean)
1023(put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
1024
a03c2342
WS
1025(defcustom verilog-auto-inst-dot-name nil
1026 "*If true, when creating ports with AUTOINST, use .name syntax.
1027This will use \".port\" instead of \".port(port)\" when possible.
1028This is only legal in SystemVerilog files, and will confuse older
1029simulators. Setting `verilog-auto-inst-vector' to nil may also
1030be desirable to increase how often .name will be used."
1031 :group 'verilog-mode-auto
1032 :type 'boolean)
1033(put 'verilog-auto-inst-dot-name 'safe-local-variable 'verilog-booleanp)
1034
4c5e69c6
DN
1035(defcustom verilog-auto-inst-param-value nil
1036 "*If set, AUTOINST will replace parameters with the parameter value.
1037If nil, leave parameters as symbolic names.
1038
1039Parameters must be in Verilog 2001 format #(...), and if a parameter is not
1040listed as such there (as when the default value is acceptable), it will not
1041be replaced, and will remain symbolic.
1042
1043For example, imagine a submodule uses parameters to declare the size of its
1044inputs. This is then used by a upper module:
1045
a03c2342 1046 module InstModule (o,i);
4c5e69c6
DN
1047 parameter WIDTH;
1048 input [WIDTH-1:0] i;
1049 endmodule
1050
1051 module ExampInst;
1052 InstModule
1053 #(PARAM(10))
1054 instName
1055 (/*AUTOINST*/
1056 .i (i[PARAM-1:0]));
1057
1058Note even though PARAM=10, the AUTOINST has left the parameter as a
1059symbolic name. If `verilog-auto-inst-param-value' is set, this will
1060instead expand to:
1061
1062 module ExampInst;
1063 InstModule
1064 #(PARAM(10))
1065 instName
1066 (/*AUTOINST*/
1067 .i (i[9:0]));"
1068 :group 'verilog-mode-auto
1069 :type 'boolean)
7cb1c4d7 1070(put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
4c5e69c6 1071
9489a450
MM
1072(defcustom verilog-auto-inst-sort nil
1073 "*If set, AUTOINST signal names will be sorted, not in declaration order.
1074Also affects AUTOINSTPARAM. Declaration order is the default for
1075backward compatibility, and as some teams prefer signals that are
1076declared together to remain together. Sorted order reduces
1077changes when declarations are moved around in a file.
1078
1079See also `verilog-auto-arg-sort'."
2bed3f04 1080 :version "24.1"
9489a450
MM
1081 :group 'verilog-mode-auto
1082 :type 'boolean)
1083(put 'verilog-auto-inst-sort 'safe-local-variable 'verilog-booleanp)
1084
6341f357
DN
1085(defcustom verilog-auto-inst-vector t
1086 "*If true, when creating default ports with AUTOINST, use bus subscripts.
1087If nil, skip the subscript when it matches the entire bus as declared in
1088the module (AUTOWIRE signals always are subscripted, you must manually
37ea4b9b
JB
1089declare the wire to have the subscripts removed.) Setting this to nil may
1090speed up some simulators, but is less general and harder to read, so avoid."
6341f357 1091 :group 'verilog-mode-auto
60618039 1092 :type 'boolean)
eaf7efe9 1093(put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
6341f357
DN
1094
1095(defcustom verilog-auto-inst-template-numbers nil
1096 "*If true, when creating templated ports with AUTOINST, add a comment.
9489a450
MM
1097
1098If t, the comment will add the line number of the template that
1099was used for that port declaration. This setting is suggested
1100only for debugging use, as regular use may cause a large numbers
1101of merge conflicts.
1102
1103If 'lhs', the comment will show the left hand side of the
1104AUTO_TEMPLATE rule that is matched. This is less precise than
1105numbering (t) when multiple rules have the same pin name, but
1106won't merge conflict."
6341f357 1107 :group 'verilog-mode-auto
9489a450
MM
1108 :type '(choice (const nil) (const t) (const lhs)))
1109(put 'verilog-auto-inst-template-numbers 'safe-local-variable
1110 '(lambda (x) (memq x '(nil t lhs))))
6341f357 1111
4c5e69c6
DN
1112(defcustom verilog-auto-inst-column 40
1113 "*Indent-to column number for net name part of AUTOINST created pin."
1114 :group 'verilog-mode-indent
1115 :type 'integer)
1116(put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
6341f357
DN
1117
1118(defcustom verilog-auto-input-ignore-regexp nil
1119 "*If set, when creating AUTOINPUT list, ignore signals matching this regexp.
1120See the \\[verilog-faq] for examples on using this."
1121 :group 'verilog-mode-auto
60618039
DN
1122 :type 'string)
1123(put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
6341f357
DN
1124
1125(defcustom verilog-auto-inout-ignore-regexp nil
1126 "*If set, when creating AUTOINOUT list, ignore signals matching this regexp.
1127See the \\[verilog-faq] for examples on using this."
1128 :group 'verilog-mode-auto
60618039
DN
1129 :type 'string)
1130(put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
6341f357
DN
1131
1132(defcustom verilog-auto-output-ignore-regexp nil
1133 "*If set, when creating AUTOOUTPUT list, ignore signals matching this regexp.
1134See the \\[verilog-faq] for examples on using this."
1135 :group 'verilog-mode-auto
60618039
DN
1136 :type 'string)
1137(put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
6341f357 1138
9489a450
MM
1139(defcustom verilog-auto-tieoff-declaration "wire"
1140 "*Data type used for the declaration for AUTOTIEOFF. If \"wire\" then
1141create a wire, if \"assign\" create an assignment, else the data type for
1142variable creation."
2bed3f04 1143 :version "24.1"
9489a450
MM
1144 :group 'verilog-mode-auto
1145 :type 'string)
1146(put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
1147
a03c2342
WS
1148(defcustom verilog-auto-tieoff-ignore-regexp nil
1149 "*If set, when creating AUTOTIEOFF list, ignore signals matching this regexp.
1150See the \\[verilog-faq] for examples on using this."
1151 :group 'verilog-mode-auto
1152 :type 'string)
1153(put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
1154
6341f357
DN
1155(defcustom verilog-auto-unused-ignore-regexp nil
1156 "*If set, when creating AUTOUNUSED list, ignore signals matching this regexp.
1157See the \\[verilog-faq] for examples on using this."
1158 :group 'verilog-mode-auto
60618039
DN
1159 :type 'string)
1160(put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
6341f357
DN
1161
1162(defcustom verilog-typedef-regexp nil
1163 "*If non-nil, regular expression that matches Verilog-2001 typedef names.
1164For example, \"_t$\" matches typedefs named with _t, as in the C language."
1165 :group 'verilog-mode-auto
60618039
DN
1166 :type 'string)
1167(put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
6341f357
DN
1168
1169(defcustom verilog-mode-hook 'verilog-set-compile-command
37ea4b9b 1170 "*Hook run after Verilog mode is loaded."
6341f357
DN
1171 :type 'hook
1172 :group 'verilog-mode)
1173
1174(defcustom verilog-auto-hook nil
1175 "*Hook run after `verilog-mode' updates AUTOs."
60618039
DN
1176 :group 'verilog-mode-auto
1177 :type 'hook)
6341f357
DN
1178
1179(defcustom verilog-before-auto-hook nil
1180 "*Hook run before `verilog-mode' updates AUTOs."
60618039
DN
1181 :group 'verilog-mode-auto
1182 :type 'hook)
6341f357
DN
1183
1184(defcustom verilog-delete-auto-hook nil
1185 "*Hook run after `verilog-mode' deletes AUTOs."
60618039
DN
1186 :group 'verilog-mode-auto
1187 :type 'hook)
6341f357
DN
1188
1189(defcustom verilog-before-delete-auto-hook nil
1190 "*Hook run before `verilog-mode' deletes AUTOs."
60618039
DN
1191 :group 'verilog-mode-auto
1192 :type 'hook)
6341f357
DN
1193
1194(defcustom verilog-getopt-flags-hook nil
1195 "*Hook run after `verilog-getopt-flags' determines the Verilog option lists."
60618039
DN
1196 :group 'verilog-mode-auto
1197 :type 'hook)
6341f357
DN
1198
1199(defcustom verilog-before-getopt-flags-hook nil
1200 "*Hook run before `verilog-getopt-flags' determines the Verilog option lists."
60618039
DN
1201 :group 'verilog-mode-auto
1202 :type 'hook)
6341f357
DN
1203
1204(defvar verilog-imenu-generic-expression
1205 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
1206 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
37ea4b9b 1207 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
6341f357
DN
1208
1209;;
1210;; provide a verilog-header function.
1211;; Customization variables:
1212;;
1213(defvar verilog-date-scientific-format nil
1214 "*If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1215If nil, in European format (e.g. 17.09.1997). The brain-dead American
1216format (e.g. 09/17/1997) is not supported.")
1217
1218(defvar verilog-company nil
37ea4b9b 1219 "*Default name of Company for Verilog header.
6341f357 1220If set will become buffer local.")
7ea26faf
DN
1221(make-variable-buffer-local 'verilog-company)
1222
6341f357 1223(defvar verilog-project nil
37ea4b9b 1224 "*Default name of Project for Verilog header.
6341f357 1225If set will become buffer local.")
7ea26faf
DN
1226(make-variable-buffer-local 'verilog-project)
1227
7d49d8e1 1228(defvar verilog-mode-map
6edb5716
DN
1229 (let ((map (make-sparse-keymap)))
1230 (define-key map ";" 'electric-verilog-semi)
1231 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1232 (define-key map ":" 'electric-verilog-colon)
1233 ;;(define-key map "=" 'electric-verilog-equal)
1234 (define-key map "\`" 'electric-verilog-tick)
1235 (define-key map "\t" 'electric-verilog-tab)
1236 (define-key map "\r" 'electric-verilog-terminate-line)
1237 ;; backspace/delete key bindings
1238 (define-key map [backspace] 'backward-delete-char-untabify)
1239 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1240 (define-key map [delete] 'delete-char)
1241 (define-key map [(meta delete)] 'kill-word))
1242 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1243 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1244 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1245 (define-key map "\M-\t" 'verilog-complete-word)
1246 (define-key map "\M-?" 'verilog-show-completions)
9489a450 1247 ;; Note \C-c and letter are reserved for users
6edb5716
DN
1248 (define-key map "\C-c\`" 'verilog-lint-off)
1249 (define-key map "\C-c\*" 'verilog-delete-auto-star-implicit)
9489a450 1250 (define-key map "\C-c\?" 'verilog-diff-auto)
6edb5716
DN
1251 (define-key map "\C-c\C-r" 'verilog-label-be)
1252 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1253 (define-key map "\C-c=" 'verilog-pretty-expr)
1254 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1255 (define-key map "\M-*" 'verilog-star-comment)
1256 (define-key map "\C-c\C-c" 'verilog-comment-region)
1257 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
e1776067
DN
1258 (when (featurep 'xemacs)
1259 (define-key map [(meta control h)] 'verilog-mark-defun)
1260 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1261 (define-key map "\M-\C-e" 'verilog-end-of-defun))
6edb5716
DN
1262 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1263 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1264 (define-key map "\C-c\C-a" 'verilog-auto)
1265 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
a03c2342 1266 (define-key map "\C-c\C-p" 'verilog-preprocess)
6edb5716
DN
1267 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1268 (define-key map "\C-c\C-e" 'verilog-expand-vector)
30d48f20
DN
1269 (define-key map "\C-c\C-h" 'verilog-header)
1270 map)
6341f357 1271 "Keymap used in Verilog mode.")
6341f357
DN
1272
1273;; menus
7d55bf04
DN
1274(easy-menu-define
1275 verilog-menu verilog-mode-map "Menu for Verilog mode"
495ab0d5
DN
1276 (verilog-easy-menu-filter
1277 '("Verilog"
1278 ("Choose Compilation Action"
1279 ["None"
1280 (progn
1281 (setq verilog-tool nil)
1282 (verilog-set-compile-command))
1283 :style radio
1284 :selected (equal verilog-tool nil)
1285 :help "When invoking compilation, use compile-command"]
1286 ["Lint"
1287 (progn
1288 (setq verilog-tool 'verilog-linter)
1289 (verilog-set-compile-command))
1290 :style radio
1291 :selected (equal verilog-tool `verilog-linter)
1292 :help "When invoking compilation, use lint checker"]
1293 ["Coverage"
1294 (progn
1295 (setq verilog-tool 'verilog-coverage)
1296 (verilog-set-compile-command))
1297 :style radio
1298 :selected (equal verilog-tool `verilog-coverage)
1299 :help "When invoking compilation, annotate for coverage"]
1300 ["Simulator"
1301 (progn
1302 (setq verilog-tool 'verilog-simulator)
1303 (verilog-set-compile-command))
1304 :style radio
1305 :selected (equal verilog-tool `verilog-simulator)
1306 :help "When invoking compilation, interpret Verilog source"]
1307 ["Compiler"
1308 (progn
1309 (setq verilog-tool 'verilog-compiler)
1310 (verilog-set-compile-command))
1311 :style radio
1312 :selected (equal verilog-tool `verilog-compiler)
1313 :help "When invoking compilation, compile Verilog source"]
a03c2342
WS
1314 ["Preprocessor"
1315 (progn
1316 (setq verilog-tool 'verilog-preprocessor)
1317 (verilog-set-compile-command))
1318 :style radio
1319 :selected (equal verilog-tool `verilog-preprocessor)
1320 :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
495ab0d5
DN
1321 )
1322 ("Move"
1323 ["Beginning of function" verilog-beg-of-defun
1324 :keys "C-M-a"
1325 :help "Move backward to the beginning of the current function or procedure"]
1326 ["End of function" verilog-end-of-defun
1327 :keys "C-M-e"
1328 :help "Move forward to the end of the current function or procedure"]
1329 ["Mark function" verilog-mark-defun
1330 :keys "C-M-h"
1331 :help "Mark the current Verilog function or procedure"]
1332 ["Goto function/module" verilog-goto-defun
1333 :help "Move to specified Verilog module/task/function"]
1334 ["Move to beginning of block" electric-verilog-backward-sexp
1335 :help "Move backward over one balanced expression"]
1336 ["Move to end of block" electric-verilog-forward-sexp
1337 :help "Move forward over one balanced expression"]
1338 )
1339 ("Comments"
1340 ["Comment Region" verilog-comment-region
1341 :help "Put marked area into a comment"]
1342 ["UnComment Region" verilog-uncomment-region
1343 :help "Uncomment an area commented with Comment Region"]
1344 ["Multi-line comment insert" verilog-star-comment
1345 :help "Insert Verilog /* */ comment at point"]
1346 ["Lint error to comment" verilog-lint-off
1347 :help "Convert a Verilog linter warning line into a disable statement"]
1348 )
1349 "----"
1350 ["Compile" compile
1351 :help "Perform compilation-action (above) on the current buffer"]
1352 ["AUTO, Save, Compile" verilog-auto-save-compile
1353 :help "Recompute AUTOs, save buffer, and compile"]
1354 ["Next Compile Error" next-error
1355 :help "Visit next compilation error message and corresponding source code"]
1356 ["Ignore Lint Warning at point" verilog-lint-off
1357 :help "Convert a Verilog linter warning line into a disable statement"]
1358 "----"
1359 ["Line up declarations around point" verilog-pretty-declarations
1360 :help "Line up declarations around point"]
1361 ["Line up equations around point" verilog-pretty-expr
1362 :help "Line up expressions around point"]
1363 ["Redo/insert comments on every end" verilog-label-be
1364 :help "Label matching begin ... end statements"]
1365 ["Expand [x:y] vector line" verilog-expand-vector
1366 :help "Take a signal vector on the current line and expand it to multiple lines"]
1367 ["Insert begin-end block" verilog-insert-block
1368 :help "Insert begin ... end"]
1369 ["Complete word" verilog-complete-word
1370 :help "Complete word at point"]
1371 "----"
1372 ["Recompute AUTOs" verilog-auto
1373 :help "Expand AUTO meta-comment statements"]
1374 ["Kill AUTOs" verilog-delete-auto
1375 :help "Remove AUTO expansions"]
9489a450
MM
1376 ["Diff AUTOs" verilog-diff-auto
1377 :help "Show differences in AUTO expansions"]
495ab0d5
DN
1378 ["Inject AUTOs" verilog-inject-auto
1379 :help "Inject AUTOs into legacy non-AUTO buffer"]
1380 ("AUTO Help..."
1381 ["AUTO General" (describe-function 'verilog-auto)
1382 :help "Help introduction on AUTOs"]
1383 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1384 :help "Help on verilog-library-flags"]
1385 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1386 :help "Help on verilog-library-directories"]
1387 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1388 :help "Help on verilog-library-files"]
1389 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1390 :help "Help on verilog-library-extensions"]
1391 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1392 :help "Help on reading `defines"]
1393 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1394 :help "Help on parsing `includes"]
1395 ["AUTOARG" (describe-function 'verilog-auto-arg)
1396 :help "Help on AUTOARG - declaring module port list"]
1397 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1398 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
86a4c7ac 1399 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
7cb1c4d7 1400 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
9489a450
MM
1401 ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
1402 :help "Help on AUTOINOUTCOMP - copying i/o from another file as all inputs"]
495ab0d5
DN
1403 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1404 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
a3a8b002
DN
1405 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1406 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
495ab0d5
DN
1407 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1408 :help "Help on AUTOINOUT - adding inouts from cells"]
1409 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1410 :help "Help on AUTOINPUT - adding inputs from cells"]
1411 ["AUTOINST" (describe-function 'verilog-auto-inst)
1412 :help "Help on AUTOINST - adding pins for cells"]
1413 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1414 :help "Help on expanding Verilog-2001 .* pins"]
1415 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1416 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
9489a450
MM
1417 ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
1418 :help "Help on AUTOLOGIC - declaring logic signals"]
495ab0d5
DN
1419 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1420 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1421 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1422 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1423 ["AUTOREG" (describe-function 'verilog-auto-reg)
1424 :help "Help on AUTOREG - declaring registers for non-wires"]
1425 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1426 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1427 ["AUTORESET" (describe-function 'verilog-auto-reset)
1428 :help "Help on AUTORESET - resetting always blocks"]
1429 ["AUTOSENSE" (describe-function 'verilog-auto-sense)
1430 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1431 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
22bcf204 1432 :help "Help on AUTOTIEOFF - tying off unused outputs"]
495ab0d5
DN
1433 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1434 :help "Help on AUTOUNUSED - terminating unused inputs"]
1435 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1436 :help "Help on AUTOWIRE - declaring wires for cells"]
1437 )
1438 "----"
1439 ["Submit bug report" verilog-submit-bug-report
1440 :help "Submit via mail a bug report on verilog-mode.el"]
1441 ["Version and FAQ" verilog-faq
1442 :help "Show the current version, and where to get the FAQ etc"]
1443 ["Customize Verilog Mode..." verilog-customize
1444 :help "Customize variables and other settings used by Verilog-Mode"]
1445 ["Customize Verilog Fonts & Colors" verilog-font-customize
1446 :help "Customize fonts used by Verilog-Mode."])))
7d55bf04
DN
1447
1448(easy-menu-define
1449 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
495ab0d5
DN
1450 (verilog-easy-menu-filter
1451 '("Statements"
1452 ["Header" verilog-sk-header
1453 :help "Insert a header block at the top of file"]
1454 ["Comment" verilog-sk-comment
1455 :help "Insert a comment block"]
1456 "----"
1457 ["Module" verilog-sk-module
1458 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
9489a450
MM
1459 ["OVM Class" verilog-sk-ovm-class
1460 :help "Insert an OVM class block"]
1461 ["UVM Class" verilog-sk-uvm-class
1462 :help "Insert an UVM class block"]
495ab0d5
DN
1463 ["Primitive" verilog-sk-primitive
1464 :help "Insert a primitive .. (.. );.. endprimitive block"]
1465 "----"
1466 ["Input" verilog-sk-input
1467 :help "Insert an input declaration"]
1468 ["Output" verilog-sk-output
1469 :help "Insert an output declaration"]
1470 ["Inout" verilog-sk-inout
1471 :help "Insert an inout declaration"]
1472 ["Wire" verilog-sk-wire
1473 :help "Insert a wire declaration"]
1474 ["Reg" verilog-sk-reg
1475 :help "Insert a register declaration"]
1476 ["Define thing under point as a register" verilog-sk-define-signal
1477 :help "Define signal under point as a register at the top of the module"]
1478 "----"
1479 ["Initial" verilog-sk-initial
1480 :help "Insert an initial begin .. end block"]
1481 ["Always" verilog-sk-always
1482 :help "Insert an always @(AS) begin .. end block"]
1483 ["Function" verilog-sk-function
1484 :help "Insert a function .. begin .. end endfunction block"]
1485 ["Task" verilog-sk-task
1486 :help "Insert a task .. begin .. end endtask block"]
1487 ["Specify" verilog-sk-specify
1488 :help "Insert a specify .. endspecify block"]
1489 ["Generate" verilog-sk-generate
1490 :help "Insert a generate .. endgenerate block"]
1491 "----"
1492 ["Begin" verilog-sk-begin
1493 :help "Insert a begin .. end block"]
1494 ["If" verilog-sk-if
1495 :help "Insert an if (..) begin .. end block"]
1496 ["(if) else" verilog-sk-else-if
1497 :help "Insert an else if (..) begin .. end block"]
1498 ["For" verilog-sk-for
1499 :help "Insert a for (...) begin .. end block"]
1500 ["While" verilog-sk-while
1501 :help "Insert a while (...) begin .. end block"]
1502 ["Fork" verilog-sk-fork
1503 :help "Insert a fork begin .. end .. join block"]
1504 ["Repeat" verilog-sk-repeat
1505 :help "Insert a repeat (..) begin .. end block"]
1506 ["Case" verilog-sk-case
1507 :help "Insert a case block, prompting for details"]
1508 ["Casex" verilog-sk-casex
1509 :help "Insert a casex (...) item: begin.. end endcase block"]
1510 ["Casez" verilog-sk-casez
1511 :help "Insert a casez (...) item: begin.. end endcase block"])))
6341f357
DN
1512
1513(defvar verilog-mode-abbrev-table nil
1514 "Abbrev table in use in Verilog-mode buffers.")
1515
1516(define-abbrev-table 'verilog-mode-abbrev-table ())
9489a450
MM
1517(verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
1518(verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
1519(verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
1520(verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
1521(verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
1522(verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
1523(verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
1524(verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
1525(verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
1526(verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
1527(verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
1528(verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
1529(verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
1530(verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
1531(verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
1532(verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
1533(verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
1534(verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
1535(verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
1536(verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
1537(verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
1538(verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
1539(verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
1540(verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
1541(verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
6341f357 1542
b68a96b9
DN
1543;;
1544;; Macros
1545;;
1546
a03c2342 1547(defsubst verilog-within-string ()
3ba6b2ee 1548 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
a03c2342 1549
b68a96b9
DN
1550(defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1551 "Replace occurrences of FROM-STRING with TO-STRING.
1552FIXEDCASE and LITERAL as in `replace-match`. STRING is what to replace.
1553The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1554will break, as the o's continuously replace. xa -> x works ok though."
1555 ;; Hopefully soon to a emacs built-in
9489a450
MM
1556 ;; Also note \ in the replacement prevent multiple replacements; IE
1557 ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
1558 ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
b68a96b9
DN
1559 (let ((start 0))
1560 (while (string-match from-string string start)
1561 (setq string (replace-match to-string fixedcase literal string)
4c5e69c6 1562 start (min (length string) (+ (match-beginning 0) (length to-string)))))
b68a96b9
DN
1563 string))
1564
1565(defsubst verilog-string-remove-spaces (string)
1566 "Remove spaces surrounding STRING."
1567 (save-match-data
1568 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1569 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1570 string))
1571
1572(defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1573 ; checkdoc-params: (REGEXP BOUND NOERROR)
1574 "Like `re-search-forward', but skips over match in comments or strings."
7cb1c4d7
DN
1575 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1576 (while (and
1577 (re-search-forward REGEXP BOUND NOERROR)
1578 (setq mdata (match-data))
1579 (and (verilog-skip-forward-comment-or-string)
1580 (progn
1581 (setq mdata '(nil nil))
1582 (if BOUND
1583 (< (point) BOUND)
1584 t)))))
1585 (store-match-data mdata)
1586 (match-end 0)))
b68a96b9
DN
1587
1588(defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1589 ; checkdoc-params: (REGEXP BOUND NOERROR)
1590 "Like `re-search-backward', but skips over match in comments or strings."
7cb1c4d7
DN
1591 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1592 (while (and
1593 (re-search-backward REGEXP BOUND NOERROR)
1594 (setq mdata (match-data))
1595 (and (verilog-skip-backward-comment-or-string)
1596 (progn
1597 (setq mdata '(nil nil))
1598 (if BOUND
1599 (> (point) BOUND)
1600 t)))))
1601 (store-match-data mdata)
1602 (match-end 0)))
b68a96b9
DN
1603
1604(defsubst verilog-re-search-forward-quick (regexp bound noerror)
1605 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1606but trashes match data and is faster for REGEXP that doesn't match often.
9489a450 1607This uses `verilog-scan' and text properties to ignore comments,
b68a96b9
DN
1608so there may be a large up front penalty for the first search."
1609 (let (pt)
1610 (while (and (not pt)
1611 (re-search-forward regexp bound noerror))
9489a450
MM
1612 (if (verilog-inside-comment-or-string-p)
1613 (re-search-forward "[/\"\n]" nil t) ;; Only way a comment or quote can end
1614 (setq pt (match-end 0))))
b68a96b9
DN
1615 pt))
1616
1617(defsubst verilog-re-search-backward-quick (regexp bound noerror)
1618 ; checkdoc-params: (REGEXP BOUND NOERROR)
1619 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1620but trashes match data and is faster for REGEXP that doesn't match often.
9489a450 1621This uses `verilog-scan' and text properties to ignore comments,
b68a96b9
DN
1622so there may be a large up front penalty for the first search."
1623 (let (pt)
1624 (while (and (not pt)
1625 (re-search-backward regexp bound noerror))
9489a450
MM
1626 (if (verilog-inside-comment-or-string-p)
1627 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
1628 (setq pt (match-beginning 0))))
b68a96b9
DN
1629 pt))
1630
a03c2342
WS
1631(defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
1632 "Like `re-search-forward', but first search for SUBSTR constant.
1633Then searched for the normal REGEXP (which contains SUBSTR), with given
1634BOUND and NOERROR. The REGEXP must fit within a single line.
1635This speeds up complicated regexp matches."
1636 ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
1637 ;; thus require matches to be on one line, and use beginning-of-line.
1638 (let (done)
1639 (while (and (not done)
1640 (search-forward substr bound noerror))
1641 (save-excursion
1642 (beginning-of-line)
3ba6b2ee 1643 (setq done (re-search-forward regexp (point-at-eol) noerror)))
a03c2342
WS
1644 (unless (and (<= (match-beginning 0) (point))
1645 (>= (match-end 0) (point)))
1646 (setq done nil)))
1647 (when done (goto-char done))
1648 done))
1649;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1650
1651(defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
1652 "Like `re-search-backward', but first search for SUBSTR constant.
1653Then searched for the normal REGEXP (which contains SUBSTR), with given
1654BOUND and NOERROR. The REGEXP must fit within a single line.
1655This speeds up complicated regexp matches."
1656 ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
1657 ;; thus require matches to be on one line, and use beginning-of-line.
1658 (let (done)
1659 (while (and (not done)
1660 (search-backward substr bound noerror))
1661 (save-excursion
1662 (end-of-line)
3ba6b2ee 1663 (setq done (re-search-backward regexp (point-at-bol) noerror)))
a03c2342
WS
1664 (unless (and (<= (match-beginning 0) (point))
1665 (>= (match-end 0) (point)))
1666 (setq done nil)))
1667 (when done (goto-char done))
1668 done))
1669;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
b68a96b9 1670
9489a450
MM
1671(defun verilog-delete-trailing-whitespace ()
1672 "Delete trailing spaces or tabs, but not newlines nor linefeeds."
1673 ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
1674 (save-excursion
1675 (goto-char (point-min))
22bcf204 1676 (while (re-search-forward "[ \t]+$" nil t) ;; Not syntactic WS as no formfeed
9489a450
MM
1677 (replace-match "" nil nil))))
1678
60618039
DN
1679(defvar compile-command)
1680
6341f357
DN
1681;; compilation program
1682(defun verilog-set-compile-command ()
37ea4b9b 1683 "Function to compute shell command to compile Verilog.
6341f357
DN
1684
1685This reads `verilog-tool' and sets `compile-command'. This specifies the
1686program that executes when you type \\[compile] or
1687\\[verilog-auto-save-compile].
1688
a03c2342
WS
1689By default `verilog-tool' uses a Makefile if one exists in the
1690current directory. If not, it is set to the `verilog-linter',
1691`verilog-compiler', `verilog-coverage', `verilog-preprocessor',
1692or `verilog-simulator' variables, as selected with the Verilog ->
1693\"Choose Compilation Action\" menu.
6341f357
DN
1694
1695You should set `verilog-tool' or the other variables to the path and
1696arguments for your Verilog simulator. For example:
1697 \"vcs -p123 -O\"
1698or a string like:
1699 \"(cd /tmp; surecov %s)\".
1700
1701In the former case, the path to the current buffer is concat'ed to the
1702value of `verilog-tool'; in the later, the path to the current buffer is
1703substituted for the %s.
1704
a03c2342
WS
1705Where __FLAGS__ appears in the string `verilog-current-flags'
1706will be substituted.
1707
1708Where __FILE__ appears in the string, the variable
1709`buffer-file-name' of the current buffer, without the directory
1710portion, will be substituted."
6341f357
DN
1711 (interactive)
1712 (cond
1713 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1714 (file-exists-p "Makefile"))
175069ef 1715 (set (make-local-variable 'compile-command) "make "))
6341f357 1716 (t
175069ef 1717 (set (make-local-variable 'compile-command)
9489a450
MM
1718 (if verilog-tool
1719 (if (string-match "%s" (eval verilog-tool))
1720 (format (eval verilog-tool) (or buffer-file-name ""))
1721 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1722 ""))))
6341f357
DN
1723 (verilog-modify-compile-command))
1724
a03c2342
WS
1725(defun verilog-expand-command (command)
1726 "Replace meta-information in COMMAND and return it.
1727Where __FLAGS__ appears in the string `verilog-current-flags'
1728will be substituted. Where __FILE__ appears in the string, the
1729current buffer's file-name, without the directory portion, will
1730be substituted."
1731 (setq command (verilog-string-replace-matches
1732 ;; Note \\b only works if under verilog syntax table
1733 "\\b__FLAGS__\\b" (verilog-current-flags)
1734 t t command))
1735 (setq command (verilog-string-replace-matches
1736 "\\b__FILE__\\b" (file-name-nondirectory
1737 (or (buffer-file-name) ""))
1738 t t command))
1739 command)
1740
6341f357 1741(defun verilog-modify-compile-command ()
a03c2342 1742 "Update `compile-command' using `verilog-expand-command'."
6341f357
DN
1743 (when (and
1744 (stringp compile-command)
a03c2342 1745 (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
175069ef 1746 (set (make-local-variable 'compile-command)
9489a450 1747 (verilog-expand-command compile-command))))
6341f357 1748
d9e8a018
GM
1749(if (featurep 'xemacs)
1750 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1751 (defun verilog-error-regexp-add-xemacs ()
1752 "Teach XEmacs about verilog errors.
7ea26faf
DN
1753Called by `compilation-mode-hook'. This allows \\[next-error] to
1754find the errors."
d9e8a018
GM
1755 (interactive)
1756 (if (boundp 'compilation-error-regexp-systems-alist)
1757 (if (and
1758 (not (equal compilation-error-regexp-systems-list 'all))
1759 (not (member compilation-error-regexp-systems-list 'verilog)))
1760 (push 'verilog compilation-error-regexp-systems-list)))
1761 (if (boundp 'compilation-error-regexp-alist-alist)
1762 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1763 (setcdr compilation-error-regexp-alist-alist
1764 (cons verilog-error-regexp-xemacs-alist
1765 (cdr compilation-error-regexp-alist-alist)))))
1766 (if (boundp 'compilation-font-lock-keywords)
1767 (progn
9489a450
MM
1768 (set (make-local-variable 'compilation-font-lock-keywords)
1769 verilog-error-font-lock-keywords)
d9e8a018
GM
1770 (font-lock-set-defaults)))
1771 ;; Need to re-run compilation-error-regexp builder
1772 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1773 (compilation-build-compilation-error-regexp-alist))
1774 ))
d88782c3
DN
1775
1776;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
a3a8b002
DN
1777(defun verilog-error-regexp-add-emacs ()
1778 "Tell Emacs compile that we are Verilog.
1779Called by `compilation-mode-hook'. This allows \\[next-error] to
1780find the errors."
1781 (interactive)
d88782c3
DN
1782 (if (boundp 'compilation-error-regexp-alist-alist)
1783 (progn
1784 (if (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1785 (mapcar
1786 (lambda (item)
1787 (push (car item) compilation-error-regexp-alist)
1788 (push item compilation-error-regexp-alist-alist)
1789 )
1790 verilog-error-regexp-emacs-alist)))))
1791
1792(if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1793(if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
6341f357
DN
1794
1795(defconst verilog-directive-re
a03c2342
WS
1796 (eval-when-compile
1797 (verilog-regexp-words
1798 '(
1799 "`case" "`default" "`define" "`else" "`elsif" "`endfor" "`endif"
1800 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1801 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1802 "`time_scale" "`undef" "`while" ))))
6341f357 1803
a3a8b002
DN
1804(defconst verilog-directive-re-1
1805 (concat "[ \t]*" verilog-directive-re))
1806
6341f357
DN
1807(defconst verilog-directive-begin
1808 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1809
1810(defconst verilog-directive-middle
a03c2342 1811 "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
6341f357
DN
1812
1813(defconst verilog-directive-end
1814 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1815
a3a8b002
DN
1816(defconst verilog-ovm-begin-re
1817 (eval-when-compile
1818 (verilog-regexp-opt
1819 '(
1820 "`ovm_component_utils_begin"
14862301 1821 "`ovm_component_param_utils_begin"
a3a8b002
DN
1822 "`ovm_field_utils_begin"
1823 "`ovm_object_utils_begin"
14862301 1824 "`ovm_object_param_utils_begin"
a3a8b002
DN
1825 "`ovm_sequence_utils_begin"
1826 "`ovm_sequencer_utils_begin"
1827 ) nil )))
1828
1829(defconst verilog-ovm-end-re
1830 (eval-when-compile
1831 (verilog-regexp-opt
1832 '(
1833 "`ovm_component_utils_end"
1834 "`ovm_field_utils_end"
1835 "`ovm_object_utils_end"
1836 "`ovm_sequence_utils_end"
1837 "`ovm_sequencer_utils_end"
1838 ) nil )))
1839
9489a450
MM
1840(defconst verilog-uvm-begin-re
1841 (eval-when-compile
1842 (verilog-regexp-opt
1843 '(
1844 "`uvm_component_utils_begin"
1845 "`uvm_component_param_utils_begin"
1846 "`uvm_field_utils_begin"
1847 "`uvm_object_utils_begin"
1848 "`uvm_object_param_utils_begin"
1849 "`uvm_sequence_utils_begin"
1850 "`uvm_sequencer_utils_begin"
1851 ) nil )))
1852
1853(defconst verilog-uvm-end-re
1854 (eval-when-compile
1855 (verilog-regexp-opt
1856 '(
1857 "`uvm_component_utils_end"
1858 "`uvm_field_utils_end"
1859 "`uvm_object_utils_end"
1860 "`uvm_sequence_utils_end"
1861 "`uvm_sequencer_utils_end"
1862 ) nil )))
1863
86a4c7ac
DN
1864(defconst verilog-vmm-begin-re
1865 (eval-when-compile
1866 (verilog-regexp-opt
1867 '(
1868 "`vmm_data_member_begin"
1869 "`vmm_env_member_begin"
1870 "`vmm_scenario_member_begin"
1871 "`vmm_subenv_member_begin"
1872 "`vmm_xactor_member_begin"
1873 ) nil ) ) )
1874
1875(defconst verilog-vmm-end-re
1876 (eval-when-compile
1877 (verilog-regexp-opt
1878 '(
1879 "`vmm_data_member_end"
1880 "`vmm_env_member_end"
1881 "`vmm_scenario_member_end"
1882 "`vmm_subenv_member_end"
1883 "`vmm_xactor_member_end"
1884 ) nil ) ) )
1885
1886(defconst verilog-vmm-statement-re
1887 (eval-when-compile
1888 (verilog-regexp-opt
1889 '(
1890;; "`vmm_xactor_member_enum_array"
1891 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
1892;; "`vmm_xactor_member_scalar_array"
1893;; "`vmm_xactor_member_scalar"
1894 ) nil )))
1895
a3a8b002
DN
1896(defconst verilog-ovm-statement-re
1897 (eval-when-compile
1898 (verilog-regexp-opt
1899 '(
1900 ;; Statements
1901 "`DUT_ERROR"
1902 "`MESSAGE"
1903 "`dut_error"
1904 "`message"
1905 "`ovm_analysis_imp_decl"
1906 "`ovm_blocking_get_imp_decl"
1907 "`ovm_blocking_get_peek_imp_decl"
1908 "`ovm_blocking_master_imp_decl"
1909 "`ovm_blocking_peek_imp_decl"
1910 "`ovm_blocking_put_imp_decl"
1911 "`ovm_blocking_slave_imp_decl"
1912 "`ovm_blocking_transport_imp_decl"
1913 "`ovm_component_registry"
1914 "`ovm_component_registry_param"
1915 "`ovm_component_utils"
1916 "`ovm_create"
1917 "`ovm_create_seq"
1918 "`ovm_declare_sequence_lib"
1919 "`ovm_do"
1920 "`ovm_do_seq"
1921 "`ovm_do_seq_with"
1922 "`ovm_do_with"
1923 "`ovm_error"
1924 "`ovm_fatal"
1925 "`ovm_field_aa_int_byte"
1926 "`ovm_field_aa_int_byte_unsigned"
1927 "`ovm_field_aa_int_int"
1928 "`ovm_field_aa_int_int_unsigned"
1929 "`ovm_field_aa_int_integer"
1930 "`ovm_field_aa_int_integer_unsigned"
1931 "`ovm_field_aa_int_key"
1932 "`ovm_field_aa_int_longint"
1933 "`ovm_field_aa_int_longint_unsigned"
1934 "`ovm_field_aa_int_shortint"
1935 "`ovm_field_aa_int_shortint_unsigned"
1936 "`ovm_field_aa_int_string"
1937 "`ovm_field_aa_object_int"
1938 "`ovm_field_aa_object_string"
1939 "`ovm_field_aa_string_int"
1940 "`ovm_field_aa_string_string"
1941 "`ovm_field_array_int"
1942 "`ovm_field_array_object"
1943 "`ovm_field_array_string"
1944 "`ovm_field_enum"
1945 "`ovm_field_event"
1946 "`ovm_field_int"
1947 "`ovm_field_object"
1948 "`ovm_field_queue_int"
1949 "`ovm_field_queue_object"
1950 "`ovm_field_queue_string"
1951 "`ovm_field_sarray_int"
1952 "`ovm_field_string"
1953 "`ovm_field_utils"
1954 "`ovm_file"
1955 "`ovm_get_imp_decl"
1956 "`ovm_get_peek_imp_decl"
1957 "`ovm_info"
1958 "`ovm_info1"
1959 "`ovm_info2"
1960 "`ovm_info3"
1961 "`ovm_info4"
1962 "`ovm_line"
1963 "`ovm_master_imp_decl"
1964 "`ovm_msg_detail"
1965 "`ovm_non_blocking_transport_imp_decl"
1966 "`ovm_nonblocking_get_imp_decl"
1967 "`ovm_nonblocking_get_peek_imp_decl"
1968 "`ovm_nonblocking_master_imp_decl"
1969 "`ovm_nonblocking_peek_imp_decl"
1970 "`ovm_nonblocking_put_imp_decl"
1971 "`ovm_nonblocking_slave_imp_decl"
1972 "`ovm_object_registry"
1973 "`ovm_object_registry_param"
1974 "`ovm_object_utils"
1975 "`ovm_peek_imp_decl"
1976 "`ovm_phase_func_decl"
1977 "`ovm_phase_task_decl"
1978 "`ovm_print_aa_int_object"
1979 "`ovm_print_aa_string_int"
1980 "`ovm_print_aa_string_object"
1981 "`ovm_print_aa_string_string"
1982 "`ovm_print_array_int"
1983 "`ovm_print_array_object"
1984 "`ovm_print_array_string"
1985 "`ovm_print_object_queue"
1986 "`ovm_print_queue_int"
1987 "`ovm_print_string_queue"
1988 "`ovm_put_imp_decl"
1989 "`ovm_rand_send"
1990 "`ovm_rand_send_with"
1991 "`ovm_send"
1992 "`ovm_sequence_utils"
1993 "`ovm_slave_imp_decl"
1994 "`ovm_transport_imp_decl"
1995 "`ovm_update_sequence_lib"
1996 "`ovm_update_sequence_lib_and_item"
1997 "`ovm_warning"
1998 "`static_dut_error"
1999 "`static_message") nil )))
2000
9489a450
MM
2001(defconst verilog-uvm-statement-re
2002 (eval-when-compile
2003 (verilog-regexp-opt
2004 '(
2005 ;; Statements
2006 "`uvm_analysis_imp_decl"
2007 "`uvm_blocking_get_imp_decl"
2008 "`uvm_blocking_get_peek_imp_decl"
2009 "`uvm_blocking_master_imp_decl"
2010 "`uvm_blocking_peek_imp_decl"
2011 "`uvm_blocking_put_imp_decl"
2012 "`uvm_blocking_slave_imp_decl"
2013 "`uvm_blocking_transport_imp_decl"
2014 "`uvm_component_param_utils"
2015 "`uvm_component_registry"
2016 "`uvm_component_registry_param"
2017 "`uvm_component_utils"
2018 "`uvm_create"
2019 "`uvm_create_on"
301b181a 2020 "`uvm_create_seq" ;; Undocumented in 1.1
9489a450
MM
2021 "`uvm_declare_p_sequencer"
2022 "`uvm_declare_sequence_lib" ;; Deprecated in 1.1
2023 "`uvm_do"
2024 "`uvm_do_callbacks"
2025 "`uvm_do_callbacks_exit_on"
2026 "`uvm_do_obj_callbacks"
2027 "`uvm_do_obj_callbacks_exit_on"
2028 "`uvm_do_on"
2029 "`uvm_do_on_pri"
2030 "`uvm_do_on_pri_with"
2031 "`uvm_do_on_with"
2032 "`uvm_do_pri"
2033 "`uvm_do_pri_with"
2034 "`uvm_do_seq" ;; Undocumented in 1.1
2035 "`uvm_do_seq_with" ;; Undocumented in 1.1
2036 "`uvm_do_with"
2037 "`uvm_error"
2038 "`uvm_error_context"
2039 "`uvm_fatal"
2040 "`uvm_fatal_context"
2041 "`uvm_field_aa_int_byte"
2042 "`uvm_field_aa_int_byte_unsigned"
2043 "`uvm_field_aa_int_enum"
2044 "`uvm_field_aa_int_int"
2045 "`uvm_field_aa_int_int_unsigned"
2046 "`uvm_field_aa_int_integer"
2047 "`uvm_field_aa_int_integer_unsigned"
2048 "`uvm_field_aa_int_key"
2049 "`uvm_field_aa_int_longint"
2050 "`uvm_field_aa_int_longint_unsigned"
2051 "`uvm_field_aa_int_shortint"
2052 "`uvm_field_aa_int_shortint_unsigned"
2053 "`uvm_field_aa_int_string"
2054 "`uvm_field_aa_object_int"
2055 "`uvm_field_aa_object_string"
2056 "`uvm_field_aa_string_int"
2057 "`uvm_field_aa_string_string"
2058 "`uvm_field_array_enum"
2059 "`uvm_field_array_int"
2060 "`uvm_field_array_object"
2061 "`uvm_field_array_string"
2062 "`uvm_field_enum"
2063 "`uvm_field_event"
2064 "`uvm_field_int"
2065 "`uvm_field_object"
2066 "`uvm_field_queue_enum"
2067 "`uvm_field_queue_int"
2068 "`uvm_field_queue_object"
2069 "`uvm_field_queue_string"
2070 "`uvm_field_real"
2071 "`uvm_field_sarray_enum"
2072 "`uvm_field_sarray_int"
2073 "`uvm_field_sarray_object"
2074 "`uvm_field_sarray_string"
2075 "`uvm_field_string"
2076 "`uvm_field_utils"
2077 "`uvm_file" ;; Undocumented in 1.1, use `__FILE__
2078 "`uvm_get_imp_decl"
2079 "`uvm_get_peek_imp_decl"
2080 "`uvm_info"
2081 "`uvm_info_context"
2082 "`uvm_line" ;; Undocumented in 1.1, use `__LINE__
2083 "`uvm_master_imp_decl"
2084 "`uvm_non_blocking_transport_imp_decl" ;; Deprecated in 1.1
2085 "`uvm_nonblocking_get_imp_decl"
2086 "`uvm_nonblocking_get_peek_imp_decl"
2087 "`uvm_nonblocking_master_imp_decl"
2088 "`uvm_nonblocking_peek_imp_decl"
2089 "`uvm_nonblocking_put_imp_decl"
2090 "`uvm_nonblocking_slave_imp_decl"
2091 "`uvm_nonblocking_transport_imp_decl"
2092 "`uvm_object_param_utils"
2093 "`uvm_object_registry"
2094 "`uvm_object_registry_param" ;; Undocumented in 1.1
2095 "`uvm_object_utils"
2096 "`uvm_pack_array"
2097 "`uvm_pack_arrayN"
2098 "`uvm_pack_enum"
2099 "`uvm_pack_enumN"
2100 "`uvm_pack_int"
2101 "`uvm_pack_intN"
2102 "`uvm_pack_queue"
2103 "`uvm_pack_queueN"
2104 "`uvm_pack_real"
2105 "`uvm_pack_sarray"
2106 "`uvm_pack_sarrayN"
2107 "`uvm_pack_string"
2108 "`uvm_peek_imp_decl"
2109 "`uvm_put_imp_decl"
2110 "`uvm_rand_send"
2111 "`uvm_rand_send_pri"
2112 "`uvm_rand_send_pri_with"
2113 "`uvm_rand_send_with"
2114 "`uvm_record_attribute"
2115 "`uvm_record_field"
2116 "`uvm_register_cb"
2117 "`uvm_send"
2118 "`uvm_send_pri"
2119 "`uvm_sequence_utils" ;; Deprecated in 1.1
2120 "`uvm_set_super_type"
2121 "`uvm_slave_imp_decl"
2122 "`uvm_transport_imp_decl"
2123 "`uvm_unpack_array"
2124 "`uvm_unpack_arrayN"
2125 "`uvm_unpack_enum"
2126 "`uvm_unpack_enumN"
2127 "`uvm_unpack_int"
2128 "`uvm_unpack_intN"
2129 "`uvm_unpack_queue"
2130 "`uvm_unpack_queueN"
2131 "`uvm_unpack_real"
2132 "`uvm_unpack_sarray"
2133 "`uvm_unpack_sarrayN"
2134 "`uvm_unpack_string"
2135 "`uvm_update_sequence_lib" ;; Deprecated in 1.1
2136 "`uvm_update_sequence_lib_and_item" ;; Deprecated in 1.1
2137 "`uvm_warning"
2138 "`uvm_warning_context") nil )))
2139
6341f357
DN
2140
2141;;
2142;; Regular expressions used to calculate indent, etc.
2143;;
2144(defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
6341f357
DN
2145;; Want to match
2146;; aa :
2147;; aa,bb :
2148;; a[34:32] :
2149;; a,
2150;; b :
9489a450
MM
2151(defconst verilog-assignment-operator-re
2152 (eval-when-compile
2153 (verilog-regexp-opt
2154 `(
2155 ;; blocking assignment_operator
2156 "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
2157 ;; non blocking assignment operator
2158 "<="
2159 ;; comparison
2160 "==" "!=" "===" "!===" "<=" ">=" "==\?" "!=\?"
2161 ;; event_trigger
2162 "->" "->>"
2163 ;; property_expr
2164 "|->" "|=>"
2165 ;; Is this a legal verilog operator?
2166 ":="
2167 ) 't
2168 )))
2169(defconst verilog-assignment-operation-re
2170 (concat
2171; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
2172; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
2173 "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
6341f357 2174
14862301 2175(defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
a03c2342
WS
2176(defconst verilog-property-re
2177 (concat "\\(" verilog-label-re "\\)?"
2178 "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
2179 ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
2180
6341f357
DN
2181(defconst verilog-no-indent-begin-re
2182 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\|always_comb\\|always_ff\\|always_latch\\)\\>")
2183
2184(defconst verilog-ends-re
2185 ;; Parenthesis indicate type of keyword found
2186 (concat
2187 "\\(\\<else\\>\\)\\|" ; 1
2188 "\\(\\<if\\>\\)\\|" ; 2
86a4c7ac
DN
2189 "\\(\\<assert\\>\\)\\|" ; 3
2190 "\\(\\<end\\>\\)\\|" ; 3.1
6341f357
DN
2191 "\\(\\<endcase\\>\\)\\|" ; 4
2192 "\\(\\<endfunction\\>\\)\\|" ; 5
2193 "\\(\\<endtask\\>\\)\\|" ; 6
2194 "\\(\\<endspecify\\>\\)\\|" ; 7
2195 "\\(\\<endtable\\>\\)\\|" ; 8
2196 "\\(\\<endgenerate\\>\\)\\|" ; 9
2197 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
2198 "\\(\\<endclass\\>\\)\\|" ; 11
a3a8b002 2199 "\\(\\<endgroup\\>\\)\\|" ; 12
86a4c7ac
DN
2200 ;; VMM
2201 "\\(\\<`vmm_data_member_end\\>\\)\\|"
2202 "\\(\\<`vmm_env_member_end\\>\\)\\|"
2203 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
2204 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
2205 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
a3a8b002
DN
2206 ;; OVM
2207 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
2208 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
2209 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
2210 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
2211 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
9489a450
MM
2212 ;; UVM
2213 "\\(\\<`uvm_component_utils_end\\>\\)\\|"
2214 "\\(\\<`uvm_field_utils_end\\>\\)\\|"
2215 "\\(\\<`uvm_object_utils_end\\>\\)\\|"
2216 "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
2217 "\\(\\<`uvm_sequencer_utils_end\\>\\)"
6341f357
DN
2218 ))
2219
2220(defconst verilog-auto-end-comment-lines-re
da6062e6 2221 ;; Matches to names in this list cause auto-end-commenting
6341f357
DN
2222 (concat "\\("
2223 verilog-directive-re "\\)\\|\\("
2224 (eval-when-compile
2225 (verilog-regexp-words
2226 `( "begin"
2227 "else"
2228 "end"
2229 "endcase"
2230 "endclass"
2231 "endclocking"
2232 "endgroup"
2233 "endfunction"
2234 "endmodule"
2235 "endprogram"
2236 "endprimitive"
2237 "endinterface"
2238 "endpackage"
2239 "endsequence"
2240 "endspecify"
2241 "endtable"
2242 "endtask"
2243 "join"
2244 "join_any"
2245 "join_none"
2246 "module"
2247 "macromodule"
2248 "primitive"
2249 "interface"
2250 "package")))
2251 "\\)"))
2252
2253;;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
2254;;; verilog-end-block-ordered-re matches exactly the same strings.
2255(defconst verilog-end-block-ordered-re
2256 ;; Parenthesis indicate type of keyword found
2257 (concat "\\(\\<endcase\\>\\)\\|" ; 1
2258 "\\(\\<end\\>\\)\\|" ; 2
2259 "\\(\\<end" ; 3, but not used
2260 "\\(" ; 4, but not used
2261 "\\(function\\)\\|" ; 5
2262 "\\(task\\)\\|" ; 6
2263 "\\(module\\)\\|" ; 7
2264 "\\(primitive\\)\\|" ; 8
2265 "\\(interface\\)\\|" ; 9
2266 "\\(package\\)\\|" ; 10
2267 "\\(class\\)\\|" ; 11
2268 "\\(group\\)\\|" ; 12
2269 "\\(program\\)\\|" ; 13
2270 "\\(sequence\\)\\|" ; 14
2271 "\\(clocking\\)\\|" ; 15
2272 "\\)\\>\\)"))
2273(defconst verilog-end-block-re
2274 (eval-when-compile
2275 (verilog-regexp-words
2276
2277 `("end" ;; closes begin
2278 "endcase" ;; closes any of case, casex casez or randcase
2279 "join" "join_any" "join_none" ;; closes fork
2280 "endclass"
2281 "endtable"
2282 "endspecify"
2283 "endfunction"
2284 "endgenerate"
2285 "endtask"
2286 "endgroup"
2287 "endproperty"
2288 "endinterface"
2289 "endpackage"
2290 "endprogram"
2291 "endsequence"
2292 "endclocking"
a3a8b002
DN
2293 ;; OVM
2294 "`ovm_component_utils_end"
2295 "`ovm_field_utils_end"
2296 "`ovm_object_utils_end"
2297 "`ovm_sequence_utils_end"
2298 "`ovm_sequencer_utils_end"
9489a450
MM
2299 ;; UVM
2300 "`uvm_component_utils_end"
2301 "`uvm_field_utils_end"
2302 "`uvm_object_utils_end"
2303 "`uvm_sequence_utils_end"
2304 "`uvm_sequencer_utils_end"
86a4c7ac
DN
2305 ;; VMM
2306 "`vmm_data_member_end"
2307 "`vmm_env_member_end"
2308 "`vmm_scenario_member_end"
2309 "`vmm_subenv_member_end"
2310 "`vmm_xactor_member_end"
60618039 2311 ))))
6341f357
DN
2312
2313
2314(defconst verilog-endcomment-reason-re
2315 ;; Parenthesis indicate type of keyword found
2316 (concat
a3a8b002
DN
2317 "\\(\\<begin\\>\\)\\|" ; 1
2318 "\\(\\<else\\>\\)\\|" ; 2
2319 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
2320 "\\(\\<always_comb\\>\\(\[ \t\]*@\\)?\\)\\|" ; 4
2321 "\\(\\<always_ff\\>\\(\[ \t\]*@\\)?\\)\\|" ; 5
2322 "\\(\\<always_latch\\>\\(\[ \t\]*@\\)?\\)\\|" ; 6
2323 "\\(\\<fork\\>\\)\\|" ; 7
14862301 2324 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
6341f357 2325 "\\(\\<if\\>\\)\\|"
a03c2342
WS
2326 verilog-property-re "\\|"
2327 "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
6341f357 2328 "\\(\\<clocking\\>\\)\\|"
14862301
SM
2329 "\\(\\<task\\>\\)\\|"
2330 "\\(\\<function\\>\\)\\|"
6341f357
DN
2331 "\\(\\<initial\\>\\)\\|"
2332 "\\(\\<interface\\>\\)\\|"
2333 "\\(\\<package\\>\\)\\|"
2334 "\\(\\<final\\>\\)\\|"
6341f357
DN
2335 "\\(@\\)\\|"
2336 "\\(\\<while\\>\\)\\|"
2337 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
2338 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
2339 "#"))
2340
2341(defconst verilog-named-block-re "begin[ \t]*:")
2342
2343;; These words begin a block which can occur inside a module which should be indented,
2344;; and closed with the respective word from the end-block list
2345
2346(defconst verilog-beg-block-re
2347 (eval-when-compile
2348 (verilog-regexp-words
2349 `("begin"
2350 "case" "casex" "casez" "randcase"
2351 "clocking"
2352 "generate"
2353 "fork"
2354 "function"
2355 "property"
2356 "specify"
2357 "table"
2358 "task"
9489a450 2359 ;; OVM
a3a8b002 2360 "`ovm_component_utils_begin"
14862301 2361 "`ovm_component_param_utils_begin"
a3a8b002
DN
2362 "`ovm_field_utils_begin"
2363 "`ovm_object_utils_begin"
14862301 2364 "`ovm_object_param_utils_begin"
a3a8b002
DN
2365 "`ovm_sequence_utils_begin"
2366 "`ovm_sequencer_utils_begin"
9489a450
MM
2367 ;; UVM
2368 "`uvm_component_utils_begin"
2369 "`uvm_component_param_utils_begin"
2370 "`uvm_field_utils_begin"
2371 "`uvm_object_utils_begin"
2372 "`uvm_object_param_utils_begin"
2373 "`uvm_sequence_utils_begin"
2374 "`uvm_sequencer_utils_begin"
86a4c7ac
DN
2375 ;; VMM
2376 "`vmm_data_member_begin"
2377 "`vmm_env_member_begin"
2378 "`vmm_scenario_member_begin"
2379 "`vmm_subenv_member_begin"
2380 "`vmm_xactor_member_begin"
6341f357
DN
2381 ))))
2382;; These are the same words, in a specific order in the regular
2383;; expression so that matching will work nicely for
2384;; verilog-forward-sexp and verilog-calc-indent
6341f357 2385(defconst verilog-beg-block-re-ordered
4c5e69c6
DN
2386 ( concat "\\(\\<begin\\>\\)" ;1
2387 "\\|\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
9489a450 2388 "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
4c5e69c6
DN
2389 "\\|\\(\\<class\\>\\)" ;6
2390 "\\|\\(\\<table\\>\\)" ;7
2391 "\\|\\(\\<specify\\>\\)" ;8
2392 "\\|\\(\\<function\\>\\)" ;9
a3a8b002
DN
2393 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
2394 "\\|\\(\\<task\\>\\)" ;14
2395 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
2396 "\\|\\(\\<generate\\>\\)" ;18
2397 "\\|\\(\\<covergroup\\>\\)" ;16 20
2398 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
2399 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
2400 "\\|\\(\\<clocking\\>\\)" ;22 27
9489a450 2401 "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;28
86a4c7ac 2402 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
a3a8b002
DN
2403 ;;
2404
4c5e69c6 2405 ))
6341f357
DN
2406
2407(defconst verilog-end-block-ordered-rry
2408 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2409 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
2410 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2411 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
2412 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
2413 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
2414 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
2415 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
2416 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
2417 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
2418 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
2419 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
2420 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
2421 ] )
2422
2423(defconst verilog-nameable-item-re
2424 (eval-when-compile
2425 (verilog-regexp-words
2426 `("begin"
2427 "fork"
2428 "join" "join_any" "join_none"
2429 "end"
2430 "endcase"
2431 "endconfig"
2432 "endclass"
2433 "endclocking"
2434 "endfunction"
2435 "endgenerate"
2436 "endmodule"
86a4c7ac 2437 "endprimitive"
6341f357
DN
2438 "endinterface"
2439 "endpackage"
2440 "endspecify"
2441 "endtable"
2442 "endtask" )
2443 )))
2444
2445(defconst verilog-declaration-opener
2446 (eval-when-compile
2447 (verilog-regexp-words
2448 `("module" "begin" "task" "function"))))
2449
2450(defconst verilog-declaration-prefix-re
2451 (eval-when-compile
2452 (verilog-regexp-words
2453 `(
2454 ;; port direction
6edb5716 2455 "inout" "input" "output" "ref"
6341f357
DN
2456 ;; changeableness
2457 "const" "static" "protected" "local"
2458 ;; parameters
6edb5716 2459 "localparam" "parameter" "var"
6341f357
DN
2460 ;; type creation
2461 "typedef"
2462 ))))
2463(defconst verilog-declaration-core-re
2464 (eval-when-compile
2465 (verilog-regexp-words
2466 `(
7e2a6000 2467 ;; port direction (by themselves)
871c637e 2468 "inout" "input" "output"
6341f357
DN
2469 ;; integer_atom_type
2470 "byte" "shortint" "int" "longint" "integer" "time"
2471 ;; integer_vector_type
2472 "bit" "logic" "reg"
2473 ;; non_integer_type
2474 "shortreal" "real" "realtime"
2475 ;; net_type
2476 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
2477 ;; misc
2478 "string" "event" "chandle" "virtual" "enum" "genvar"
2479 "struct" "union"
2480 ;; builtin classes
6edb5716 2481 "mailbox" "semaphore"
6341f357 2482 ))))
6edb5716 2483(defconst verilog-declaration-re
6341f357
DN
2484 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
2485(defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
2486(defconst verilog-optional-signed-re "\\s-*\\(signed\\)?")
2487(defconst verilog-optional-signed-range-re
2488 (concat
2489 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
2490(defconst verilog-macroexp-re "`\\sw+")
2491
2492(defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
2493(defconst verilog-declaration-re-2-no-macro
2494 (concat "\\s-*" verilog-declaration-re
2495 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2496 "\\)?"))
2497(defconst verilog-declaration-re-2-macro
2498 (concat "\\s-*" verilog-declaration-re
2499 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2500 "\\|\\(" verilog-macroexp-re "\\)"
2501 "\\)?"))
2502(defconst verilog-declaration-re-1-macro
2503 (concat "^" verilog-declaration-re-2-macro))
2504
2505(defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2506
2507(defconst verilog-defun-re
2508 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2509(defconst verilog-end-defun-re
2510 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2511(defconst verilog-zero-indent-re
2512 (concat verilog-defun-re "\\|" verilog-end-defun-re))
9489a450
MM
2513(defconst verilog-inst-comment-re
2514 (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
6341f357
DN
2515
2516(defconst verilog-behavioral-block-beg-re
2a9eb3f5
DN
2517 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2518 "function" "task"))))
a3a8b002 2519(defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
6341f357
DN
2520(defconst verilog-indent-re
2521 (eval-when-compile
2522 (verilog-regexp-words
2523 `(
2524 "{"
2525 "always" "always_latch" "always_ff" "always_comb"
2526 "begin" "end"
2527; "unique" "priority"
2528 "case" "casex" "casez" "randcase" "endcase"
2529 "class" "endclass"
2530 "clocking" "endclocking"
2531 "config" "endconfig"
2532 "covergroup" "endgroup"
2533 "fork" "join" "join_any" "join_none"
2534 "function" "endfunction"
2535 "final"
2536 "generate" "endgenerate"
2537 "initial"
2538 "interface" "endinterface"
2539 "module" "macromodule" "endmodule"
2540 "package" "endpackage"
4c36be58 2541 "primitive" "endprimitive"
6341f357
DN
2542 "program" "endprogram"
2543 "property" "endproperty"
2544 "sequence" "randsequence" "endsequence"
2545 "specify" "endspecify"
2546 "table" "endtable"
2547 "task" "endtask"
4c5e69c6 2548 "virtual"
6341f357
DN
2549 "`case"
2550 "`default"
2551 "`define" "`undef"
a03c2342 2552 "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
6341f357
DN
2553 "`while" "`endwhile"
2554 "`for" "`endfor"
2555 "`format"
2556 "`include"
2557 "`let"
2558 "`protect" "`endprotect"
2559 "`switch" "`endswitch"
2560 "`timescale"
2561 "`time_scale"
a3a8b002
DN
2562 ;; OVM Begin tokens
2563 "`ovm_component_utils_begin"
14862301 2564 "`ovm_component_param_utils_begin"
a3a8b002
DN
2565 "`ovm_field_utils_begin"
2566 "`ovm_object_utils_begin"
14862301 2567 "`ovm_object_param_utils_begin"
a3a8b002
DN
2568 "`ovm_sequence_utils_begin"
2569 "`ovm_sequencer_utils_begin"
2570 ;; OVM End tokens
2571 "`ovm_component_utils_end"
2572 "`ovm_field_utils_end"
2573 "`ovm_object_utils_end"
2574 "`ovm_sequence_utils_end"
2575 "`ovm_sequencer_utils_end"
9489a450
MM
2576 ;; UVM Begin tokens
2577 "`uvm_component_utils_begin"
2578 "`uvm_component_param_utils_begin"
2579 "`uvm_field_utils_begin"
2580 "`uvm_object_utils_begin"
2581 "`uvm_object_param_utils_begin"
2582 "`uvm_sequence_utils_begin"
2583 "`uvm_sequencer_utils_begin"
2584 ;; UVM End tokens
2585 "`uvm_component_utils_end" ;; Typo in spec, it's not uvm_component_end
2586 "`uvm_field_utils_end"
2587 "`uvm_object_utils_end"
2588 "`uvm_sequence_utils_end"
2589 "`uvm_sequencer_utils_end"
86a4c7ac
DN
2590 ;; VMM Begin tokens
2591 "`vmm_data_member_begin"
2592 "`vmm_env_member_begin"
2593 "`vmm_scenario_member_begin"
2594 "`vmm_subenv_member_begin"
2595 "`vmm_xactor_member_begin"
2596 ;; VMM End tokens
2597 "`vmm_data_member_end"
2598 "`vmm_env_member_end"
2599 "`vmm_scenario_member_end"
2600 "`vmm_subenv_member_end"
2601 "`vmm_xactor_member_end"
6341f357
DN
2602 ))))
2603
a3a8b002
DN
2604(defconst verilog-defun-level-not-generate-re
2605 (eval-when-compile
2606 (verilog-regexp-words
2607 `( "module" "macromodule" "primitive" "class" "program"
2608 "interface" "package" "config"))))
2609
6341f357
DN
2610(defconst verilog-defun-level-re
2611 (eval-when-compile
2612 (verilog-regexp-words
a3a8b002
DN
2613 (append
2614 `( "module" "macromodule" "primitive" "class" "program"
2615 "interface" "package" "config")
2616 `( "initial" "final" "always" "always_comb" "always_ff"
2617 "always_latch" "endtask" "endfunction" )))))
6341f357 2618
a3a8b002 2619(defconst verilog-defun-level-generate-only-re
6341f357
DN
2620 (eval-when-compile
2621 (verilog-regexp-words
a3a8b002
DN
2622 `( "initial" "final" "always" "always_comb" "always_ff"
2623 "always_latch" "endtask" "endfunction" ))))
6341f357
DN
2624
2625(defconst verilog-cpp-level-re
2626 (eval-when-compile
2627 (verilog-regexp-words
2628 `(
2629 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2630 ))))
9489a450 2631(defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
6341f357
DN
2632(defconst verilog-extended-case-re "\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?")
2633(defconst verilog-extended-complete-re
a03c2342 2634 (concat "\\(\\<extern\\s-+\\|\\<\\(\\<pure\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)"
6341f357 2635 "\\|\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)"
a3a8b002 2636 "\\|\\(\\<import\\>\\s-+\\)?\"DPI-C\"\\s-+\\(function\\>\\|task\\>\\)"
6341f357
DN
2637 "\\|" verilog-extended-case-re ))
2638(defconst verilog-basic-complete-re
2639 (eval-when-compile
2640 (verilog-regexp-words
2641 `(
2642 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2643 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
14862301 2644 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
6341f357
DN
2645 ))))
2646(defconst verilog-complete-reg
2647 (concat
2648 verilog-extended-complete-re
2649 "\\|"
2650 verilog-basic-complete-re))
2651
2652(defconst verilog-end-statement-re
2653 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2654 verilog-end-block-re "\\)"))
2655
2656(defconst verilog-endcase-re
a3a8b002 2657 (concat verilog-extended-case-re "\\|"
6341f357
DN
2658 "\\(endcase\\)\\|"
2659 verilog-defun-re
2660 ))
2661
2662(defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2663 "String used to mark beginning of excluded text.")
2664(defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2665 "String used to mark end of excluded text.")
2666(defconst verilog-preprocessor-re
2667 (eval-when-compile
2668 (verilog-regexp-words
2669 `(
2670 "`define" "`include" "`ifdef" "`ifndef" "`if" "`endif" "`else"
2671 ))))
2672
2673(defconst verilog-keywords
2674 '( "`case" "`default" "`define" "`else" "`endfor" "`endif"
2675 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
2676 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
2677 "`time_scale" "`undef" "`while"
2678
2679 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2680 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2681 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2682 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2683 "config" "const" "constraint" "context" "continue" "cover"
2684 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2685 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2686 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2687 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2688 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2689 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2690 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2691 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2692 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2693 "include" "initial" "inout" "input" "inside" "instance" "int"
2694 "integer" "interface" "intersect" "join" "join_any" "join_none"
2695 "large" "liblist" "library" "local" "localparam" "logic"
2696 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2697 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2698 "notif0" "notif1" "null" "or" "output" "package" "packed"
2699 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2700 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2701 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2702 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2703 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2704 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2705 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2706 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2707 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2708 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2709 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2710 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2711 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2712 "wire" "with" "within" "wor" "xnor" "xor"
fd9ea9d3
WS
2713 ;; 1800-2009
2714 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2715 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2716 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2717 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
6341f357
DN
2718 )
2719 "List of Verilog keywords.")
2720
6341f357
DN
2721(defconst verilog-comment-start-regexp "//\\|/\\*"
2722 "Dual comment value for `comment-start-regexp'.")
2723
e1776067
DN
2724(defvar verilog-mode-syntax-table
2725 (let ((table (make-syntax-table)))
2726 ;; Populate the syntax TABLE.
2727 (modify-syntax-entry ?\\ "\\" table)
2728 (modify-syntax-entry ?+ "." table)
2729 (modify-syntax-entry ?- "." table)
2730 (modify-syntax-entry ?= "." table)
2731 (modify-syntax-entry ?% "." table)
2732 (modify-syntax-entry ?< "." table)
2733 (modify-syntax-entry ?> "." table)
2734 (modify-syntax-entry ?& "." table)
2735 (modify-syntax-entry ?| "." table)
2736 (modify-syntax-entry ?` "w" table)
2737 (modify-syntax-entry ?_ "w" table)
2738 (modify-syntax-entry ?\' "." table)
2739
2740 ;; Set up TABLE to handle block and line style comments.
2741 (if (featurep 'xemacs)
2742 (progn
2743 ;; XEmacs (formerly Lucid) has the best implementation
2744 (modify-syntax-entry ?/ ". 1456" table)
2745 (modify-syntax-entry ?* ". 23" table)
2746 (modify-syntax-entry ?\n "> b" table))
d63b01e1 2747 ;; Emacs does things differently, but we can work with it
e1776067
DN
2748 (modify-syntax-entry ?/ ". 124b" table)
2749 (modify-syntax-entry ?* ". 23" table)
2750 (modify-syntax-entry ?\n "> b" table))
2751 table)
37ea4b9b 2752 "Syntax table used in Verilog mode buffers.")
6341f357 2753
7ea26faf 2754(defvar verilog-font-lock-keywords nil
6341f357
DN
2755 "Default highlighting for Verilog mode.")
2756
7ea26faf 2757(defvar verilog-font-lock-keywords-1 nil
6341f357
DN
2758 "Subdued level highlighting for Verilog mode.")
2759
7ea26faf 2760(defvar verilog-font-lock-keywords-2 nil
6341f357
DN
2761 "Medium level highlighting for Verilog mode.
2762See also `verilog-font-lock-extra-types'.")
2763
7ea26faf 2764(defvar verilog-font-lock-keywords-3 nil
6341f357
DN
2765 "Gaudy level highlighting for Verilog mode.
2766See also `verilog-font-lock-extra-types'.")
2767(defvar verilog-font-lock-translate-off-face
2768 'verilog-font-lock-translate-off-face
2769 "Font to use for translated off regions.")
2770(defface verilog-font-lock-translate-off-face
2771 '((((class color)
2772 (background light))
2773 (:background "gray90" :italic t ))
2774 (((class color)
2775 (background dark))
2776 (:background "gray10" :italic t ))
2777 (((class grayscale) (background light))
2778 (:foreground "DimGray" :italic t))
2779 (((class grayscale) (background dark))
2780 (:foreground "LightGray" :italic t))
2781 (t (:italis t)))
2782 "Font lock mode face used to background highlight translate-off regions."
2783 :group 'font-lock-highlighting-faces)
2784
2785(defvar verilog-font-lock-p1800-face
2786 'verilog-font-lock-p1800-face
2787 "Font to use for p1800 keywords.")
2788(defface verilog-font-lock-p1800-face
2789 '((((class color)
2790 (background light))
2791 (:foreground "DarkOrange3" :bold t ))
2792 (((class color)
2793 (background dark))
2794 (:foreground "orange1" :bold t ))
2795 (t (:italic t)))
2796 "Font lock mode face used to highlight P1800 keywords."
2797 :group 'font-lock-highlighting-faces)
2798
2799(defvar verilog-font-lock-ams-face
2800 'verilog-font-lock-ams-face
2801 "Font to use for Analog/Mixed Signal keywords.")
2802(defface verilog-font-lock-ams-face
2803 '((((class color)
2804 (background light))
2805 (:foreground "Purple" :bold t ))
2806 (((class color)
2807 (background dark))
2808 (:foreground "orange1" :bold t ))
2809 (t (:italic t)))
2810 "Font lock mode face used to highlight AMS keywords."
2811 :group 'font-lock-highlighting-faces)
2812
2a9eb3f5
DN
2813(defvar verilog-font-grouping-keywords-face
2814 'verilog-font-lock-grouping-keywords-face
2815 "Font to use for Verilog Grouping Keywords (such as begin..end).")
2816(defface verilog-font-lock-grouping-keywords-face
2817 '((((class color)
2818 (background light))
2819 (:foreground "red4" :bold t ))
2820 (((class color)
2821 (background dark))
2822 (:foreground "red4" :bold t ))
2823 (t (:italic t)))
2824 "Font lock mode face used to highlight verilog grouping keywords."
2825 :group 'font-lock-highlighting-faces)
2826
6341f357
DN
2827(let* ((verilog-type-font-keywords
2828 (eval-when-compile
2829 (verilog-regexp-opt
2830 '(
2831 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
2832 "event" "genvar" "inout" "input" "integer" "localparam"
2833 "logic" "mailbox" "nand" "nmos" "not" "notif0" "notif1" "or"
fd9ea9d3 2834 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
6341f357
DN
2835 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
2836 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
2837 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
2838 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
2839 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
2840 ) nil )))
2841
2842 (verilog-pragma-keywords
2843 (eval-when-compile
2844 (verilog-regexp-opt
9c059794 2845 '("surefire" "synopsys" "rtl_synthesis" "verilint" "leda" "0in") nil
6341f357
DN
2846 )))
2847
fd9ea9d3 2848 (verilog-1800-2005-keywords
6341f357
DN
2849 (eval-when-compile
2850 (verilog-regexp-opt
2851 '("alias" "assert" "assume" "automatic" "before" "bind"
2852 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
2853 "clocking" "config" "const" "constraint" "context" "continue"
2854 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
2855 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
2856 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
2857 "expect" "export" "extends" "extern" "first_match" "foreach"
2858 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
2859 "illegal_bins" "import" "incdir" "include" "inside" "instance"
2860 "int" "intersect" "large" "liblist" "library" "local" "longint"
2861 "matches" "medium" "modport" "new" "noshowcancelled" "null"
2862 "packed" "program" "property" "protected" "pull0" "pull1"
2863 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2864 "randcase" "randsequence" "ref" "release" "return" "scalared"
2865 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
2866 "specparam" "static" "string" "strong0" "strong1" "struct"
2867 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
2868 "type" "union" "unsigned" "use" "var" "virtual" "void"
2869 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
2870 ) nil )))
2871
fd9ea9d3
WS
2872 (verilog-1800-2009-keywords
2873 (eval-when-compile
2874 (verilog-regexp-opt
2875 '("accept_on" "checker" "endchecker" "eventually" "global"
2876 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
2877 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
2878 "sync_accept_on" "sync_reject_on" "unique0" "until"
2879 "until_with" "untyped" "weak" ) nil )))
2880
6341f357
DN
2881 (verilog-ams-keywords
2882 (eval-when-compile
2883 (verilog-regexp-opt
2884 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
2885 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
2886 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
2887 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
2888 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
2889 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
2890 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
2891 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
2892 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
2893 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
2894 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
2895
2896 (verilog-font-keywords
2897 (eval-when-compile
2898 (verilog-regexp-opt
2899 '(
2a9eb3f5
DN
2900 "assign" "case" "casex" "casez" "randcase" "deassign"
2901 "default" "disable" "else" "endcase" "endfunction"
6341f357
DN
2902 "endgenerate" "endinterface" "endmodule" "endprimitive"
2903 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
2904 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
2905 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
2906 "package" "endpackage" "always" "always_comb" "always_ff"
2907 "always_latch" "posedge" "primitive" "priority" "release"
2908 "repeat" "specify" "table" "task" "unique" "wait" "while"
2909 "class" "program" "endclass" "endprogram"
2a9eb3f5
DN
2910 ) nil )))
2911
2912 (verilog-font-grouping-keywords
2913 (eval-when-compile
2914 (verilog-regexp-opt
2915 '( "begin" "end" ) nil ))))
6341f357
DN
2916
2917 (setq verilog-font-lock-keywords
2918 (list
2919 ;; Fontify all builtin keywords
2920 (concat "\\<\\(" verilog-font-keywords "\\|"
2921 ;; And user/system tasks and functions
a1ebd734
DN
2922 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
2923 "\\)\\>")
b1d0fc86
DN
2924 ;; Fontify all types
2925 (if verilog-highlight-grouping-keywords
2926 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2927 'verilog-font-lock-ams-face)
2928 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2929 'font-lock-type-face))
2930 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
a1ebd734 2931 'font-lock-type-face)
fd9ea9d3 2932 ;; Fontify IEEE-1800-2005 keywords appropriately
6341f357 2933 (if verilog-highlight-p1800-keywords
fd9ea9d3 2934 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
6341f357 2935 'verilog-font-lock-p1800-face)
fd9ea9d3
WS
2936 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2937 'font-lock-type-face))
2938 ;; Fontify IEEE-1800-2009 keywords appropriately
2939 (if verilog-highlight-p1800-keywords
2940 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2941 'verilog-font-lock-p1800-face)
2942 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
6341f357
DN
2943 'font-lock-type-face))
2944 ;; Fontify Verilog-AMS keywords
2945 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
60618039 2946 'verilog-font-lock-ams-face)))
6341f357
DN
2947
2948 (setq verilog-font-lock-keywords-1
2949 (append verilog-font-lock-keywords
2950 (list
2951 ;; Fontify module definitions
2952 (list
2953 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
2954 '(1 font-lock-keyword-face)
2955 '(3 font-lock-function-name-face 'prepend))
2956 ;; Fontify function definitions
2957 (list
2958 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
2959 '(1 font-lock-keyword-face)
ecb0ab90 2960 '(3 font-lock-constant-face prepend))
6341f357
DN
2961 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
2962 (1 font-lock-keyword-face)
ecb0ab90 2963 (2 font-lock-constant-face append))
6341f357 2964 '("\\<function\\>\\s-+\\(\\sw+\\)"
ecb0ab90 2965 1 'font-lock-constant-face append))))
6341f357
DN
2966
2967 (setq verilog-font-lock-keywords-2
2968 (append verilog-font-lock-keywords-1
2969 (list
2970 ;; Fontify pragmas
2971 (concat "\\(//\\s-*" verilog-pragma-keywords "\\s-.*\\)")
2972 ;; Fontify escaped names
2973 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
2974 ;; Fontify macro definitions/ uses
2975 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
2976 'font-lock-preprocessor-face
2977 'font-lock-type-face))
2978 ;; Fontify delays/numbers
2979 '("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9_.\]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
2980 0 font-lock-type-face append)
2981 ;; Fontify instantiation names
a03c2342 2982 '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
6341f357
DN
2983 )))
2984
2985 (setq verilog-font-lock-keywords-3
2986 (append verilog-font-lock-keywords-2
2987 (when verilog-highlight-translate-off
2988 (list
2989 ;; Fontify things in translate off regions
7ea26faf
DN
2990 '(verilog-match-translate-off
2991 (0 'verilog-font-lock-translate-off-face prepend))
2992 )))))
6341f357 2993
a03c2342
WS
2994;;
2995;; Buffer state preservation
2996
2997(defmacro verilog-save-buffer-state (&rest body)
2998 "Execute BODY forms, saving state around insignificant change.
2999Changes in text properties like `face' or `syntax-table' are
3000considered insignificant. This macro allows text properties to
3001be changed, even in a read-only buffer.
3002
3003A change is considered significant if it affects the buffer text
3004in any way that isn't completely restored again. Any
3005user-visible changes to the buffer must not be within a
3006`verilog-save-buffer-state'."
3007 ;; From c-save-buffer-state
3008 `(let* ((modified (buffer-modified-p))
3009 (buffer-undo-list t)
3010 (inhibit-read-only t)
3011 (inhibit-point-motion-hooks t)
9489a450 3012 (verilog-no-change-functions t)
a03c2342
WS
3013 before-change-functions
3014 after-change-functions
3015 deactivate-mark
3016 buffer-file-name ; Prevent primitives checking
3017 buffer-file-truename) ; for file modification
3018 (unwind-protect
3019 (progn ,@body)
3020 (and (not modified)
3021 (buffer-modified-p)
3022 (set-buffer-modified-p nil)))))
3023
3024(defmacro verilog-save-no-change-functions (&rest body)
3025 "Execute BODY forms, disabling all change hooks in BODY.
53964682 3026For insignificant changes, see instead `verilog-save-buffer-state'."
a03c2342 3027 `(let* ((inhibit-point-motion-hooks t)
9489a450 3028 (verilog-no-change-functions t)
a03c2342
WS
3029 before-change-functions
3030 after-change-functions)
3031 (progn ,@body)))
6341f357 3032
a03c2342
WS
3033;;
3034;; Comment detection and caching
3035
3036(defvar verilog-scan-cache-preserving nil
3037 "If set, the specified buffer's comment properties are static.
9489a450 3038Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
a03c2342
WS
3039and `verilog-scan'.")
3040
3041(defvar verilog-scan-cache-tick nil
3042 "Modification tick at which `verilog-scan' was last completed.")
3043(make-variable-buffer-local 'verilog-scan-cache-tick)
3044
9489a450
MM
3045(defun verilog-scan-cache-flush ()
3046 "Flush the `verilog-scan' cache."
3047 (setq verilog-scan-cache-tick nil))
3048
a03c2342
WS
3049(defun verilog-scan-cache-ok-p ()
3050 "Return t iff the scan cache is up to date."
3051 (or (and verilog-scan-cache-preserving
3052 (eq verilog-scan-cache-preserving (current-buffer))
3053 verilog-scan-cache-tick)
3054 (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
3055
3056(defmacro verilog-save-scan-cache (&rest body)
3057 "Execute the BODY forms, allowing scan cache preservation within BODY.
3058This requires that insertions must use `verilog-insert'."
3059 ;; If the buffer is out of date, trash it, as we'll not check later the tick
3060 ;; Note this must work properly if there's multiple layers of calls
3061 ;; to verilog-save-scan-cache even with differing ticks.
3062 `(progn
3063 (unless (verilog-scan-cache-ok-p) ;; Must be before let
3064 (setq verilog-scan-cache-tick nil))
3065 (let* ((verilog-scan-cache-preserving (current-buffer)))
3066 (progn ,@body))))
3067
3068(defun verilog-scan-region (beg end)
9489a450
MM
3069 "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
3070This creates v-cmts properties where comments are in force."
a03c2342
WS
3071 ;; Why properties and not overlays? Overlays have much slower non O(1)
3072 ;; lookup times.
3073 ;; This function is warm - called on every verilog-insert
6341f357 3074 (save-excursion
a03c2342
WS
3075 (save-match-data
3076 (verilog-save-buffer-state
3077 (let (pt)
3078 (goto-char beg)
3079 (while (< (point) end)
3080 (cond ((looking-at "//")
3081 (setq pt (point))
3082 (or (search-forward "\n" end t)
3083 (goto-char end))
3084 ;; "1+": The leading // or /* itself isn't considered as
3085 ;; being "inside" the comment, so that a (search-backward)
3086 ;; that lands at the start of the // won't mis-indicate
9489a450
MM
3087 ;; it's inside a comment. Also otherwise it would be
3088 ;; hard to find a commented out /*AS*/ vs one that isn't
3089 (put-text-property (1+ pt) (point) 'v-cmts t))
a03c2342
WS
3090 ((looking-at "/\\*")
3091 (setq pt (point))
3092 (or (search-forward "*/" end t)
3093 ;; No error - let later code indicate it so we can
3094 ;; use inside functions on-the-fly
3095 ;;(error "%s: Unmatched /* */, at char %d"
3096 ;; (verilog-point-text) (point))
3097 (goto-char end))
9489a450
MM
3098 (put-text-property (1+ pt) (point) 'v-cmts t))
3099 ((looking-at "\"")
3100 (setq pt (point))
3101 (or (re-search-forward "[^\\]\"" end t) ;; don't forward-char first, since we look for a non backslash first
3102 ;; No error - let later code indicate it so we can
3103 (goto-char end))
3104 (put-text-property (1+ pt) (point) 'v-cmts t))
a03c2342
WS
3105 (t
3106 (forward-char 1)
9489a450
MM
3107 (if (re-search-forward "[/\"]" end t)
3108 (backward-char 1)
a03c2342
WS
3109 (goto-char end))))))))))
3110
3111(defun verilog-scan ()
3112 "Parse the buffer, marking all comments with properties.
3113Also assumes any text inserted since `verilog-scan-cache-tick'
3114either is ok to parse as a non-comment, or `verilog-insert' was used."
9489a450 3115 ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
a03c2342
WS
3116 (unless (verilog-scan-cache-ok-p)
3117 (save-excursion
3118 (verilog-save-buffer-state
3119 (when verilog-debug
3120 (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
3121 verilog-scan-cache-preserving verilog-scan-cache-tick
3122 (buffer-chars-modified-tick)))
9489a450 3123 (remove-text-properties (point-min) (point-max) '(v-cmts nil))
a03c2342
WS
3124 (verilog-scan-region (point-min) (point-max))
3125 (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
8350f087 3126 (when verilog-debug (message "Scanning... done"))))))
6341f357 3127
9489a450
MM
3128(defun verilog-scan-debug ()
3129 "For debugging, show with display face results of `verilog-scan'."
3130 (font-lock-mode 0)
3131 ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
3132 (save-excursion
3133 (goto-char (point-min))
3134 (remove-text-properties (point-min) (point-max) '(face nil))
3135 (while (not (eobp))
3136 (cond ((get-text-property (point) 'v-cmts)
3137 (put-text-property (point) (1+ (point)) `face 'underline)
3138 ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
3139 (forward-char 1))
3140 (t
3141 (goto-char (or (next-property-change (point)) (point-max))))))))
3142
3143(defun verilog-scan-and-debug ()
3144 "For debugging, run `verilog-scan' and `verilog-scan-debug'."
3145 (let (verilog-scan-cache-preserving
3146 verilog-scan-cache-tick)
3147 (goto-char (point-min))
3148 (verilog-scan)
3149 (verilog-scan-debug)))
3150
3151(defun verilog-inside-comment-or-string-p (&optional pos)
3152 "Check if optional point POS is inside a comment.
a03c2342
WS
3153This may require a slow pre-parse of the buffer with `verilog-scan'
3154to establish comment properties on all text."
3155 ;; This function is very hot
3156 (verilog-scan)
9489a450
MM
3157 (if pos
3158 (and (>= pos (point-min))
3159 (get-text-property pos 'v-cmts))
3160 (get-text-property (point) 'v-cmts)))
a03c2342
WS
3161
3162(defun verilog-insert (&rest stuff)
9489a450 3163 "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
a03c2342
WS
3164Any insert that includes a comment must have the entire commente
3165inserted using a single call to `verilog-insert'."
3166 (let ((pt (point)))
3167 (while stuff
3168 (insert (car stuff))
3169 (setq stuff (cdr stuff)))
3170 (verilog-scan-region pt (point))))
3171
3172;; More searching
6341f357
DN
3173
3174(defun verilog-declaration-end ()
3175 (search-forward ";"))
3176
3177(defun verilog-point-text (&optional pointnum)
3178 "Return text describing where POINTNUM or current point is (for errors).
3179Use filename, if current buffer being edited shorten to just buffer name."
3180 (concat (or (and (equal (window-buffer (selected-window)) (current-buffer))
3181 (buffer-name))
3182 buffer-file-name
3183 (buffer-name))
9489a450 3184 ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
6341f357
DN
3185
3186(defun electric-verilog-backward-sexp ()
495ab0d5 3187 "Move backward over one balanced expression."
6341f357
DN
3188 (interactive)
3189 ;; before that see if we are in a comment
60618039
DN
3190 (verilog-backward-sexp))
3191
6341f357 3192(defun electric-verilog-forward-sexp ()
495ab0d5 3193 "Move forward over one balanced expression."
6341f357
DN
3194 (interactive)
3195 ;; before that see if we are in a comment
60618039
DN
3196 (verilog-forward-sexp))
3197
6341f357
DN
3198;;;used by hs-minor-mode
3199(defun verilog-forward-sexp-function (arg)
3200 (if (< arg 0)
3201 (verilog-backward-sexp)
3202 (verilog-forward-sexp)))
3203
3204
3205(defun verilog-backward-sexp ()
3206 (let ((reg)
3207 (elsec 1)
3208 (found nil)
60618039 3209 (st (point)))
6341f357
DN
3210 (if (not (looking-at "\\<"))
3211 (forward-word -1))
3212 (cond
60618039 3213 ((verilog-skip-backward-comment-or-string))
6341f357
DN
3214 ((looking-at "\\<else\\>")
3215 (setq reg (concat
3216 verilog-end-block-re
3217 "\\|\\(\\<else\\>\\)"
60618039 3218 "\\|\\(\\<if\\>\\)"))
6341f357
DN
3219 (while (and (not found)
3220 (verilog-re-search-backward reg nil 'move))
3221 (cond
3222 ((match-end 1) ; matched verilog-end-block-re
3223 ; try to leap back to matching outward block by striding across
3224 ; indent level changing tokens then immediately
3225 ; previous line governs indentation.
3226 (verilog-leap-to-head))
3227 ((match-end 2) ; else, we're in deep
3228 (setq elsec (1+ elsec)))
3229 ((match-end 3) ; found it
3230 (setq elsec (1- elsec))
3231 (if (= 0 elsec)
3232 ;; Now previous line describes syntax
60618039 3233 (setq found 't))))))
6341f357
DN
3234 ((looking-at verilog-end-block-re)
3235 (verilog-leap-to-head))
3236 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
3237 (cond
3238 ((match-end 1)
3239 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
3240 ((match-end 2)
3241 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
3242 ((match-end 3)
3243 (verilog-re-search-backward "\\<class\\>" nil 'move))
3244 ((match-end 4)
3245 (verilog-re-search-backward "\\<program\\>" nil 'move))
3246 ((match-end 5)
3247 (verilog-re-search-backward "\\<interface\\>" nil 'move))
3248 ((match-end 6)
3249 (verilog-re-search-backward "\\<package\\>" nil 'move))
3250 (t
3251 (goto-char st)
3252 (backward-sexp 1))))
3253 (t
3254 (goto-char st)
60618039 3255 (backward-sexp)))))
6341f357
DN
3256
3257(defun verilog-forward-sexp ()
3258 (let ((reg)
3259 (md 2)
4c5e69c6
DN
3260 (st (point))
3261 (nest 'yes))
6341f357
DN
3262 (if (not (looking-at "\\<"))
3263 (forward-word -1))
3264 (cond
3265 ((verilog-skip-forward-comment-or-string)
60618039 3266 (verilog-forward-syntactic-ws))
4c5e69c6 3267 ((looking-at verilog-beg-block-re-ordered)
6341f357 3268 (cond
4c5e69c6
DN
3269 ((match-end 1);
3270 ;; Search forward for matching end
6341f357 3271 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
4c5e69c6
DN
3272 ((match-end 2)
3273 ;; Search forward for matching endcase
2a9eb3f5
DN
3274 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
3275 (setq md 3) ;; ender is third item in regexp
3276 )
4c5e69c6 3277 ((match-end 4)
9489a450 3278 ;; might be "disable fork" or "wait fork"
fd9ea9d3
WS
3279 (let
3280 (here)
9489a450
MM
3281 (if (or
3282 (looking-at verilog-disable-fork-re)
3283 (and (looking-at "fork")
3284 (progn
3285 (setq here (point)) ;; sometimes a fork is just a fork
3286 (forward-word -1)
3287 (looking-at verilog-disable-fork-re))))
3288 (progn ;; it is a disable fork; ignore it
fd9ea9d3 3289 (goto-char (match-end 0))
9489a450 3290 (forward-word 1)
fd9ea9d3 3291 (setq reg nil))
9489a450
MM
3292 (progn ;; it is a nice simple fork
3293 (goto-char here) ;; return from looking for "disable fork"
3294 ;; Search forward for matching join
3295 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
4c5e69c6
DN
3296 ((match-end 6)
3297 ;; Search forward for matching endclass
6341f357 3298 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
a3a8b002 3299
4c5e69c6
DN
3300 ((match-end 7)
3301 ;; Search forward for matching endtable
3302 (setq reg "\\<endtable\\>" )
3303 (setq nest 'no))
3304 ((match-end 8)
3305 ;; Search forward for matching endspecify
3306 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
3307 ((match-end 9)
3308 ;; Search forward for matching endfunction
3309 (setq reg "\\<endfunction\\>" )
3310 (setq nest 'no))
3311 ((match-end 10)
a3a8b002
DN
3312 ;; Search forward for matching endfunction
3313 (setq reg "\\<endfunction\\>" )
3314 (setq nest 'no))
3315 ((match-end 14)
4c5e69c6
DN
3316 ;; Search forward for matching endtask
3317 (setq reg "\\<endtask\\>" )
3318 (setq nest 'no))
a3a8b002 3319 ((match-end 15)
4c5e69c6
DN
3320 ;; Search forward for matching endtask
3321 (setq reg "\\<endtask\\>" )
3322 (setq nest 'no))
a3a8b002 3323 ((match-end 19)
4c5e69c6
DN
3324 ;; Search forward for matching endgenerate
3325 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
a3a8b002 3326 ((match-end 20)
4c5e69c6
DN
3327 ;; Search forward for matching endgroup
3328 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
a3a8b002 3329 ((match-end 21)
4c5e69c6
DN
3330 ;; Search forward for matching endproperty
3331 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
a3a8b002 3332 ((match-end 25)
4c5e69c6
DN
3333 ;; Search forward for matching endsequence
3334 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
3335 (setq md 3)) ; 3 to get to endsequence in the reg above
a3a8b002 3336 ((match-end 27)
4c5e69c6
DN
3337 ;; Search forward for matching endclocking
3338 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
2a9eb3f5
DN
3339 (if (and reg
3340 (forward-word 1))
6341f357 3341 (catch 'skip
a3a8b002 3342 (if (eq nest 'yes)
82883f0a
GM
3343 (let ((depth 1)
3344 here)
4c5e69c6
DN
3345 (while (verilog-re-search-forward reg nil 'move)
3346 (cond
fd9ea9d3 3347 ((match-end md) ; a closer in regular expression, so we are climbing out
4c5e69c6
DN
3348 (setq depth (1- depth))
3349 (if (= 0 depth) ; we are out!
3350 (throw 'skip 1)))
fd9ea9d3
WS
3351 ((match-end 1) ; an opener in the r-e, so we are in deeper now
3352 (setq here (point)) ; remember where we started
3353 (goto-char (match-beginning 1))
3354 (cond
fd9ea9d3
WS
3355 ((if (or
3356 (looking-at verilog-disable-fork-re)
3357 (and (looking-at "fork")
82883f0a 3358 (progn
fd9ea9d3
WS
3359 (forward-word -1)
3360 (looking-at verilog-disable-fork-re))))
3361 (progn ;; it is a disable fork; another false alarm
3362 (goto-char (match-end 0)))
3363 (progn ;; it is a simple fork (or has nothing to do with fork)
3364 (goto-char here)
3365 (setq depth (1+ depth))))))))))
4c5e69c6
DN
3366 (if (verilog-re-search-forward reg nil 'move)
3367 (throw 'skip 1))))))
a3a8b002 3368
6341f357
DN
3369 ((looking-at (concat
3370 "\\(\\<\\(macro\\)?module\\>\\)\\|"
3371 "\\(\\<primitive\\>\\)\\|"
3372 "\\(\\<class\\>\\)\\|"
3373 "\\(\\<program\\>\\)\\|"
3374 "\\(\\<interface\\>\\)\\|"
3375 "\\(\\<package\\>\\)"))
3376 (cond
3377 ((match-end 1)
3378 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
3379 ((match-end 2)
3380 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
3381 ((match-end 3)
3382 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
3383 ((match-end 4)
3384 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
3385 ((match-end 5)
3386 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
3387 ((match-end 6)
3388 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
3389 (t
3390 (goto-char st)
3391 (if (= (following-char) ?\) )
3392 (forward-char 1)
3393 (forward-sexp 1)))))
3394 (t
3395 (goto-char st)
3396 (if (= (following-char) ?\) )
3397 (forward-char 1)
60618039 3398 (forward-sexp 1))))))
6341f357
DN
3399
3400(defun verilog-declaration-beg ()
3401 (verilog-re-search-backward verilog-declaration-re (bobp) t))
3402
6341f357
DN
3403;;
3404;;
3405;; Mode
3406;;
3407(defvar verilog-which-tool 1)
6edb5716 3408;;;###autoload
175069ef 3409(define-derived-mode verilog-mode prog-mode "Verilog"
6341f357
DN
3410 "Major mode for editing Verilog code.
3411\\<verilog-mode-map>
3412See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
3413AUTOs can improve coding efficiency.
3414
3415Use \\[verilog-faq] for a pointer to frequently asked questions.
3416
3417NEWLINE, TAB indents for Verilog code.
3418Delete converts tabs to spaces as it moves back.
3419
3420Supports highlighting.
3421
3422Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
3423with no args, if that value is non-nil.
3424
3425Variables controlling indentation/edit style:
3426
3427 variable `verilog-indent-level' (default 3)
3428 Indentation of Verilog statements with respect to containing block.
3429 `verilog-indent-level-module' (default 3)
3430 Absolute indentation of Module level Verilog statements.
3431 Set to 0 to get initial and always statements lined up
3432 on the left side of your screen.
3433 `verilog-indent-level-declaration' (default 3)
3434 Indentation of declarations with respect to containing block.
3435 Set to 0 to get them list right under containing block.
3436 `verilog-indent-level-behavioral' (default 3)
3437 Indentation of first begin in a task or function block
37ea4b9b
JB
3438 Set to 0 to get such code to lined up underneath the task or
3439 function keyword.
6341f357 3440 `verilog-indent-level-directive' (default 1)
37ea4b9b 3441 Indentation of `ifdef/`endif blocks.
6341f357
DN
3442 `verilog-cexp-indent' (default 1)
3443 Indentation of Verilog statements broken across lines i.e.:
3444 if (a)
3445 begin
3446 `verilog-case-indent' (default 2)
3447 Indentation for case statements.
3448 `verilog-auto-newline' (default nil)
3449 Non-nil means automatically newline after semicolons and the punctuation
3450 mark after an end.
3451 `verilog-auto-indent-on-newline' (default t)
37ea4b9b 3452 Non-nil means automatically indent line after newline.
6341f357
DN
3453 `verilog-tab-always-indent' (default t)
3454 Non-nil means TAB in Verilog mode should always reindent the current line,
3455 regardless of where in the line point is when the TAB command is used.
3456 `verilog-indent-begin-after-if' (default t)
3457 Non-nil means to indent begin statements following a preceding
37ea4b9b 3458 if, else, while, for and repeat statements, if any. Otherwise,
6341f357
DN
3459 the begin is lined up with the preceding token. If t, you get:
3460 if (a)
3461 begin // amount of indent based on `verilog-cexp-indent'
3462 otherwise you get:
3463 if (a)
3464 begin
3465 `verilog-auto-endcomments' (default t)
3466 Non-nil means a comment /* ... */ is set after the ends which ends
3467 cases, tasks, functions and modules.
3468 The type and name of the object will be set between the braces.
3469 `verilog-minimum-comment-distance' (default 10)
3470 Minimum distance (in lines) between begin and end required before a comment
3471 will be inserted. Setting this variable to zero results in every
3472 end acquiring a comment; the default avoids too many redundant
3473 comments in tight quarters.
a3a8b002 3474 `verilog-auto-lineup' (default 'declarations)
6341f357
DN
3475 List of contexts where auto lineup of code should be done.
3476
3477Variables controlling other actions:
3478
3479 `verilog-linter' (default surelint)
3480 Unix program to call to run the lint checker. This is the default
3481 command for \\[compile-command] and \\[verilog-auto-save-compile].
3482
3483See \\[customize] for the complete list of variables.
3484
3485AUTO expansion functions are, in part:
3486
3487 \\[verilog-auto] Expand AUTO statements.
3488 \\[verilog-delete-auto] Remove the AUTOs.
3489 \\[verilog-inject-auto] Insert AUTOs for the first time.
3490
3491Some other functions are:
3492
3493 \\[verilog-complete-word] Complete word with appropriate possibilities.
3494 \\[verilog-mark-defun] Mark function.
3495 \\[verilog-beg-of-defun] Move to beginning of current function.
3496 \\[verilog-end-of-defun] Move to end of current function.
3497 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
3498
3499 \\[verilog-comment-region] Put marked area in a comment.
3500 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
495ab0d5 3501 \\[verilog-insert-block] Insert begin ... end.
6341f357
DN
3502 \\[verilog-star-comment] Insert /* ... */.
3503
495ab0d5 3504 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
6341f357
DN
3505 \\[verilog-sk-begin] Insert a begin .. end block.
3506 \\[verilog-sk-case] Insert a case block, prompting for details.
3507 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
3508 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
495ab0d5 3509 \\[verilog-sk-header] Insert a header block at the top of file.
6341f357
DN
3510 \\[verilog-sk-initial] Insert an initial begin .. end block.
3511 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
3512 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
9489a450
MM
3513 \\[verilog-sk-ovm-class] Insert an OVM Class block.
3514 \\[verilog-sk-uvm-class] Insert an UVM Class block.
6341f357
DN
3515 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
3516 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
3517 \\[verilog-sk-specify] Insert a specify .. endspecify block.
3518 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
3519 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
3520 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
3521 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
3522 \\[verilog-sk-if] Insert an if (..) begin .. end block.
3523 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
3524 \\[verilog-sk-comment] Insert a comment block.
3525 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
3526 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
3527 \\[verilog-sk-input] Insert an input declaration, prompting for details.
3528 \\[verilog-sk-output] Insert an output declaration, prompting for details.
3529 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
3530 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
3531 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
3532 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
3533 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
3534
3535All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
3536Key bindings specific to `verilog-mode-map' are:
3537
3538\\{verilog-mode-map}"
175069ef 3539 :abbrev-table verilog-mode-abbrev-table
60618039 3540 (set (make-local-variable 'beginning-of-defun-function)
6edb5716 3541 'verilog-beg-of-defun)
60618039 3542 (set (make-local-variable 'end-of-defun-function)
6edb5716 3543 'verilog-end-of-defun)
6341f357 3544 (set-syntax-table verilog-mode-syntax-table)
175069ef
SM
3545 (set (make-local-variable 'indent-line-function)
3546 #'verilog-indent-line-relative)
6341f357 3547 (setq comment-indent-function 'verilog-comment-indent)
175069ef 3548 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
175069ef
SM
3549 (set (make-local-variable 'comment-start) "// ")
3550 (set (make-local-variable 'comment-end) "")
3551 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
3552 (set (make-local-variable 'comment-multi-line) nil)
6341f357
DN
3553 ;; Set up for compilation
3554 (setq verilog-which-tool 1)
3555 (setq verilog-tool 'verilog-linter)
3556 (verilog-set-compile-command)
3557 (when (boundp 'hack-local-variables-hook) ;; Also modify any file-local-variables
3558 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
3559
3560 ;; Setting up menus
6edb5716 3561 (when (featurep 'xemacs)
7d55bf04
DN
3562 (easy-menu-add verilog-stmt-menu)
3563 (easy-menu-add verilog-menu)
3564 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
60618039 3565
d63b01e1 3566 ;; Stuff for GNU Emacs
7ea26faf 3567 (set (make-local-variable 'font-lock-defaults)
7664ec44 3568 `((verilog-font-lock-keywords verilog-font-lock-keywords-1
7ea26faf
DN
3569 verilog-font-lock-keywords-2
3570 verilog-font-lock-keywords-3)
7664ec44 3571 nil nil nil
a3a8b002
DN
3572 ,(if (functionp 'syntax-ppss)
3573 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
3574 ;; font-lock-beginning-of-syntax-function, so
3575 ;; font-lock-beginning-of-syntax-function, can't use
7664ec44 3576 ;; verilog-beg-of-defun.
a3a8b002
DN
3577 nil
3578 'verilog-beg-of-defun)))
6341f357 3579 ;;------------------------------------------------------------
a03c2342 3580 ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
6341f357 3581 ;; all buffer local:
a03c2342
WS
3582 (unless noninteractive ;; Else can't see the result, and change hooks are slow
3583 (when (featurep 'xemacs)
3584 (make-local-hook 'font-lock-mode-hook)
3585 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
3586 (make-local-hook 'after-change-functions))
3587 (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
3588 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
3589 (add-hook 'after-change-functions 'verilog-highlight-region t t))
6341f357 3590
37ea4b9b 3591 ;; Tell imenu how to handle Verilog.
175069ef
SM
3592 (set (make-local-variable 'imenu-generic-expression)
3593 verilog-imenu-generic-expression)
9c059794 3594 ;; Tell which-func-modes that imenu knows about verilog
7d0da90e 3595 (when (boundp 'which-func-modes)
9c059794 3596 (add-to-list 'which-func-modes 'verilog-mode))
6341f357 3597 ;; hideshow support
9c059794
DN
3598 (when (boundp 'hs-special-modes-alist)
3599 (unless (assq 'verilog-mode hs-special-modes-alist)
3600 (setq hs-special-modes-alist
3601 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
3602 verilog-forward-sexp-function)
3603 hs-special-modes-alist))))
6341f357
DN
3604
3605 ;; Stuff for autos
9489a450
MM
3606 (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
3607 ;; verilog-mode-hook call added by define-derived-mode
3608 )
6341f357
DN
3609\f
3610
3611;;
3612;; Electric functions
3613;;
3614(defun electric-verilog-terminate-line (&optional arg)
3615 "Terminate line and indent next line.
3616With optional ARG, remove existing end of line comments."
3617 (interactive)
3618 ;; before that see if we are in a comment
d63b01e1 3619 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
3620 (cond
3621 ((nth 7 state) ; Inside // comment
3622 (if (eolp)
3623 (progn
3624 (delete-horizontal-space)
3625 (newline))
3626 (progn
3627 (newline)
7ea26faf 3628 (insert "// ")
6341f357
DN
3629 (beginning-of-line)))
3630 (verilog-indent-line))
3631 ((nth 4 state) ; Inside any comment (hence /**/)
3632 (newline)
3633 (verilog-more-comment))
3634 ((eolp)
3635 ;; First, check if current line should be indented
3636 (if (save-excursion
3637 (delete-horizontal-space)
3638 (beginning-of-line)
3639 (skip-chars-forward " \t")
3640 (if (looking-at verilog-auto-end-comment-lines-re)
3641 (let ((indent-str (verilog-indent-line)))
3642 ;; Maybe we should set some endcomments
3643 (if verilog-auto-endcomments
3644 (verilog-set-auto-endcomments indent-str arg))
3645 (end-of-line)
3646 (delete-horizontal-space)
3647 (if arg
3648 ()
3649 (newline))
3650 nil)
3651 (progn
3652 (end-of-line)
3653 (delete-horizontal-space)
60618039 3654 't)))
6341f357
DN
3655 ;; see if we should line up assignments
3656 (progn
a3a8b002
DN
3657 (if (or (eq 'all verilog-auto-lineup)
3658 (eq 'assignments verilog-auto-lineup))
3659 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
60618039
DN
3660 (newline))
3661 (forward-line 1))
6341f357
DN
3662 ;; Indent next line
3663 (if verilog-auto-indent-on-newline
60618039 3664 (verilog-indent-line)))
6341f357 3665 (t
60618039 3666 (newline)))))
6341f357
DN
3667
3668(defun electric-verilog-terminate-and-indent ()
3669 "Insert a newline and indent for the next statement."
3670 (interactive)
3671 (electric-verilog-terminate-line 1))
3672
3673(defun electric-verilog-semi ()
3674 "Insert `;' character and reindent the line."
3675 (interactive)
a3a8b002 3676 (verilog-insert-last-command-event)
6341f357
DN
3677
3678 (if (or (verilog-in-comment-or-string-p)
3679 (verilog-in-escaped-name-p))
3680 ()
3681 (save-excursion
3682 (beginning-of-line)
3683 (verilog-forward-ws&directives)
60618039 3684 (verilog-indent-line))
6341f357
DN
3685 (if (and verilog-auto-newline
3686 (not (verilog-parenthesis-depth)))
3687 (electric-verilog-terminate-line))))
3688
3689(defun electric-verilog-semi-with-comment ()
3690 "Insert `;' character, reindent the line and indent for comment."
3691 (interactive)
3692 (insert "\;")
3693 (save-excursion
3694 (beginning-of-line)
3695 (verilog-indent-line))
3696 (indent-for-comment))
3697
3698(defun electric-verilog-colon ()
3699 "Insert `:' and do all indentations except line indent on this line."
3700 (interactive)
a3a8b002 3701 (verilog-insert-last-command-event)
6341f357
DN
3702 ;; Do nothing if within string.
3703 (if (or
3704 (verilog-within-string)
3705 (not (verilog-in-case-region-p)))
3706 ()
3707 (save-excursion
3708 (let ((p (point))
3709 (lim (progn (verilog-beg-of-statement) (point))))
3710 (goto-char p)
3711 (verilog-backward-case-item lim)
3712 (verilog-indent-line)))
3713;; (let ((verilog-tab-always-indent nil))
3714;; (verilog-indent-line))
3715 ))
3716
3717;;(defun electric-verilog-equal ()
3718;; "Insert `=', and do indentation if within block."
3719;; (interactive)
a3a8b002 3720;; (verilog-insert-last-command-event)
6341f357
DN
3721;; Could auto line up expressions, but not yet
3722;; (if (eq (car (verilog-calculate-indent)) 'block)
3723;; (let ((verilog-tab-always-indent nil))
3724;; (verilog-indent-command)))
3725;; )
3726
3727(defun electric-verilog-tick ()
3728 "Insert back-tick, and indent to column 0 if this is a CPP directive."
3729 (interactive)
a3a8b002 3730 (verilog-insert-last-command-event)
6341f357 3731 (save-excursion
a3a8b002
DN
3732 (if (verilog-in-directive-p)
3733 (verilog-indent-line))))
6341f357
DN
3734
3735(defun electric-verilog-tab ()
3736 "Function called when TAB is pressed in Verilog mode."
3737 (interactive)
3738 ;; If verilog-tab-always-indent, indent the beginning of the line.
7cb1c4d7
DN
3739 (cond
3740 ;; The region is active, indent it.
3741 ((and (region-active-p)
3742 (not (eq (region-beginning) (region-end))))
3743 (indent-region (region-beginning) (region-end) nil))
3744 ((or verilog-tab-always-indent
3745 (save-excursion
3746 (skip-chars-backward " \t")
3747 (bolp)))
3748 (let* ((oldpnt (point))
3749 (boi-point
3750 (save-excursion
3751 (beginning-of-line)
3752 (skip-chars-forward " \t")
3753 (verilog-indent-line)
3754 (back-to-indentation)
3755 (point))))
3756 (if (< (point) boi-point)
3757 (back-to-indentation)
3758 (cond ((not verilog-tab-to-comment))
3759 ((not (eolp))
3760 (end-of-line))
3761 (t
3762 (indent-for-comment)
3763 (when (and (eolp) (= oldpnt (point)))
6341f357 3764 ; kill existing comment
7cb1c4d7
DN
3765 (beginning-of-line)
3766 (re-search-forward comment-start-skip oldpnt 'move)
3767 (goto-char (match-beginning 0))
3768 (skip-chars-backward " \t")
3769 (kill-region (point) oldpnt)))))))
3770 (t (progn (insert "\t")))))
a3a8b002 3771
6341f357
DN
3772\f
3773
3774;;
3775;; Interactive functions
3776;;
3777
3778(defun verilog-indent-buffer ()
3779 "Indent-region the entire buffer as Verilog code.
3780To call this from the command line, see \\[verilog-batch-indent]."
3781 (interactive)
3782 (verilog-mode)
3783 (indent-region (point-min) (point-max) nil))
3784
3785(defun verilog-insert-block ()
3786 "Insert Verilog begin ... end; block in the code with right indentation."
3787 (interactive)
3788 (verilog-indent-line)
3789 (insert "begin")
3790 (electric-verilog-terminate-line)
3791 (save-excursion
3792 (electric-verilog-terminate-line)
3793 (insert "end")
3794 (beginning-of-line)
3795 (verilog-indent-line)))
3796
3797(defun verilog-star-comment ()
3798 "Insert Verilog star comment at point."
3799 (interactive)
3800 (verilog-indent-line)
3801 (insert "/*")
3802 (save-excursion
3803 (newline)
3804 (insert " */"))
3805 (newline)
3806 (insert " * "))
3807
7ea26faf 3808(defun verilog-insert-1 (fmt max)
60618039 3809 "Use format string FMT to insert integers 0 to MAX - 1.
7ea26faf
DN
3810Inserts one integer per line, at the current column. Stops early
3811if it reaches the end of the buffer."
3812 (let ((col (current-column))
3813 (n 0))
3814 (save-excursion
3815 (while (< n max)
3816 (insert (format fmt n))
3817 (forward-line 1)
3818 ;; Note that this function does not bother to check for lines
3819 ;; shorter than col.
3820 (if (eobp)
3821 (setq n max)
3822 (setq n (1+ n))
3823 (move-to-column col))))))
3824
3825(defun verilog-insert-indices (max)
3826 "Insert a set of indices into a rectangle.
3827The upper left corner is defined by point. Indices begin with 0
3828and extend to the MAX - 1. If no prefix arg is given, the user
3829is prompted for a value. The indices are surrounded by square
3830brackets \[]. For example, the following code with the point
3831located after the first 'a' gives:
6341f357
DN
3832
3833 a = b a[ 0] = b
3834 a = b a[ 1] = b
3835 a = b a[ 2] = b
3836 a = b a[ 3] = b
3837 a = b ==> insert-indices ==> a[ 4] = b
3838 a = b a[ 5] = b
3839 a = b a[ 6] = b
3840 a = b a[ 7] = b
3841 a = b a[ 8] = b"
3842
60618039 3843 (interactive "NMAX: ")
7ea26faf 3844 (verilog-insert-1 "[%3d]" max))
6341f357 3845
7ea26faf 3846(defun verilog-generate-numbers (max)
6341f357
DN
3847 "Insert a set of generated numbers into a rectangle.
3848The upper left corner is defined by point. The numbers are padded to three
3849digits, starting with 000 and extending to (MAX - 1). If no prefix argument
7ea26faf 3850is supplied, then the user is prompted for the MAX number. Consider the
6341f357
DN
3851following code fragment:
3852
7ea26faf
DN
3853 buf buf buf buf000
3854 buf buf buf buf001
3855 buf buf buf buf002
3856 buf buf buf buf003
3857 buf buf ==> generate-numbers ==> buf buf004
3858 buf buf buf buf005
3859 buf buf buf buf006
3860 buf buf buf buf007
3861 buf buf buf buf008"
3862
60618039 3863 (interactive "NMAX: ")
7ea26faf 3864 (verilog-insert-1 "%3.3d" max))
6341f357
DN
3865
3866(defun verilog-mark-defun ()
37ea4b9b 3867 "Mark the current Verilog function (or procedure).
6341f357
DN
3868This puts the mark at the end, and point at the beginning."
3869 (interactive)
7d55bf04
DN
3870 (if (featurep 'xemacs)
3871 (progn
3872 (push-mark (point))
3873 (verilog-end-of-defun)
3874 (push-mark (point))
3875 (verilog-beg-of-defun)
3876 (if (fboundp 'zmacs-activate-region)
3877 (zmacs-activate-region)))
3878 (mark-defun)))
6341f357
DN
3879
3880(defun verilog-comment-region (start end)
3881 ; checkdoc-params: (start end)
3882 "Put the region into a Verilog comment.
3883The comments that are in this area are \"deformed\":
3884`*)' becomes `!(*' and `}' becomes `!{'.
3885These deformed comments are returned to normal if you use
3886\\[verilog-uncomment-region] to undo the commenting.
3887
3888The commented area starts with `verilog-exclude-str-start', and ends with
3889`verilog-exclude-str-end'. But if you change these variables,
3890\\[verilog-uncomment-region] won't recognize the comments."
3891 (interactive "r")
3892 (save-excursion
3893 ;; Insert start and endcomments
3894 (goto-char end)
3895 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
3896 (not (save-excursion (skip-chars-backward " \t") (bolp))))
3897 (forward-line 1)
3898 (beginning-of-line))
3899 (insert verilog-exclude-str-end)
3900 (setq end (point))
3901 (newline)
3902 (goto-char start)
3903 (beginning-of-line)
3904 (insert verilog-exclude-str-start)
3905 (newline)
3906 ;; Replace end-comments within commented area
3907 (goto-char end)
3908 (save-excursion
3909 (while (re-search-backward "\\*/" start t)
3910 (replace-match "*-/" t t)))
3911 (save-excursion
3912 (let ((s+1 (1+ start)))
3913 (while (re-search-backward "/\\*" s+1 t)
60618039 3914 (replace-match "/-*" t t))))))
6341f357
DN
3915
3916(defun verilog-uncomment-region ()
3917 "Uncomment a commented area; change deformed comments back to normal.
3918This command does nothing if the pointer is not in a commented
3919area. See also `verilog-comment-region'."
3920 (interactive)
3921 (save-excursion
3922 (let ((start (point))
3923 (end (point)))
3924 ;; Find the boundaries of the comment
3925 (save-excursion
3926 (setq start (progn (search-backward verilog-exclude-str-start nil t)
3927 (point)))
3928 (setq end (progn (search-forward verilog-exclude-str-end nil t)
3929 (point))))
3930 ;; Check if we're really inside a comment
3931 (if (or (equal start (point)) (<= end (point)))
3932 (message "Not standing within commented area.")
3933 (progn
3934 ;; Remove endcomment
3935 (goto-char end)
3936 (beginning-of-line)
3937 (let ((pos (point)))
3938 (end-of-line)
3939 (delete-region pos (1+ (point))))
3940 ;; Change comments back to normal
3941 (save-excursion
3942 (while (re-search-backward "\\*-/" start t)
3943 (replace-match "*/" t t)))
3944 (save-excursion
3945 (while (re-search-backward "/-\\*" start t)
3946 (replace-match "/*" t t)))
3947 ;; Remove start comment
3948 (goto-char start)
3949 (beginning-of-line)
3950 (let ((pos (point)))
3951 (end-of-line)
3952 (delete-region pos (1+ (point)))))))))
3953
3954(defun verilog-beg-of-defun ()
3955 "Move backward to the beginning of the current function or procedure."
3956 (interactive)
3957 (verilog-re-search-backward verilog-defun-re nil 'move))
3958
9489a450
MM
3959(defun verilog-beg-of-defun-quick ()
3960 "Move backward to the beginning of the current function or procedure.
3961Uses `verilog-scan' cache."
3962 (interactive)
3963 (verilog-re-search-backward-quick verilog-defun-re nil 'move))
3964
6341f357
DN
3965(defun verilog-end-of-defun ()
3966 "Move forward to the end of the current function or procedure."
3967 (interactive)
3968 (verilog-re-search-forward verilog-end-defun-re nil 'move))
3969
3970(defun verilog-get-beg-of-defun (&optional warn)
3971 (save-excursion
3972 (cond ((verilog-re-search-forward-quick verilog-defun-re nil t)
3973 (point))
3974 (t
3975 (error "%s: Can't find module beginning" (verilog-point-text))
3976 (point-max)))))
3977(defun verilog-get-end-of-defun (&optional warn)
3978 (save-excursion
3979 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
3980 (point))
3981 (t
3982 (error "%s: Can't find endmodule" (verilog-point-text))
3983 (point-max)))))
3984
3985(defun verilog-label-be (&optional arg)
3986 "Label matching begin ... end, fork ... join and case ... endcase statements.
3987With ARG, first kill any existing labels."
3988 (interactive)
3989 (let ((cnt 0)
3990 (oldpos (point))
3991 (b (progn
3992 (verilog-beg-of-defun)
3993 (point-marker)))
3994 (e (progn
3995 (verilog-end-of-defun)
60618039 3996 (point-marker))))
6341f357
DN
3997 (goto-char (marker-position b))
3998 (if (> (- e b) 200)
3999 (message "Relabeling module..."))
4000 (while (and
4001 (> (marker-position e) (point))
4002 (verilog-re-search-forward
4003 (concat
4004 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|\\(primitive\\)\\|\\(interface\\)\\|\\(package\\)\\|\\(case\\)\\)?\\>"
4005 "\\|\\(`endif\\)\\|\\(`else\\)")
4006 nil 'move))
4007 (goto-char (match-beginning 0))
4008 (let ((indent-str (verilog-indent-line)))
4009 (verilog-set-auto-endcomments indent-str 't)
4010 (end-of-line)
60618039 4011 (delete-horizontal-space))
6341f357
DN
4012 (setq cnt (1+ cnt))
4013 (if (= 9 (% cnt 10))
60618039 4014 (message "%d..." cnt)))
6341f357
DN
4015 (goto-char oldpos)
4016 (if (or
4017 (> (- e b) 200)
4018 (> cnt 20))
60618039 4019 (message "%d lines auto commented" cnt))))
6341f357
DN
4020
4021(defun verilog-beg-of-statement ()
4022 "Move backward to beginning of statement."
4023 (interactive)
4024 ;; Move back token by token until we see the end
4c36be58 4025 ;; of some earlier line.
a03c2342
WS
4026 (let (h)
4027 (while
4028 ;; If the current point does not begin a new
4029 ;; statement, as in the character ahead of us is a ';', or SOF
4030 ;; or the string after us unambiguously starts a statement,
4031 ;; or the token before us unambiguously ends a statement,
4032 ;; then move back a token and test again.
4033 (not (or
4034 ;; stop if beginning of buffer
4035 (bolp)
4036 ;; stop if we find a ;
4037 (= (preceding-char) ?\;)
4038 ;; stop if we see a named coverpoint
4039 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
4040 ;; keep going if we are in the middle of a word
4041 (not (or (looking-at "\\<") (forward-word -1)))
53964682 4042 ;; stop if we see an assertion (perhaps labeled)
a03c2342
WS
4043 (and
4044 (looking-at "\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
4045 (progn
4046 (setq h (point))
4047 (save-excursion
4048 (verilog-backward-token)
4049 (if (looking-at verilog-label-re)
4050 (setq h (point))))
4051 (goto-char h)))
4052 ;; stop if we see a complete reg, perhaps an extended one
4053 (and
4054 (looking-at verilog-complete-reg)
4055 (let* ((p (point)))
4056 (while (and (looking-at verilog-extended-complete-re)
4057 (progn (setq p (point))
4058 (verilog-backward-token)
4059 (/= p (point)))))
4060 (goto-char p)))
4061 ;; stop if we see a complete reg (previous found extended ones)
4062 (looking-at verilog-basic-complete-re)
4063 ;; stop if previous token is an ender
4064 (save-excursion
4065 (verilog-backward-token)
4066 (or
4067 (looking-at verilog-end-block-re)
4068 (looking-at verilog-preprocessor-re))))) ;; end of test
6341f357
DN
4069 (verilog-backward-syntactic-ws)
4070 (verilog-backward-token))
a03c2342
WS
4071 ;; Now point is where the previous line ended.
4072 (verilog-forward-syntactic-ws)))
6341f357
DN
4073
4074(defun verilog-beg-of-statement-1 ()
4075 "Move backward to beginning of statement."
4076 (interactive)
a3a8b002
DN
4077 (if (verilog-in-comment-p)
4078 (verilog-backward-syntactic-ws))
6341f357 4079 (let ((pt (point)))
a3a8b002
DN
4080 (catch 'done
4081 (while (not (looking-at verilog-complete-reg))
4082 (setq pt (point))
4083 (verilog-backward-syntactic-ws)
4084 (if (or (bolp)
a03c2342
WS
4085 (= (preceding-char) ?\;)
4086 (save-excursion
4087 (verilog-backward-token)
4088 (looking-at verilog-ends-re)))
a3a8b002
DN
4089 (progn
4090 (goto-char pt)
4091 (throw 'done t))
4092 (verilog-backward-token))))
4093 (verilog-forward-syntactic-ws)))
4094;
4095; (while (and
4096; (not (looking-at verilog-complete-reg))
4097; (not (bolp))
4098; (not (= (preceding-char) ?\;)))
4099; (verilog-backward-token)
4100; (verilog-backward-syntactic-ws)
4101; (setq pt (point)))
4102; (goto-char pt)
4103; ;(verilog-forward-syntactic-ws)
6341f357
DN
4104
4105(defun verilog-end-of-statement ()
4106 "Move forward to end of current statement."
4107 (interactive)
4108 (let ((nest 0) pos)
a3a8b002
DN
4109 (cond
4110 ((verilog-in-directive-p)
4111 (forward-line 1)
4112 (backward-char 1))
4113
4114 ((looking-at verilog-beg-block-re)
4115 (verilog-forward-sexp))
4116
4117 ((equal (char-after) ?\})
4118 (forward-char))
4119
4120 ;; Skip to end of statement
4121 ((condition-case nil
4122 (setq pos
4123 (catch 'found
4124 (while t
4125 (forward-sexp 1)
4126 (verilog-skip-forward-comment-or-string)
4127 (if (eolp)
4128 (forward-line 1))
4129 (cond ((looking-at "[ \t]*;")
4130 (skip-chars-forward "^;")
4131 (forward-char 1)
4132 (throw 'found (point)))
4133 ((save-excursion
4134 (forward-sexp -1)
4135 (looking-at verilog-beg-block-re))
4136 (goto-char (match-beginning 0))
4137 (throw 'found nil))
4138 ((looking-at "[ \t]*)")
4139 (throw 'found (point)))
4140 ((eobp)
4141 (throw 'found (point)))
4142 )))
4143
4144 )
4145 (error nil))
4146 (if (not pos)
4147 ;; Skip a whole block
4148 (catch 'found
4149 (while t
4150 (verilog-re-search-forward verilog-end-statement-re nil 'move)
4151 (setq nest (if (match-end 1)
4152 (1+ nest)
4153 (1- nest)))
4154 (cond ((eobp)
4155 (throw 'found (point)))
4156 ((= 0 nest)
4157 (throw 'found (verilog-end-of-statement))))))
4158 pos)))))
6341f357
DN
4159
4160(defun verilog-in-case-region-p ()
37ea4b9b
JB
4161 "Return true if in a case region.
4162More specifically, point @ in the line foo : @ begin"
6341f357
DN
4163 (interactive)
4164 (save-excursion
4165 (if (and
4166 (progn (verilog-forward-syntactic-ws)
4167 (looking-at "\\<begin\\>"))
4168 (progn (verilog-backward-syntactic-ws)
4169 (= (preceding-char) ?\:)))
4170 (catch 'found
4171 (let ((nest 1))
4172 (while t
4173 (verilog-re-search-backward
4174 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
4175 "\\(\\<endcase\\>\\)\\>")
4176 nil 'move)
4177 (cond
4178 ((match-end 3)
4179 (setq nest (1+ nest)))
4180 ((match-end 2)
4181 (if (= nest 1)
4182 (throw 'found 1))
4183 (setq nest (1- nest)))
4184 (t
60618039 4185 (throw 'found (= nest 0)))))))
6341f357 4186 nil)))
9489a450 4187
a03c2342 4188(defun verilog-backward-up-list (arg)
9489a450
MM
4189 "Like `backward-up-list', but deal with comments."
4190 (let ((parse-sexp-ignore-comments t))
4191 (backward-up-list arg)))
4192
4193(defun verilog-forward-sexp-cmt (arg)
4194 "Call `forward-sexp', inside comments."
4195 (let ((parse-sexp-ignore-comments nil))
4196 (forward-sexp arg)))
4197
4198(defun verilog-forward-sexp-ign-cmt (arg)
4199 "Call `forward-sexp', ignoring comments."
4200 (let ((parse-sexp-ignore-comments t))
4201 (forward-sexp arg)))
a03c2342 4202
6341f357 4203(defun verilog-in-struct-region-p ()
37ea4b9b
JB
4204 "Return true if in a struct region.
4205More specifically, in a list after a struct|union keyword."
6341f357
DN
4206 (interactive)
4207 (save-excursion
d63b01e1 4208 (let* ((state (verilog-syntax-ppss))
6341f357
DN
4209 (depth (nth 0 state)))
4210 (if depth
a03c2342 4211 (progn (verilog-backward-up-list depth)
6341f357 4212 (verilog-beg-of-statement)
60618039 4213 (looking-at "\\<typedef\\>?\\s-*\\<struct\\|union\\>"))))))
6341f357
DN
4214
4215(defun verilog-in-generate-region-p ()
37ea4b9b
JB
4216 "Return true if in a generate region.
4217More specifically, after a generate and before an endgenerate."
6341f357 4218 (interactive)
a3a8b002 4219 (let ((nest 1))
6341f357 4220 (save-excursion
a3a8b002
DN
4221 (catch 'done
4222 (while (and
4223 (/= nest 0)
4224 (verilog-re-search-backward
4225 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
4226 (cond
4227 ((match-end 1) ; module - we have crawled out
4228 (throw 'done 1))
4229 ((match-end 2) ; generate
4230 (setq nest (1- nest)))
4231 ((match-end 3) ; endgenerate
4232 (setq nest (1+ nest))))))))
6341f357
DN
4233 (= nest 0) )) ; return nest
4234
4235(defun verilog-in-fork-region-p ()
4236 "Return true if between a fork and join."
4237 (interactive)
4238 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
60618039 4239 (nest 1))
6341f357
DN
4240 (save-excursion
4241 (while (and
4242 (/= nest 0)
4243 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
4244 (cond
4245 ((match-end 1) ; fork
4246 (setq nest (1- nest)))
4247 ((match-end 2) ; join
60618039 4248 (setq nest (1+ nest)))))))
6341f357
DN
4249 (= nest 0) )) ; return nest
4250
4251(defun verilog-backward-case-item (lim)
4252 "Skip backward to nearest enclosing case item.
4253Limit search to point LIM."
4254 (interactive)
4255 (let ((str 'nil)
4256 (lim1
4257 (progn
4258 (save-excursion
4259 (verilog-re-search-backward verilog-endcomment-reason-re
4260 lim 'move)
4261 (point)))))
4262 ;; Try to find the real :
4263 (if (save-excursion (search-backward ":" lim1 t))
4264 (let ((colon 0)
4265 b e )
4266 (while
4267 (and
4268 (< colon 1)
4269 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
4270 lim1 'move))
4271 (cond
4272 ((match-end 1) ;; [
4273 (setq colon (1+ colon))
4274 (if (>= colon 0)
4275 (error "%s: unbalanced [" (verilog-point-text))))
4276 ((match-end 2) ;; ]
4277 (setq colon (1- colon)))
4278
4279 ((match-end 3) ;; :
60618039 4280 (setq colon (1+ colon)))))
6341f357
DN
4281 ;; Skip back to beginning of case item
4282 (skip-chars-backward "\t ")
4283 (verilog-skip-backward-comment-or-string)
4284 (setq e (point))
4285 (setq b
4286 (progn
4287 (if
4288 (verilog-re-search-backward
4289 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
4290 (progn
4291 (cond
4292 ((match-end 1)
4293 (goto-char (match-end 1))
4294 (verilog-forward-ws&directives)
4295 (if (looking-at "(")
4296 (progn
4297 (forward-sexp)
4298 (verilog-forward-ws&directives)))
4299 (point))
4300 (t
4301 (goto-char (match-end 0))
4302 (verilog-forward-ws&directives)
60618039
DN
4303 (point))))
4304 (error "Malformed case item"))))
6341f357
DN
4305 (setq str (buffer-substring b e))
4306 (if
4307 (setq e
4308 (string-match
4309 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4310 (setq str (concat (substring str 0 e) "...")))
4311 str)
4312 'nil)))
4313\f
4314
4315;;
4316;; Other functions
4317;;
4318
6ca0ff73 4319(defun verilog-kill-existing-comment ()
6341f357
DN
4320 "Kill auto comment on this line."
4321 (save-excursion
4322 (let* (
4323 (e (progn
4324 (end-of-line)
4325 (point)))
4326 (b (progn
4327 (beginning-of-line)
4328 (search-forward "//" e t))))
4329 (if b
4330 (delete-region (- b 2) e)))))
4331
4332(defconst verilog-directive-nest-re
4333 (concat "\\(`else\\>\\)\\|"
4334 "\\(`endif\\>\\)\\|"
4335 "\\(`if\\>\\)\\|"
4336 "\\(`ifdef\\>\\)\\|"
a03c2342
WS
4337 "\\(`ifndef\\>\\)\\|"
4338 "\\(`elsif\\>\\)"))
6341f357
DN
4339(defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
4340 "Add ending comment with given INDENT-STR.
4341With KILL-EXISTING-COMMENT, remove what was there before.
4342Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
4343Insert `// case expr ' if this line ends a case block.
4344Insert `// ifdef FOO ' if this line ends code conditional on FOO.
37ea4b9b
JB
4345Insert `// NAME ' if this line ends a function, task, module,
4346primitive or interface named NAME."
6341f357
DN
4347 (save-excursion
4348 (cond
4349 (; Comment close preprocessor directives
4350 (and
4351 (looking-at "\\(`endif\\)\\|\\(`else\\)")
4352 (or kill-existing-comment
4353 (not (save-excursion
4354 (end-of-line)
3ba6b2ee 4355 (search-backward "//" (point-at-bol) t)))))
6341f357
DN
4356 (let ((nest 1) b e
4357 m
60618039 4358 (else (if (match-end 2) "!" " ")))
6341f357
DN
4359 (end-of-line)
4360 (if kill-existing-comment
6ca0ff73 4361 (verilog-kill-existing-comment))
6341f357
DN
4362 (delete-horizontal-space)
4363 (save-excursion
4364 (backward-sexp 1)
4365 (while (and (/= nest 0)
4366 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
4367 (cond
4368 ((match-end 1) ; `else
4369 (if (= nest 1)
4370 (setq else "!")))
4371 ((match-end 2) ; `endif
4372 (setq nest (1+ nest)))
4373 ((match-end 3) ; `if
4374 (setq nest (1- nest)))
4375 ((match-end 4) ; `ifdef
4376 (setq nest (1- nest)))
4377 ((match-end 5) ; `ifndef
a03c2342
WS
4378 (setq nest (1- nest)))
4379 ((match-end 6) ; `elsif
4380 (if (= nest 1)
4381 (progn
4382 (setq else "!")
4383 (setq nest 0))))))
6341f357
DN
4384 (if (match-end 0)
4385 (setq
4386 m (buffer-substring
4387 (match-beginning 0)
4388 (match-end 0))
4389 b (progn
4390 (skip-chars-forward "^ \t")
4391 (verilog-forward-syntactic-ws)
4392 (point))
4393 e (progn
4394 (skip-chars-forward "a-zA-Z0-9_")
60618039 4395 (point)))))
6341f357
DN
4396 (if b
4397 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
4398 (insert (concat " // " else m " " (buffer-substring b e))))
4399 (progn
a03c2342 4400 (insert " // unmatched `else, `elsif or `endif")
60618039 4401 (ding 't)))))
6341f357
DN
4402
4403 (; Comment close case/class/function/task/module and named block
4404 (and (looking-at "\\<end")
4405 (or kill-existing-comment
4406 (not (save-excursion
4407 (end-of-line)
3ba6b2ee 4408 (search-backward "//" (point-at-bol) t)))))
6341f357
DN
4409 (let ((type (car indent-str)))
4410 (unless (eq type 'declaration)
4411 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ;; ignore named ends
4412 (if (looking-at verilog-end-block-ordered-re)
4413 (cond
4414 (;- This is a case block; search back for the start of this case
4415 (match-end 1) ;; of verilog-end-block-ordered-re
4416
4417 (let ((err 't)
4418 (str "UNMATCHED!!"))
4419 (save-excursion
4420 (verilog-leap-to-head)
4421 (cond
4422 ((looking-at "\\<randcase\\>")
4423 (setq str "randcase")
7ea26faf 4424 (setq err nil))
a3a8b002
DN
4425 ((looking-at "\\(\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
4426 (goto-char (match-end 0))
4427 (setq str (concat (match-string 0) " " (verilog-get-expr)))
4428 (setq err nil))
4429 ))
6341f357
DN
4430 (end-of-line)
4431 (if kill-existing-comment
6ca0ff73 4432 (verilog-kill-existing-comment))
6341f357
DN
4433 (delete-horizontal-space)
4434 (insert (concat " // " str ))
7ea26faf 4435 (if err (ding 't))))
6341f357
DN
4436
4437 (;- This is a begin..end block
4438 (match-end 2) ;; of verilog-end-block-ordered-re
4439 (let ((str " // UNMATCHED !!")
4440 (err 't)
4441 (here (point))
4442 there
60618039 4443 cntx)
6341f357
DN
4444 (save-excursion
4445 (verilog-leap-to-head)
4446 (setq there (point))
4447 (if (not (match-end 0))
4448 (progn
4449 (goto-char here)
4450 (end-of-line)
4451 (if kill-existing-comment
6ca0ff73 4452 (verilog-kill-existing-comment))
6341f357
DN
4453 (delete-horizontal-space)
4454 (insert str)
60618039 4455 (ding 't))
6341f357
DN
4456 (let ((lim
4457 (save-excursion (verilog-beg-of-defun) (point)))
60618039 4458 (here (point)))
6341f357
DN
4459 (cond
4460 (;-- handle named block differently
4461 (looking-at verilog-named-block-re)
4462 (search-forward ":")
4463 (setq there (point))
4464 (setq str (verilog-get-expr))
4465 (setq err nil)
4466 (setq str (concat " // block: " str )))
4467
4468 ((verilog-in-case-region-p) ;-- handle case item differently
4469 (goto-char here)
4470 (setq str (verilog-backward-case-item lim))
4471 (setq there (point))
4472 (setq err nil)
4473 (setq str (concat " // case: " str )))
4474
4475 (;- try to find "reason" for this begin
4476 (cond
4477 (;
4478 (eq here (progn
a03c2342
WS
4479 ;; (verilog-backward-token)
4480 (verilog-beg-of-statement)
6341f357
DN
4481 (point)))
4482 (setq err nil)
4483 (setq str ""))
4484 ((looking-at verilog-endcomment-reason-re)
4485 (setq there (match-end 0))
a3a8b002 4486 (setq cntx (concat (match-string 0) " "))
6341f357
DN
4487 (cond
4488 (;- begin
a3a8b002 4489 (match-end 1)
6341f357
DN
4490 (setq err nil)
4491 (save-excursion
4492 (if (and (verilog-continued-line)
4493 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
4494 (progn
4495 (goto-char (match-end 0))
4496 (setq there (point))
4497 (setq str
a3a8b002 4498 (concat " // " (match-string 0) " " (verilog-get-expr))))
6341f357
DN
4499 (setq str ""))))
4500
4501 (;- else
a3a8b002 4502 (match-end 2)
6341f357 4503 (let ((nest 0)
a03c2342 4504 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
6341f357
DN
4505 (catch 'skip
4506 (while (verilog-re-search-backward reg nil 'move)
4507 (cond
4508 ((match-end 1) ; begin
4509 (setq nest (1- nest)))
4510 ((match-end 2) ; end
4511 (setq nest (1+ nest)))
4512 ((match-end 3)
4513 (if (= 0 nest)
4514 (progn
4515 (goto-char (match-end 0))
4516 (setq there (point))
4517 (setq err nil)
4518 (setq str (verilog-get-expr))
4519 (setq str (concat " // else: !if" str ))
a03c2342
WS
4520 (throw 'skip 1))))
4521 ((match-end 4)
4522 (if (= 0 nest)
4523 (progn
4524 (goto-char (match-end 0))
4525 (setq there (point))
4526 (setq err nil)
4527 (setq str (verilog-get-expr))
4528 (setq str (concat " // else: !assert " str ))
60618039 4529 (throw 'skip 1)))))))))
6341f357 4530 (;- end else
a3a8b002 4531 (match-end 3)
6341f357
DN
4532 (goto-char there)
4533 (let ((nest 0)
a03c2342 4534 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
6341f357
DN
4535 (catch 'skip
4536 (while (verilog-re-search-backward reg nil 'move)
4537 (cond
4538 ((match-end 1) ; begin
4539 (setq nest (1- nest)))
4540 ((match-end 2) ; end
4541 (setq nest (1+ nest)))
4542 ((match-end 3)
4543 (if (= 0 nest)
4544 (progn
4545 (goto-char (match-end 0))
4546 (setq there (point))
4547 (setq err nil)
4548 (setq str (verilog-get-expr))
4549 (setq str (concat " // else: !if" str ))
a03c2342
WS
4550 (throw 'skip 1))))
4551 ((match-end 4)
4552 (if (= 0 nest)
4553 (progn
4554 (goto-char (match-end 0))
4555 (setq there (point))
4556 (setq err nil)
4557 (setq str (verilog-get-expr))
4558 (setq str (concat " // else: !assert " str ))
60618039 4559 (throw 'skip 1)))))))))
a03c2342 4560
a3a8b002
DN
4561 (; always_comb, always_ff, always_latch
4562 (or (match-end 4) (match-end 5) (match-end 6))
4563 (goto-char (match-end 0))
4564 (setq there (point))
4565 (setq err nil)
4566 (setq str (concat " // " cntx )))
6341f357
DN
4567
4568 (;- task/function/initial et cetera
4569 t
4570 (match-end 0)
4571 (goto-char (match-end 0))
4572 (setq there (point))
4573 (setq err nil)
a3a8b002 4574 (setq str (concat " // " cntx (verilog-get-expr))))
6341f357
DN
4575
4576 (;-- otherwise...
60618039 4577 (setq str " // auto-endcomment confused "))))
6341f357
DN
4578
4579 ((and
4580 (verilog-in-case-region-p) ;-- handle case item differently
4581 (progn
4582 (setq there (point))
4583 (goto-char here)
4584 (setq str (verilog-backward-case-item lim))))
4585 (setq err nil)
4586 (setq str (concat " // case: " str )))
4587
4588 ((verilog-in-fork-region-p)
4589 (setq err nil)
4590 (setq str " // fork branch" ))
4591
4592 ((looking-at "\\<end\\>")
4593 ;; HERE
4594 (forward-word 1)
4595 (verilog-forward-syntactic-ws)
4596 (setq err nil)
4597 (setq str (verilog-get-expr))
4598 (setq str (concat " // " cntx str )))
4599
4600 ))))
4601 (goto-char here)
4602 (end-of-line)
4603 (if kill-existing-comment
6ca0ff73 4604 (verilog-kill-existing-comment))
6341f357
DN
4605 (delete-horizontal-space)
4606 (if (or err
4607 (> (count-lines here there) verilog-minimum-comment-distance))
4608 (insert str))
4609 (if err (ding 't))
4610 ))))
4611 (;- this is endclass, which can be nested
4612 (match-end 11) ;; of verilog-end-block-ordered-re
4613 ;;(goto-char there)
4614 (let ((nest 0)
60618039
DN
4615 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
4616 string)
6341f357
DN
4617 (save-excursion
4618 (catch 'skip
4619 (while (verilog-re-search-backward reg nil 'move)
4620 (cond
4621 ((match-end 3) ; endclass
4622 (ding 't)
4623 (setq string "unmatched endclass")
4624 (throw 'skip 1))
4625
4626 ((match-end 2) ; endclass
4627 (setq nest (1+ nest)))
4628
4629 ((match-end 1) ; class
4630 (setq nest (1- nest))
4631 (if (< nest 0)
4632 (progn
4633 (goto-char (match-end 0))
4634 (let (b e)
4635 (setq b (progn
4636 (skip-chars-forward "^ \t")
4637 (verilog-forward-ws&directives)
4638 (point))
4639 e (progn
4640 (skip-chars-forward "a-zA-Z0-9_")
4641 (point)))
4642 (setq string (buffer-substring b e)))
4643 (throw 'skip 1))))
4644 ))))
4645 (end-of-line)
60618039 4646 (insert (concat " // " string ))))
6341f357
DN
4647
4648 (;- this is end{function,generate,task,module,primitive,table,generate}
4649 ;- which can not be nested.
4650 t
9c059794 4651 (let (string reg (name-re nil))
6341f357
DN
4652 (end-of-line)
4653 (if kill-existing-comment
4654 (save-match-data
6ca0ff73 4655 (verilog-kill-existing-comment)))
6341f357
DN
4656 (delete-horizontal-space)
4657 (backward-sexp)
4658 (cond
4659 ((match-end 5) ;; of verilog-end-block-ordered-re
4660 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
9c059794
DN
4661 (setq name-re "\\w+\\s-*(")
4662 )
6341f357
DN
4663 ((match-end 6) ;; of verilog-end-block-ordered-re
4664 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)"))
4665 ((match-end 7) ;; of verilog-end-block-ordered-re
4666 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
4667 ((match-end 8) ;; of verilog-end-block-ordered-re
4668 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4669 ((match-end 9) ;; of verilog-end-block-ordered-re
4670 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
4671 ((match-end 10) ;; of verilog-end-block-ordered-re
4672 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4673 ((match-end 11) ;; of verilog-end-block-ordered-re
4674 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4675 ((match-end 12) ;; of verilog-end-block-ordered-re
4676 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4677 ((match-end 13) ;; of verilog-end-block-ordered-re
4678 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4679 ((match-end 14) ;; of verilog-end-block-ordered-re
4680 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4681 ((match-end 15) ;; of verilog-end-block-ordered-re
4682 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
4683
60618039 4684 (t (error "Problem in verilog-set-auto-endcomments")))
6341f357
DN
4685 (let (b e)
4686 (save-excursion
4687 (verilog-re-search-backward reg nil 'move)
4688 (cond
4689 ((match-end 1)
4690 (setq b (progn
4691 (skip-chars-forward "^ \t")
4692 (verilog-forward-ws&directives)
a03c2342
WS
4693 (if (looking-at "static\\|automatic")
4694 (progn
4695 (goto-char (match-end 0))
4696 (verilog-forward-ws&directives)))
9c059794 4697 (if (and name-re (verilog-re-search-forward name-re nil 'move))
6341f357 4698 (progn
9c059794 4699 (goto-char (match-beginning 0))
60618039 4700 (verilog-forward-ws&directives)))
6341f357
DN
4701 (point))
4702 e (progn
4703 (skip-chars-forward "a-zA-Z0-9_")
4704 (point)))
4705 (setq string (buffer-substring b e)))
4706 (t
4707 (ding 't)
4708 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
4709 (end-of-line)
4710 (insert (concat " // " string )))
4711 ))))))))))
4712
4713(defun verilog-get-expr()
37ea4b9b 4714 "Grab expression at point, e.g, case ( a | b & (c ^d))."
6341f357
DN
4715 (let* ((b (progn
4716 (verilog-forward-syntactic-ws)
4717 (skip-chars-forward " \t")
4718 (point)))
4719 (e (let ((par 1))
4720 (cond
4721 ((looking-at "@")
4722 (forward-char 1)
4723 (verilog-forward-syntactic-ws)
4724 (if (looking-at "(")
4725 (progn
4726 (forward-char 1)
4727 (while (and (/= par 0)
4728 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4729 (cond
4730 ((match-end 1)
4731 (setq par (1+ par)))
4732 ((match-end 2)
4733 (setq par (1- par)))))))
4734 (point))
4735 ((looking-at "(")
4736 (forward-char 1)
4737 (while (and (/= par 0)
4738 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4739 (cond
4740 ((match-end 1)
4741 (setq par (1+ par)))
4742 ((match-end 2)
4743 (setq par (1- par)))))
4744 (point))
4745 ((looking-at "\\[")
4746 (forward-char 1)
4747 (while (and (/= par 0)
4748 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
4749 (cond
4750 ((match-end 1)
4751 (setq par (1+ par)))
4752 ((match-end 2)
4753 (setq par (1- par)))))
4754 (verilog-forward-syntactic-ws)
4755 (skip-chars-forward "^ \t\n\f")
4756 (point))
4757 ((looking-at "/[/\\*]")
4758 b)
4759 ('t
4760 (skip-chars-forward "^: \t\n\f")
60618039 4761 (point)))))
6341f357
DN
4762 (str (buffer-substring b e)))
4763 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4764 (setq str (concat (substring str 0 e) "...")))
4765 str))
4766
4767(defun verilog-expand-vector ()
4768 "Take a signal vector on the current line and expand it to multiple lines.
4769Useful for creating tri's and other expanded fields."
4770 (interactive)
4771 (verilog-expand-vector-internal "[" "]"))
4772
4773(defun verilog-expand-vector-internal (bra ket)
4774 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
4775 (save-excursion
4776 (forward-line 0)
4777 (let ((signal-string (buffer-substring (point)
4778 (progn
4779 (end-of-line) (point)))))
7ea26faf
DN
4780 (if (string-match
4781 (concat "\\(.*\\)"
4782 (regexp-quote bra)
4783 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
60618039
DN
4784 (regexp-quote ket)
4785 "\\(.*\\)$") signal-string)
6341f357 4786 (let* ((sig-head (match-string 1 signal-string))
7ea26faf 4787 (vec-start (string-to-number (match-string 2 signal-string)))
6341f357
DN
4788 (vec-end (if (= (match-beginning 3) (match-end 3))
4789 vec-start
7ea26faf
DN
4790 (string-to-number
4791 (substring signal-string (1+ (match-beginning 3))
4792 (match-end 3)))))
4793 (vec-range
4794 (if (= (match-beginning 4) (match-end 4))
4795 1
4796 (string-to-number
4797 (substring signal-string (+ 2 (match-beginning 4))
4798 (match-end 4)))))
6341f357
DN
4799 (sig-tail (match-string 5 signal-string))
4800 vec)
4801 ;; Decode vectors
4802 (setq vec nil)
4803 (if (< vec-range 0)
4804 (let ((tmp vec-start))
4805 (setq vec-start vec-end
4806 vec-end tmp
4807 vec-range (- vec-range))))
4808 (if (< vec-end vec-start)
4809 (while (<= vec-end vec-start)
4810 (setq vec (append vec (list vec-start)))
4811 (setq vec-start (- vec-start vec-range)))
4812 (while (<= vec-start vec-end)
4813 (setq vec (append vec (list vec-start)))
4814 (setq vec-start (+ vec-start vec-range))))
4815 ;;
4816 ;; Delete current line
4817 (delete-region (point) (progn (forward-line 0) (point)))
4818 ;;
4819 ;; Expand vector
4820 (while vec
7ea26faf
DN
4821 (insert (concat sig-head bra
4822 (int-to-string (car vec)) ket sig-tail "\n"))
6341f357
DN
4823 (setq vec (cdr vec)))
4824 (delete-char -1)
4825 ;;
4826 )))))
4827
4828(defun verilog-strip-comments ()
37ea4b9b 4829 "Strip all comments from the Verilog code."
6341f357
DN
4830 (interactive)
4831 (goto-char (point-min))
4832 (while (re-search-forward "//" nil t)
4833 (if (verilog-within-string)
4834 (re-search-forward "\"" nil t)
4835 (if (verilog-in-star-comment-p)
4836 (re-search-forward "\*/" nil t)
4837 (let ((bpt (- (point) 2)))
4838 (end-of-line)
4839 (delete-region bpt (point))))))
4840 ;;
4841 (goto-char (point-min))
4842 (while (re-search-forward "/\\*" nil t)
4843 (if (verilog-within-string)
4844 (re-search-forward "\"" nil t)
4845 (let ((bpt (- (point) 2)))
4846 (re-search-forward "\\*/")
4847 (delete-region bpt (point))))))
4848
4849(defun verilog-one-line ()
37ea4b9b 4850 "Convert structural Verilog instances to occupy one line."
6341f357
DN
4851 (interactive)
4852 (goto-char (point-min))
4853 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
4854 (replace-match "\\1 " nil nil)))
4855
4856(defun verilog-linter-name ()
4857 "Return name of linter, either surelint or verilint."
4858 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4859 compile-command))
4860 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4861 verilog-linter)))
4862 (cond ((equal compile-word1 "surelint") `surelint)
4863 ((equal compile-word1 "verilint") `verilint)
4864 ((equal lint-word1 "surelint") `surelint)
4865 ((equal lint-word1 "verilint") `verilint)
4866 (t `surelint)))) ;; back compatibility
4867
4868(defun verilog-lint-off ()
4869 "Convert a Verilog linter warning line into a disable statement.
4870For example:
4871 pci_bfm_null.v, line 46: Unused input: pci_rst_
4872becomes a comment for the appropriate tool.
4873
4874The first word of the `compile-command' or `verilog-linter'
37ea4b9b 4875variables is used to determine which product is being used.
6341f357
DN
4876
4877See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
4878 (interactive)
4879 (let ((linter (verilog-linter-name)))
4880 (cond ((equal linter `surelint)
4881 (verilog-surelint-off))
4882 ((equal linter `verilint)
4883 (verilog-verilint-off))
4884 (t (error "Linter name not set")))))
4885
7ea26faf 4886(defvar compilation-last-buffer)
a3a8b002 4887(defvar next-error-last-buffer)
7ea26faf 4888
6341f357
DN
4889(defun verilog-surelint-off ()
4890 "Convert a SureLint warning line into a disable statement.
4891Run from Verilog source window; assumes there is a *compile* buffer
4892with point set appropriately.
4893
4894For example:
4895 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
4896becomes:
4897 // surefire lint_line_off UDDONX"
4898 (interactive)
7ea26faf
DN
4899 (let ((buff (if (boundp 'next-error-last-buffer)
4900 next-error-last-buffer
4901 compilation-last-buffer)))
4902 (when (buffer-live-p buff)
7ea26faf
DN
4903 (save-excursion
4904 (switch-to-buffer buff)
4905 (beginning-of-line)
4906 (when
4907 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
4908 (let* ((code (match-string 2))
4909 (file (match-string 3))
4910 (line (match-string 4))
4911 (buffer (get-file-buffer file))
4912 dir filename)
4913 (unless buffer
4914 (progn
4915 (setq buffer
4916 (and (file-exists-p file)
4917 (find-file-noselect file)))
4918 (or buffer
4919 (let* ((pop-up-windows t))
4920 (let ((name (expand-file-name
4921 (read-file-name
4922 (format "Find this error in: (default %s) "
4923 file)
4924 dir file t))))
4925 (if (file-directory-p name)
4926 (setq name (expand-file-name filename name)))
4927 (setq buffer
4928 (and (file-exists-p name)
4929 (find-file-noselect name))))))))
4930 (switch-to-buffer buffer)
5f68c1b7
GM
4931 (goto-char (point-min))
4932 (forward-line (- (string-to-number line)))
7ea26faf
DN
4933 (end-of-line)
4934 (catch 'already
4935 (cond
4936 ((verilog-in-slash-comment-p)
4937 (re-search-backward "//")
4938 (cond
4939 ((looking-at "// surefire lint_off_line ")
4940 (goto-char (match-end 0))
3ba6b2ee 4941 (let ((lim (point-at-eol)))
7ea26faf
DN
4942 (if (re-search-forward code lim 'move)
4943 (throw 'already t)
4944 (insert (concat " " code)))))
4945 (t
4946 )))
4947 ((verilog-in-star-comment-p)
4948 (re-search-backward "/\*")
60618039 4949 (insert (format " // surefire lint_off_line %6s" code )))
7ea26faf
DN
4950 (t
4951 (insert (format " // surefire lint_off_line %6s" code ))
4952 )))))))))
6341f357
DN
4953
4954(defun verilog-verilint-off ()
4955 "Convert a Verilint warning line into a disable statement.
4956
4957For example:
4958 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
4959becomes:
4960 //Verilint 240 off // WARNING: Unused input"
4961 (interactive)
4962 (save-excursion
4963 (beginning-of-line)
4964 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
4965 (replace-match (format
4966 ;; %3s makes numbers 1-999 line up nicely
4967 "\\1//Verilint %3s off // WARNING: \\3"
4968 (match-string 2)))
4969 (beginning-of-line)
4970 (verilog-indent-line))))
4971
4972(defun verilog-auto-save-compile ()
4973 "Update automatics with \\[verilog-auto], save the buffer, and compile."
4974 (interactive)
4975 (verilog-auto) ; Always do it for safety
4976 (save-buffer)
4977 (compile compile-command))
4978
a03c2342
WS
4979(defun verilog-preprocess (&optional command filename)
4980 "Preprocess the buffer, similar to `compile', but leave output in Verilog-Mode.
4981Takes optional COMMAND or defaults to `verilog-preprocessor', and
4982FILENAME or defaults to `buffer-file-name`."
4983 (interactive
4984 (list
4985 (let ((default (verilog-expand-command verilog-preprocessor)))
4986 (set (make-local-variable `verilog-preprocessor)
4987 (read-from-minibuffer "Run Preprocessor (like this): "
4988 default nil nil
4989 'verilog-preprocess-history default)))))
4990 (unless command (setq command (verilog-expand-command verilog-preprocessor)))
8468f78b
WS
4991 (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
4992 (dir (file-name-directory (or filename buffer-file-name)))
a03c2342
WS
4993 (file (file-name-nondirectory (or filename buffer-file-name)))
4994 (cmd (concat "cd " dir "; " command " " file)))
4995 (with-output-to-temp-buffer "*Verilog-Preprocessed*"
8468f78b 4996 (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
a03c2342
WS
4997 (insert (concat "// " cmd "\n"))
4998 (shell-command cmd "*Verilog-Preprocessed*")
4999 (verilog-mode)
8468f78b
WS
5000 ;; Without this force, it takes a few idle seconds
5001 ;; to get the color, which is very jarring
5002 (when fontlocked (font-lock-fontify-buffer))))))
6341f357
DN
5003\f
5004
5005;;
5006;; Batch
5007;;
5008
5009(defmacro verilog-batch-error-wrapper (&rest body)
5010 "Execute BODY and add error prefix to any errors found.
5011This lets programs calling batch mode to easily extract error messages."
6edb5716
DN
5012 `(condition-case err
5013 (progn ,@body)
5014 (error
5015 (error "%%Error: %s%s" (error-message-string err)
d63b01e1 5016 (if (featurep 'xemacs) "\n" ""))))) ;; XEmacs forgets to add a newline
6341f357 5017
9489a450
MM
5018(defun verilog-batch-execute-func (funref &optional no-save)
5019 "Internal processing of a batch command, running FUNREF on all command arguments.
5020Save the result unless optional NO-SAVE is t."
6341f357 5021 (verilog-batch-error-wrapper
14862301
SM
5022 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
5023 ;; However, this function is called only when Emacs is being used as
5024 ;; a standalone language instead of as an editor, so we'll live.
5025 ;;
6341f357
DN
5026 ;; General globals needed
5027 (setq make-backup-files nil)
5028 (setq-default make-backup-files nil)
5029 (setq enable-local-variables t)
5030 (setq enable-local-eval t)
5031 ;; Make sure any sub-files we read get proper mode
14acf2f5 5032 (setq-default major-mode 'verilog-mode)
6341f357 5033 ;; Ditto files already read in
7ea26faf 5034 (mapc (lambda (buf)
60618039 5035 (when (buffer-file-name buf)
14acf2f5 5036 (with-current-buffer buf
60618039
DN
5037 (verilog-mode))))
5038 (buffer-list))
6341f357 5039 ;; Process the files
4f91a816 5040 (mapcar (lambda (buf)
9489a450
MM
5041 (when (buffer-file-name buf)
5042 (save-excursion
5043 (if (not (file-exists-p (buffer-file-name buf)))
5044 (error
5045 (concat "File not found: " (buffer-file-name buf))))
5046 (message (concat "Processing " (buffer-file-name buf)))
5047 (set-buffer buf)
5048 (funcall funref)
5049 (unless no-save (save-buffer)))))
6341f357
DN
5050 (buffer-list))))
5051
5052(defun verilog-batch-auto ()
5053 "For use with --batch, perform automatic expansions as a stand-alone tool.
37ea4b9b 5054This sets up the appropriate Verilog mode environment, updates automatics
6341f357
DN
5055with \\[verilog-auto] on all command-line files, and saves the buffers.
5056For proper results, multiple filenames need to be passed on the command
5057line in bottom-up order."
5058 (unless noninteractive
5059 (error "Use verilog-batch-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5060 (verilog-batch-execute-func `verilog-auto))
5061
5062(defun verilog-batch-delete-auto ()
5063 "For use with --batch, perform automatic deletion as a stand-alone tool.
37ea4b9b 5064This sets up the appropriate Verilog mode environment, deletes automatics
6341f357
DN
5065with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
5066 (unless noninteractive
5067 (error "Use verilog-batch-delete-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5068 (verilog-batch-execute-func `verilog-delete-auto))
5069
9489a450
MM
5070(defun verilog-batch-diff-auto ()
5071 "For use with --batch, perform automatic differences as a stand-alone tool.
5072This sets up the appropriate Verilog mode environment, expand automatics
5073with \\[verilog-diff-auto] on all command-line files, and reports an error
5074if any differences are observed. This is appropriate for adding to regressions
5075to insure automatics are always properly maintained."
5076 (unless noninteractive
5077 (error "Use verilog-batch-diff-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5078 (verilog-batch-execute-func `verilog-diff-auto t))
5079
6341f357
DN
5080(defun verilog-batch-inject-auto ()
5081 "For use with --batch, perform automatic injection as a stand-alone tool.
37ea4b9b 5082This sets up the appropriate Verilog mode environment, injects new automatics
6341f357
DN
5083with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
5084For proper results, multiple filenames need to be passed on the command
5085line in bottom-up order."
5086 (unless noninteractive
5087 (error "Use verilog-batch-inject-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5088 (verilog-batch-execute-func `verilog-inject-auto))
5089
5090(defun verilog-batch-indent ()
9489a450 5091 "For use with --batch, reindent an a entire file as a stand-alone tool.
37ea4b9b 5092This sets up the appropriate Verilog mode environment, calls
6341f357
DN
5093\\[verilog-indent-buffer] on all command-line files, and saves the buffers."
5094 (unless noninteractive
5095 (error "Use verilog-batch-indent only with --batch")) ;; Otherwise we'd mess up buffer modes
5096 (verilog-batch-execute-func `verilog-indent-buffer))
5097\f
5098
5099;;
5100;; Indentation
5101;;
5102(defconst verilog-indent-alist
5103 '((block . (+ ind verilog-indent-level))
5104 (case . (+ ind verilog-case-indent))
5105 (cparenexp . (+ ind verilog-indent-level))
5106 (cexp . (+ ind verilog-cexp-indent))
5107 (defun . verilog-indent-level-module)
5108 (declaration . verilog-indent-level-declaration)
5109 (directive . (verilog-calculate-indent-directive))
5110 (tf . verilog-indent-level)
5111 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
5112 (statement . ind)
5113 (cpp . 0)
5114 (comment . (verilog-comment-indent))
5115 (unknown . 3)
5116 (string . 0)))
5117
5118(defun verilog-continued-line-1 (lim)
5119 "Return true if this is a continued line.
5120Set point to where line starts. Limit search to point LIM."
5121 (let ((continued 't))
5122 (if (eq 0 (forward-line -1))
5123 (progn
5124 (end-of-line)
5125 (verilog-backward-ws&directives lim)
5126 (if (bobp)
5127 (setq continued nil)
5128 (setq continued (verilog-backward-token))))
5129 (setq continued nil))
5130 continued))
5131
5132(defun verilog-calculate-indent ()
5133 "Calculate the indent of the current Verilog line.
5134Examine previous lines. Once a line is found that is definitive as to the
37ea4b9b
JB
5135type of the current line, return that lines' indent level and its type.
5136Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
6341f357
DN
5137 (save-excursion
5138 (let* ((starting_position (point))
5139 (par 0)
5140 (begin (looking-at "[ \t]*begin\\>"))
5141 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
5142 (type (catch 'nesting
5143 ;; Keep working backwards until we can figure out
5144 ;; what type of statement this is.
5145 ;; Basically we need to figure out
5146 ;; 1) if this is a continuation of the previous line;
5147 ;; 2) are we in a block scope (begin..end)
5148
5149 ;; if we are in a comment, done.
5150 (if (verilog-in-star-comment-p)
5151 (throw 'nesting 'comment))
5152
5153 ;; if we have a directive, done.
a3a8b002
DN
5154 (if (save-excursion (beginning-of-line)
5155 (and (looking-at verilog-directive-re-1)
9489a450 5156 (not (or (looking-at "[ \t]*`[ou]vm_")
86a4c7ac 5157 (looking-at "[ \t]*`vmm_")))))
6341f357 5158 (throw 'nesting 'directive))
a3a8b002
DN
5159 ;; indent structs as if there were module level
5160 (if (verilog-in-struct-p)
5161 (throw 'nesting 'block))
6341f357 5162
9489a450
MM
5163 ;; if we are in a parenthesized list, and the user likes to indent these, return.
5164 ;; unless we are in the newfangled coverpoint or constraint blocks
5165 (if (and
a3a8b002
DN
5166 verilog-indent-lists
5167 (verilog-in-paren)
5168 (not (verilog-in-coverage-p))
5169 )
9489a450 5170 (progn (setq par 1)
a3a8b002 5171 (throw 'nesting 'block)))
6341f357 5172
9489a450
MM
5173 ;; See if we are continuing a previous line
5174 (while t
5175 ;; trap out if we crawl off the top of the buffer
5176 (if (bobp) (throw 'nesting 'cpp))
5177
5178 (if (verilog-continued-line-1 lim)
5179 (let ((sp (point)))
5180 (if (and
5181 (not (looking-at verilog-complete-reg))
5182 (verilog-continued-line-1 lim))
5183 (progn (goto-char sp)
5184 (throw 'nesting 'cexp))
5185
5186 (goto-char sp))
5187
5188 (if (and begin
5189 (not verilog-indent-begin-after-if)
5190 (looking-at verilog-no-indent-begin-re))
5191 (progn
5192 (beginning-of-line)
5193 (skip-chars-forward " \t")
5194 (throw 'nesting 'statement))
5195 (progn
5196 (throw 'nesting 'cexp))))
5197 ;; not a continued line
5198 (goto-char starting_position))
5199
5200 (if (looking-at "\\<else\\>")
5201 ;; search back for governing if, striding across begin..end pairs
5202 ;; appropriately
5203 (let ((elsec 1))
5204 (while (verilog-re-search-backward verilog-ends-re nil 'move)
5205 (cond
5206 ((match-end 1) ; else, we're in deep
5207 (setq elsec (1+ elsec)))
5208 ((match-end 2) ; if
5209 (setq elsec (1- elsec))
5210 (if (= 0 elsec)
5211 (if verilog-align-ifelse
5212 (throw 'nesting 'statement)
5213 (progn ;; back up to first word on this line
5214 (beginning-of-line)
5215 (verilog-forward-syntactic-ws)
5216 (throw 'nesting 'statement)))))
5217 ((match-end 3) ; assert block
5218 (setq elsec (1- elsec))
5219 (verilog-beg-of-statement) ;; doesn't get to beginning
5220 (if (looking-at verilog-property-re)
5221 (throw 'nesting 'statement) ; We don't need an endproperty for these
5222 (throw 'nesting 'block) ;We still need a endproperty
5223 ))
5224 (t ; endblock
5225 ; try to leap back to matching outward block by striding across
5226 ; indent level changing tokens then immediately
5227 ; previous line governs indentation.
5228 (let (( reg) (nest 1))
5229 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
5230 (cond
5231 ((match-end 4) ; end
5232 ;; Search back for matching begin
5233 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
5234 ((match-end 5) ; endcase
5235 ;; Search back for matching case
5236 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5237 ((match-end 6) ; endfunction
5238 ;; Search back for matching function
5239 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5240 ((match-end 7) ; endtask
5241 ;; Search back for matching task
5242 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
5243 ((match-end 8) ; endspecify
5244 ;; Search back for matching specify
5245 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5246 ((match-end 9) ; endtable
5247 ;; Search back for matching table
5248 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5249 ((match-end 10) ; endgenerate
5250 ;; Search back for matching generate
5251 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5252 ((match-end 11) ; joins
5253 ;; Search back for matching fork
5254 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
5255 ((match-end 12) ; class
5256 ;; Search back for matching class
5257 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5258 ((match-end 13) ; covergroup
5259 ;; Search back for matching covergroup
5260 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
5261 (catch 'skip
5262 (while (verilog-re-search-backward reg nil 'move)
6341f357 5263 (cond
9489a450
MM
5264 ((match-end 1) ; begin
5265 (setq nest (1- nest))
5266 (if (= 0 nest)
5267 (throw 'skip 1)))
5268 ((match-end 2) ; end
5269 (setq nest (1+ nest)))))
5270 )))))))
5271 (throw 'nesting (verilog-calc-1)))
5272 );; catch nesting
6341f357
DN
5273 );; type
5274 )
5275 ;; Return type of block and indent level.
5276 (if (not type)
5277 (setq type 'cpp))
5278 (if (> par 0) ; Unclosed Parenthesis
5279 (list 'cparenexp par)
5280 (cond
5281 ((eq type 'case)
5282 (list type (verilog-case-indent-level)))
5283 ((eq type 'statement)
5284 (list type (current-column)))
5285 ((eq type 'defun)
5286 (list type 0))
5287 (t
60618039
DN
5288 (list type (verilog-current-indent-level))))))))
5289
6341f357
DN
5290(defun verilog-wai ()
5291 "Show matching nesting block for debugging."
5292 (interactive)
5293 (save-excursion
4c5e69c6
DN
5294 (let* ((type (verilog-calc-1))
5295 depth)
5296 ;; Return type of block and indent level.
5297 (if (not type)
5298 (setq type 'cpp))
5299 (if (and
5300 verilog-indent-lists
a3a8b002
DN
5301 (not(or (verilog-in-coverage-p)
5302 (verilog-in-struct-p)))
4c5e69c6
DN
5303 (verilog-in-paren))
5304 (setq depth 1)
5305 (cond
5306 ((eq type 'case)
5307 (setq depth (verilog-case-indent-level)))
5308 ((eq type 'statement)
5309 (setq depth (current-column)))
5310 ((eq type 'defun)
5311 (setq depth 0))
5312 (t
5313 (setq depth (verilog-current-indent-level)))))
5314 (message "You are at nesting %s depth %d" type depth))))
6341f357
DN
5315
5316(defun verilog-calc-1 ()
5317 (catch 'nesting
a3a8b002
DN
5318 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)")))
5319 (while (verilog-re-search-backward re nil 'move)
5320 (catch 'continue
5321 (cond
5322 ((equal (char-after) ?\{)
5323 (if (verilog-at-constraint-p)
5324 (throw 'nesting 'block)))
6341f357 5325
a3a8b002
DN
5326 ((equal (char-after) ?\})
5327 (let ((there (verilog-at-close-constraint-p)))
86a4c7ac 5328 (if there ;; we are at the } that closes a constraint. Find the { that opens it
a3a8b002
DN
5329 (progn
5330 (forward-char 1)
5331 (backward-list 1)
5332 (verilog-beg-of-statement)))))
6341f357 5333
a3a8b002
DN
5334 ((looking-at verilog-beg-block-re-ordered)
5335 (cond
5336 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
5337 (let ((here (point)))
5338 (verilog-beg-of-statement)
5339 (if (looking-at verilog-extended-case-re)
5340 (throw 'nesting 'case)
5341 (goto-char here)))
5342 (throw 'nesting 'case))
5343
5344 ((match-end 4) ; *sigh* could be "disable fork"
5345 (let ((here (point)))
5346 (verilog-beg-of-statement)
9489a450 5347 (if (looking-at verilog-disable-fork-re)
fd9ea9d3 5348 t ; this is a normal statement
a3a8b002
DN
5349 (progn ; or is fork, starts a new block
5350 (goto-char here)
5351 (throw 'nesting 'block)))))
5352
a03c2342
WS
5353 ((match-end 27) ; *sigh* might be a clocking declaration
5354 (let ((here (point)))
5355 (if (verilog-in-paren)
5356 t ; this is a normal statement
5357 (progn ; or is fork, starts a new block
5358 (goto-char here)
5359 (throw 'nesting 'block)))))
a3a8b002
DN
5360
5361 ;; need to consider typedef struct here...
5362 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
6341f357
DN
5363 ; *sigh* These words have an optional prefix:
5364 ; extern {virtual|protected}? function a();
6341f357
DN
5365 ; typedef class foo;
5366 ; and we don't want to confuse this with
5367 ; function a();
5368 ; property
5369 ; ...
5370 ; endfunction
a3a8b002
DN
5371 (verilog-beg-of-statement)
5372 (if (looking-at verilog-beg-block-re-ordered)
5373 (throw 'nesting 'block)
5374 (throw 'nesting 'defun)))
7cb1c4d7 5375
a3a8b002
DN
5376 ((looking-at "\\<property\\>")
5377 ; *sigh*
7cb1c4d7 5378 ; {assert|assume|cover} property (); are complete
14862301 5379 ; and could also be labeled: - foo: assert property
7cb1c4d7
DN
5380 ; but
5381 ; property ID () ... needs end_property
a3a8b002 5382 (verilog-beg-of-statement)
a03c2342 5383 (if (looking-at verilog-property-re)
14862301 5384 (throw 'continue 'statement) ; We don't need an endproperty for these
a3a8b002
DN
5385 (throw 'nesting 'block) ;We still need a endproperty
5386 ))
6341f357 5387
a3a8b002 5388 (t (throw 'nesting 'block))))
6341f357 5389
a3a8b002
DN
5390 ((looking-at verilog-end-block-re)
5391 (verilog-leap-to-head)
5392 (if (verilog-in-case-region-p)
5393 (progn
5394 (verilog-leap-to-case-head)
5395 (if (looking-at verilog-extended-case-re)
5396 (throw 'nesting 'case)))))
6341f357 5397
a3a8b002
DN
5398 ((looking-at verilog-defun-level-re)
5399 (if (looking-at verilog-defun-level-generate-only-re)
5400 (if (verilog-in-generate-region-p)
14862301 5401 (throw 'continue 'foo) ; always block in a generate - keep looking
a3a8b002
DN
5402 (throw 'nesting 'defun))
5403 (throw 'nesting 'defun)))
5404
5405 ((looking-at verilog-cpp-level-re)
5406 (throw 'nesting 'cpp))
5407
5408 ((bobp)
5409 (throw 'nesting 'cpp)))))
5410
5411 (throw 'nesting 'cpp))))
6341f357
DN
5412
5413(defun verilog-calculate-indent-directive ()
5414 "Return indentation level for directive.
5415For speed, the searcher looks at the last directive, not the indent
5416of the appropriate enclosing block."
5417 (let ((base -1) ;; Indent of the line that determines our indentation
60618039 5418 (ind 0)) ;; Relative offset caused by other directives (like `endif on same line as `else)
6341f357
DN
5419 ;; Start at current location, scan back for another directive
5420
5421 (save-excursion
5422 (beginning-of-line)
5423 (while (and (< base 0)
5424 (verilog-re-search-backward verilog-directive-re nil t))
5425 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
60618039 5426 (setq base (current-indentation))))
6341f357
DN
5427 (cond ((and (looking-at verilog-directive-end) (< base 0)) ;; Only matters when not at BOL
5428 (setq ind (- ind verilog-indent-level-directive)))
5429 ((and (looking-at verilog-directive-middle) (>= base 0)) ;; Only matters when at BOL
5430 (setq ind (+ ind verilog-indent-level-directive)))
5431 ((looking-at verilog-directive-begin)
5432 (setq ind (+ ind verilog-indent-level-directive)))))
5433 ;; Adjust indent to starting indent of critical line
5434 (setq ind (max 0 (+ ind base))))
5435
5436 (save-excursion
5437 (beginning-of-line)
5438 (skip-chars-forward " \t")
5439 (cond ((or (looking-at verilog-directive-middle)
5440 (looking-at verilog-directive-end))
5441 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
5442 ind))
5443
5444(defun verilog-leap-to-case-head ()
5445 (let ((nest 1))
5446 (while (/= 0 nest)
a3a8b002
DN
5447 (verilog-re-search-backward
5448 (concat
5449 "\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
5450 "\\|\\(\\<endcase\\>\\)" )
5451 nil 'move)
6341f357
DN
5452 (cond
5453 ((match-end 1)
a3a8b002
DN
5454 (let ((here (point)))
5455 (verilog-beg-of-statement)
5456 (unless (looking-at verilog-extended-case-re)
5457 (goto-char here)))
6341f357 5458 (setq nest (1- nest)))
a3a8b002 5459 ((match-end 3)
6341f357
DN
5460 (setq nest (1+ nest)))
5461 ((bobp)
5462 (ding 't)
5463 (setq nest 0))))))
5464
5465(defun verilog-leap-to-head ()
37ea4b9b
JB
5466 "Move point to the head of this block.
5467Jump from end to matching begin, from endcase to matching case, and so on."
6341f357
DN
5468 (let ((reg nil)
5469 snest
4c5e69c6 5470 (nesting 'yes)
6341f357
DN
5471 (nest 1))
5472 (cond
5473 ((looking-at "\\<end\\>")
5474 ;; 1: Search back for matching begin
5475 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
5476 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
4c5e69c6 5477 ((looking-at "\\<endtask\\>")
a3a8b002 5478 ;; 2: Search back for matching task
4c5e69c6
DN
5479 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
5480 (setq nesting 'no))
6341f357 5481 ((looking-at "\\<endcase\\>")
a3a8b002
DN
5482 (catch 'nesting
5483 (verilog-leap-to-case-head) )
5484 (setq reg nil) ; to force skip
5485 )
5486
6341f357 5487 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
a3a8b002 5488 ;; 4: Search back for matching fork
6341f357
DN
5489 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5490 ((looking-at "\\<endclass\\>")
a3a8b002 5491 ;; 5: Search back for matching class
6341f357
DN
5492 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5493 ((looking-at "\\<endtable\\>")
a3a8b002 5494 ;; 6: Search back for matching table
6341f357
DN
5495 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5496 ((looking-at "\\<endspecify\\>")
a3a8b002 5497 ;; 7: Search back for matching specify
6341f357
DN
5498 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5499 ((looking-at "\\<endfunction\\>")
a3a8b002
DN
5500 ;; 8: Search back for matching function
5501 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
5502 (setq nesting 'no))
5503 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
6341f357
DN
5504 ((looking-at "\\<endgenerate\\>")
5505 ;; 8: Search back for matching generate
5506 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
6341f357
DN
5507 ((looking-at "\\<endgroup\\>")
5508 ;; 10: Search back for matching covergroup
5509 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
5510 ((looking-at "\\<endproperty\\>")
5511 ;; 11: Search back for matching property
5512 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
9489a450
MM
5513 ((looking-at verilog-uvm-end-re)
5514 ;; 12: Search back for matching sequence
5515 (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
a3a8b002
DN
5516 ((looking-at verilog-ovm-end-re)
5517 ;; 12: Search back for matching sequence
5518 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
86a4c7ac
DN
5519 ((looking-at verilog-vmm-end-re)
5520 ;; 12: Search back for matching sequence
5521 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
6341f357
DN
5522 ((looking-at "\\<endinterface\\>")
5523 ;; 12: Search back for matching interface
5524 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
5525 ((looking-at "\\<endsequence\\>")
5526 ;; 12: Search back for matching sequence
5527 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
5528 ((looking-at "\\<endclocking\\>")
5529 ;; 12: Search back for matching clocking
60618039 5530 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
6341f357
DN
5531 (if reg
5532 (catch 'skip
4c5e69c6
DN
5533 (if (eq nesting 'yes)
5534 (let (sreg)
5535 (while (verilog-re-search-backward reg nil 'move)
5536 (cond
5537 ((match-end 1) ; begin
0e5c8aed
DN
5538 (if (looking-at "fork")
5539 (let ((here (point)))
5540 (verilog-beg-of-statement)
5541 (unless (looking-at verilog-disable-fork-re)
5542 (goto-char here)
5543 (setq nest (1- nest))))
5544 (setq nest (1- nest)))
4c5e69c6
DN
5545 (if (= 0 nest)
5546 ;; Now previous line describes syntax
5547 (throw 'skip 1))
5548 (if (and snest
5549 (= snest nest))
5550 (setq reg sreg)))
5551 ((match-end 2) ; end
5552 (setq nest (1+ nest)))
5553 ((match-end 3)
5554 ;; endcase, jump to case
5555 (setq snest nest)
5556 (setq nest (1+ nest))
5557 (setq sreg reg)
5558 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5559 ((match-end 4)
5560 ;; join, jump to fork
5561 (setq snest nest)
5562 (setq nest (1+ nest))
5563 (setq sreg reg)
5564 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5565 )))
5566 ;no nesting
5567 (if (and
a3a8b002 5568 (verilog-re-search-backward reg nil 'move)
4c5e69c6
DN
5569 (match-end 1)) ; task -> could be virtual and/or protected
5570 (progn
5571 (verilog-beg-of-statement)
5572 (throw 'skip 1))
5573 (throw 'skip 1)))))))
6341f357
DN
5574
5575(defun verilog-continued-line ()
5576 "Return true if this is a continued line.
37ea4b9b 5577Set point to where line starts."
6341f357
DN
5578 (let ((continued 't))
5579 (if (eq 0 (forward-line -1))
5580 (progn
5581 (end-of-line)
5582 (verilog-backward-ws&directives)
5583 (if (bobp)
5584 (setq continued nil)
5585 (while (and continued
5586 (save-excursion
5587 (skip-chars-backward " \t")
5588 (not (bolp))))
60618039 5589 (setq continued (verilog-backward-token)))))
6341f357
DN
5590 (setq continued nil))
5591 continued))
5592
5593(defun verilog-backward-token ()
0b381c7e 5594 "Step backward token, returning true if this is a continued line."
6341f357
DN
5595 (interactive)
5596 (verilog-backward-syntactic-ws)
5597 (cond
5598 ((bolp)
5599 nil)
5600 (;-- Anything ending in a ; is complete
5601 (= (preceding-char) ?\;)
5602 nil)
5603 (; If a "}" is prefixed by a ";", then this is a complete statement
5604 ; i.e.: constraint foo { a = b; }
5605 (= (preceding-char) ?\})
5606 (progn
5607 (backward-char)
a3a8b002 5608 (not(verilog-at-close-constraint-p))))
6341f357
DN
5609 (;-- constraint foo { a = b }
5610 ; is a complete statement. *sigh*
5611 (= (preceding-char) ?\{)
5612 (progn
5613 (backward-char)
60618039 5614 (not (verilog-at-constraint-p))))
a3a8b002
DN
5615 (;" string "
5616 (= (preceding-char) ?\")
5617 (backward-char)
5618 (verilog-skip-backward-comment-or-string)
5619 nil)
5620
5621 (; [3:4]
5622 (= (preceding-char) ?\])
5623 (backward-char)
5624 (verilog-backward-open-bracket)
5625 t)
5626
6341f357
DN
5627 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
5628 ; also could be simply '@(foo)'
5629 ; or foo u1 #(a=8)
5630 ; (b, ... which ISN'T complete
5631 ;;;; Do we need this???
5632 (= (preceding-char) ?\))
5633 (progn
5634 (backward-char)
a03c2342 5635 (verilog-backward-up-list 1)
6341f357
DN
5636 (verilog-backward-syntactic-ws)
5637 (let ((back (point)))
5638 (forward-word -1)
5639 (cond
a3a8b002 5640 ;;XX
6341f357
DN
5641 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
5642 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
9489a450
MM
5643 ((looking-at verilog-uvm-statement-re)
5644 nil)
5645 ((looking-at verilog-uvm-begin-re)
5646 t)
5647 ((looking-at verilog-uvm-end-re)
5648 t)
a3a8b002
DN
5649 ((looking-at verilog-ovm-statement-re)
5650 nil)
5651 ((looking-at verilog-ovm-begin-re)
5652 t)
5653 ((looking-at verilog-ovm-end-re)
5654 t)
86a4c7ac
DN
5655 ;; JBA find VMM macros
5656 ((looking-at verilog-vmm-statement-re)
5657 nil )
5658 ((looking-at verilog-vmm-begin-re)
5659 t)
5660 ((looking-at verilog-vmm-end-re)
5661 nil)
5662 ;; JBA trying to catch macro lines with no ; at end
5663 ((looking-at "\\<`")
5664 nil)
6341f357
DN
5665 (t
5666 (goto-char back)
5667 (cond
5668 ((= (preceding-char) ?\@)
5669 (backward-char)
5670 (save-excursion
5671 (verilog-backward-token)
5672 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
5673 ((= (preceding-char) ?\#)
60618039
DN
5674 (backward-char))
5675 (t t)))))))
6341f357
DN
5676
5677 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
5678 t
5679 (forward-word -1)
0e5c8aed
DN
5680 (while (= (preceding-char) ?\_)
5681 (forward-word -1))
6341f357
DN
5682 (cond
5683 ((looking-at "\\<else\\>")
5684 t)
2a9eb3f5
DN
5685 ((looking-at verilog-behavioral-block-beg-re)
5686 t)
6341f357
DN
5687 ((looking-at verilog-indent-re)
5688 nil)
5689 (t
5690 (let
5691 ((back (point)))
5692 (verilog-backward-syntactic-ws)
5693 (cond
5694 ((= (preceding-char) ?\:)
5695 (backward-char)
5696 (verilog-backward-syntactic-ws)
5697 (backward-sexp)
5698 (if (looking-at verilog-nameable-item-re )
5699 nil
60618039 5700 t))
6341f357
DN
5701 ((= (preceding-char) ?\#)
5702 (backward-char)
5703 t)
5704 ((= (preceding-char) ?\`)
5705 (backward-char)
5706 t)
5707
5708 (t
5709 (goto-char back)
60618039 5710 t))))))))
6341f357 5711
fd9ea9d3 5712(defun verilog-backward-syntactic-ws ()
9489a450 5713 "Move backwards putting point after first non-whitespace non-comment."
fd9ea9d3
WS
5714 (verilog-skip-backward-comments)
5715 (forward-comment (- (buffer-size))))
6341f357 5716
9489a450
MM
5717(defun verilog-backward-syntactic-ws-quick ()
5718 "As with `verilog-backward-syntactic-ws' but uses `verilog-scan' cache."
5719 (while (cond ((bobp)
5720 nil) ; Done
5721 ((> (skip-syntax-backward " ") 0)
5722 t)
5723 ((eq (preceding-char) ?\n) ;; \n's terminate // so aren't space syntax
5724 (forward-char -1)
5725 t)
5726 ((or (verilog-inside-comment-or-string-p (1- (point)))
5727 (verilog-inside-comment-or-string-p (point)))
5728 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
5729 t))))
5730
fd9ea9d3
WS
5731(defun verilog-forward-syntactic-ws ()
5732 (verilog-skip-forward-comment-p)
5733 (forward-comment (buffer-size)))
6341f357
DN
5734
5735(defun verilog-backward-ws&directives (&optional bound)
5736 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
5737Optional BOUND limits search."
5738 (save-restriction
5739 (let* ((bound (or bound (point-min)))
5740 (here bound)
5741 (p nil) )
5742 (if (< bound (point))
5743 (progn
d63b01e1 5744 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5745 (cond
5746 ((nth 7 state) ;; in // comment
5747 (verilog-re-search-backward "//" nil 'move)
5748 (skip-chars-backward "/"))
5749 ((nth 4 state) ;; in /* */ comment
5750 (verilog-re-search-backward "/\*" nil 'move))))
5751 (narrow-to-region bound (point))
5752 (while (/= here (point))
5753 (setq here (point))
5754 (verilog-skip-backward-comments)
5755 (setq p
5756 (save-excursion
5757 (beginning-of-line)
5758 (cond
a3a8b002
DN
5759 ((and verilog-highlight-translate-off
5760 (verilog-within-translate-off))
6341f357
DN
5761 (verilog-back-to-start-translate-off (point-min)))
5762 ((looking-at verilog-directive-re-1)
5763 (point))
5764 (t
5765 nil))))
60618039 5766 (if p (goto-char p))))))))
6341f357
DN
5767
5768(defun verilog-forward-ws&directives (&optional bound)
5769 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
5770Optional BOUND limits search."
5771 (save-restriction
5772 (let* ((bound (or bound (point-max)))
5773 (here bound)
60618039 5774 jump)
6341f357
DN
5775 (if (> bound (point))
5776 (progn
d63b01e1 5777 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5778 (cond
5779 ((nth 7 state) ;; in // comment
a03c2342
WS
5780 (end-of-line)
5781 (forward-char 1)
5782 (skip-chars-forward " \t\n\f")
5783 )
6341f357 5784 ((nth 4 state) ;; in /* */ comment
a03c2342 5785 (verilog-re-search-forward "\*\/\\s-*" nil 'move))))
6341f357
DN
5786 (narrow-to-region (point) bound)
5787 (while (/= here (point))
5788 (setq here (point)
5789 jump nil)
5790 (forward-comment (buffer-size))
a03c2342
WS
5791 (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ;; Attribute
5792 (goto-char (match-end 0)))
6341f357
DN
5793 (save-excursion
5794 (beginning-of-line)
5795 (if (looking-at verilog-directive-re-1)
5796 (setq jump t)))
5797 (if jump
60618039 5798 (beginning-of-line 2))))))))
6341f357
DN
5799
5800(defun verilog-in-comment-p ()
5801 "Return true if in a star or // comment."
d63b01e1 5802 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5803 (or (nth 4 state) (nth 7 state))))
5804
5805(defun verilog-in-star-comment-p ()
5806 "Return true if in a star comment."
d63b01e1 5807 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5808 (and
5809 (nth 4 state) ; t if in a comment of style a // or b /**/
5810 (not
5811 (nth 7 state) ; t if in a comment of style b /**/
5812 ))))
5813
5814(defun verilog-in-slash-comment-p ()
5815 "Return true if in a slash comment."
d63b01e1 5816 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5817 (nth 7 state)))
5818
5819(defun verilog-in-comment-or-string-p ()
5820 "Return true if in a string or comment."
d63b01e1 5821 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5822 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
5823
a03c2342
WS
5824(defun verilog-in-attribute-p ()
5825 "Return true if point is in an attribute (* [] attribute *)."
9489a450
MM
5826 (save-match-data
5827 (save-excursion
5828 (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
5829 (numberp (match-beginning 1)))))
5830
5831(defun verilog-in-parameter-p ()
5832 "Return true if point is in a parameter assignment #( p1=1, p2=5)."
5833 (save-match-data
5834 (save-excursion
5835 (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
5836 (numberp (match-beginning 1)))))
a03c2342 5837
6341f357
DN
5838(defun verilog-in-escaped-name-p ()
5839 "Return true if in an escaped name."
5840 (save-excursion
5841 (backward-char)
5842 (skip-chars-backward "^ \t\n\f")
5843 (if (equal (char-after (point) ) ?\\ )
5844 t
5845 nil)))
a3a8b002 5846(defun verilog-in-directive-p ()
a03c2342 5847 "Return true if in a directive."
a3a8b002
DN
5848 (save-excursion
5849 (beginning-of-line)
5850 (looking-at verilog-directive-re-1)))
6341f357 5851
9489a450
MM
5852(defun verilog-in-parenthesis-p ()
5853 "Return true if in a ( ) expression (but not { } or [ ])."
5854 (save-match-data
5855 (save-excursion
5856 (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
5857 (numberp (match-beginning 1)))))
5858
6341f357 5859(defun verilog-in-paren ()
9489a450
MM
5860 "Return true if in a parenthetical expression.
5861May cache result using `verilog-syntax-ppss'."
d63b01e1 5862 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5863 (> (nth 0 state) 0 )))
5864
9489a450
MM
5865(defun verilog-in-paren-quick ()
5866 "Return true if in a parenthetical expression.
5867Always starts from point-min, to allow inserts with hooks disabled."
5868 ;; The -quick refers to its use alongside the other -quick functions,
5869 ;; not that it's likely to be faster than verilog-in-paren.
5870 (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
5871 (> (nth 0 state) 0 )))
5872
a3a8b002
DN
5873(defun verilog-in-struct-p ()
5874 "Return true if in a struct declaration."
5875 (interactive)
5876 (save-excursion
5877 (if (verilog-in-paren)
5878 (progn
a03c2342 5879 (verilog-backward-up-list 1)
a3a8b002
DN
5880 (verilog-at-struct-p)
5881 )
5882 nil)))
5883
5884(defun verilog-in-coverage-p ()
6341f357
DN
5885 "Return true if in a constraint or coverpoint expression."
5886 (interactive)
5887 (save-excursion
5888 (if (verilog-in-paren)
5889 (progn
a03c2342 5890 (verilog-backward-up-list 1)
6341f357
DN
5891 (verilog-at-constraint-p)
5892 )
5893 nil)))
5894(defun verilog-at-close-constraint-p ()
5895 "If at the } that closes a constraint or covergroup, return true."
5896 (if (and
5897 (equal (char-after) ?\})
5898 (verilog-in-paren))
5899
5900 (save-excursion
5901 (verilog-backward-ws&directives)
5902 (if (equal (char-before) ?\;)
5903 (point)
5904 nil))))
5905
5906(defun verilog-at-constraint-p ()
5907 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
5908 (if (save-excursion
5909 (and
5910 (equal (char-after) ?\{)
5911 (forward-list)
5912 (progn (backward-char 1)
5913 (verilog-backward-ws&directives)
60618039 5914 (equal (char-before) ?\;))))
6341f357
DN
5915 ;; maybe
5916 (verilog-re-search-backward "\\<constraint\\|coverpoint\\|cross\\>" nil 'move)
5917 ;; not
60618039 5918 nil))
6341f357 5919
a3a8b002
DN
5920(defun verilog-at-struct-p ()
5921 "If at the { of a struct, return true, moving point to struct."
5922 (save-excursion
5923 (if (and (equal (char-after) ?\{)
5924 (verilog-backward-token))
0e5c8aed 5925 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
a3a8b002
DN
5926 nil)))
5927
6341f357
DN
5928(defun verilog-parenthesis-depth ()
5929 "Return non zero if in parenthetical-expression."
d63b01e1 5930 (save-excursion (nth 1 (verilog-syntax-ppss))))
6341f357
DN
5931
5932
5933(defun verilog-skip-forward-comment-or-string ()
5934 "Return true if in a string or comment."
d63b01e1 5935 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5936 (cond
5937 ((nth 3 state) ;Inside string
495ab0d5 5938 (search-forward "\"")
6341f357
DN
5939 t)
5940 ((nth 7 state) ;Inside // comment
5941 (forward-line 1)
5942 t)
5943 ((nth 4 state) ;Inside any comment (hence /**/)
5944 (search-forward "*/"))
5945 (t
5946 nil))))
5947
5948(defun verilog-skip-backward-comment-or-string ()
5949 "Return true if in a string or comment."
d63b01e1 5950 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5951 (cond
5952 ((nth 3 state) ;Inside string
5953 (search-backward "\"")
5954 t)
5955 ((nth 7 state) ;Inside // comment
5956 (search-backward "//")
5957 (skip-chars-backward "/")
5958 t)
5959 ((nth 4 state) ;Inside /* */ comment
5960 (search-backward "/*")
5961 t)
5962 (t
5963 nil))))
5964
5965(defun verilog-skip-backward-comments ()
5966 "Return true if a comment was skipped."
5967 (let ((more t))
5968 (while more
5969 (setq more
d63b01e1 5970 (let ((state (save-excursion (verilog-syntax-ppss))))
6341f357
DN
5971 (cond
5972 ((nth 7 state) ;Inside // comment
5973 (search-backward "//")
5974 (skip-chars-backward "/")
5975 (skip-chars-backward " \t\n\f")
5976 t)
5977 ((nth 4 state) ;Inside /* */ comment
5978 (search-backward "/*")
5979 (skip-chars-backward " \t\n\f")
5980 t)
5981 ((and (not (bobp))
5982 (= (char-before) ?\/)
60618039 5983 (= (char-before (1- (point))) ?\*))
6341f357 5984 (goto-char (- (point) 2))
a03c2342
WS
5985 t) ;; Let nth 4 state handle the rest
5986 ((and (not (bobp))
5987 (= (char-before) ?\))
5988 (= (char-before (1- (point))) ?\*))
5989 (goto-char (- (point) 2))
5990 (if (search-backward "(*" nil t)
5991 (progn
5992 (skip-chars-backward " \t\n\f")
5993 t)
5994 (progn
5995 (goto-char (+ (point) 2))
5996 nil)))
6341f357 5997 (t
a03c2342 5998 (/= (skip-chars-backward " \t\n\f") 0))))))))
6341f357
DN
5999
6000(defun verilog-skip-forward-comment-p ()
6001 "If in comment, move to end and return true."
a03c2342
WS
6002 (let* (h
6003 (state (save-excursion (verilog-syntax-ppss)))
6004 (skip (cond
6005 ((nth 3 state) ;Inside string
6006 t)
6007 ((nth 7 state) ;Inside // comment
6008 (end-of-line)
6009 (forward-char 1)
6010 t)
6011 ((nth 4 state) ;Inside /* comment
6012 (search-forward "*/")
6013 t)
6014 ((verilog-in-attribute-p) ;Inside (* attribute
6015 (search-forward "*)" nil t)
6016 t)
6017 (t nil))))
6018 (skip-chars-forward " \t\n\f")
6019 (while
6020 (cond
6021 ((looking-at "\\/\\*")
6022 (progn
6023 (setq h (point))
6024 (goto-char (match-end 0))
6025 (if (search-forward "*/" nil t)
6026 (progn
6027 (skip-chars-forward " \t\n\f")
6028 (setq skip 't))
6029 (progn
6030 (goto-char h)
6031 nil))))
6032 ((looking-at "(\\*")
6033 (progn
6034 (setq h (point))
6035 (goto-char (match-end 0))
6036 (if (search-forward "*)" nil t)
6037 (progn
6038 (skip-chars-forward " \t\n\f")
6039 (setq skip 't))
6040 (progn
6041 (goto-char h)
6042 nil))))
6043 (t nil)))
6044 skip))
6341f357
DN
6045
6046(defun verilog-indent-line-relative ()
6047 "Cheap version of indent line.
6048Only look at a few lines to determine indent level."
6049 (interactive)
6050 (let ((indent-str)
6051 (sp (point)))
6052 (if (looking-at "^[ \t]*$")
6053 (cond ;- A blank line; No need to be too smart.
6054 ((bobp)
6055 (setq indent-str (list 'cpp 0)))
6056 ((verilog-continued-line)
6057 (let ((sp1 (point)))
6058 (if (verilog-continued-line)
7ea26faf
DN
6059 (progn
6060 (goto-char sp)
60618039
DN
6061 (setq indent-str
6062 (list 'statement (verilog-current-indent-level))))
6341f357
DN
6063 (goto-char sp1)
6064 (setq indent-str (list 'block (verilog-current-indent-level)))))
6065 (goto-char sp))
6066 ((goto-char sp)
6067 (setq indent-str (verilog-calculate-indent))))
6068 (progn (skip-chars-forward " \t")
6069 (setq indent-str (verilog-calculate-indent))))
6070 (verilog-do-indent indent-str)))
6071
6072(defun verilog-indent-line ()
6073 "Indent for special part of code."
6074 (verilog-do-indent (verilog-calculate-indent)))
6075
6076(defun verilog-do-indent (indent-str)
6077 (let ((type (car indent-str))
6078 (ind (car (cdr indent-str))))
6079 (cond
6080 (; handle continued exp
6081 (eq type 'cexp)
6082 (let ((here (point)))
6083 (verilog-backward-syntactic-ws)
6084 (cond
6085 ((or
6086 (= (preceding-char) ?\,)
6087 (= (preceding-char) ?\])
6088 (save-excursion
6089 (verilog-beg-of-statement-1)
6090 (looking-at verilog-declaration-re)))
6091 (let* ( fst
6092 (val
6093 (save-excursion
6094 (backward-char 1)
6095 (verilog-beg-of-statement-1)
6096 (setq fst (point))
6097 (if (looking-at verilog-declaration-re)
6098 (progn ;; we have multiple words
6099 (goto-char (match-end 0))
6100 (skip-chars-forward " \t")
6101 (cond
6102 ((and verilog-indent-declaration-macros
6103 (= (following-char) ?\`))
6104 (progn
6105 (forward-char 1)
6106 (forward-word 1)
6107 (skip-chars-forward " \t")))
6108 ((= (following-char) ?\[)
6109 (progn
6110 (forward-char 1)
a03c2342 6111 (verilog-backward-up-list -1)
60618039 6112 (skip-chars-forward " \t"))))
6341f357
DN
6113 (current-column))
6114 (progn
6115 (goto-char fst)
60618039 6116 (+ (current-column) verilog-cexp-indent))))))
6341f357 6117 (goto-char here)
9489a450
MM
6118 (indent-line-to val)
6119 (if (and (not verilog-indent-lists)
6120 (verilog-in-paren))
6121 (verilog-pretty-declarations))
6122 ))
6341f357
DN
6123 ((= (preceding-char) ?\) )
6124 (goto-char here)
6125 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6126 (indent-line-to val)))
6127 (t
6128 (goto-char here)
6129 (let ((val))
6130 (verilog-beg-of-statement-1)
6131 (if (and (< (point) here)
6132 (verilog-re-search-forward "=[ \\t]*" here 'move))
6133 (setq val (current-column))
6134 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
6135 (goto-char here)
60618039 6136 (indent-line-to val))))))
6341f357
DN
6137
6138 (; handle inside parenthetical expressions
6139 (eq type 'cparenexp)
a03c2342
WS
6140 (let* ( here
6141 (val (save-excursion
6142 (verilog-backward-up-list 1)
6143 (forward-char 1)
6144 (if verilog-indent-lists
6145 (skip-chars-forward " \t")
6146 (verilog-forward-syntactic-ws))
6147 (setq here (point))
6148 (current-column)))
6149
6150 (decl (save-excursion
6151 (goto-char here)
6152 (verilog-forward-syntactic-ws)
6153 (setq here (point))
6154 (looking-at verilog-declaration-re))))
6155 (indent-line-to val)
6156 (if decl
6157 (verilog-pretty-declarations))))
a3a8b002 6158
6341f357
DN
6159 (;-- Handle the ends
6160 (or
6161 (looking-at verilog-end-block-re )
6162 (verilog-at-close-constraint-p))
6163 (let ((val (if (eq type 'statement)
6164 (- ind verilog-indent-level)
6165 ind)))
6166 (indent-line-to val)))
6167
6168 (;-- Case -- maybe line 'em up
6169 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
6170 (progn
6171 (cond
6172 ((looking-at "\\<endcase\\>")
6173 (indent-line-to ind))
6174 (t
6175 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6176 (indent-line-to val))))))
6177
6178 (;-- defun
6179 (and (eq type 'defun)
6180 (looking-at verilog-zero-indent-re))
6181 (indent-line-to 0))
6182
6183 (;-- declaration
6184 (and (or
6185 (eq type 'defun)
6186 (eq type 'block))
6187 (looking-at verilog-declaration-re))
6188 (verilog-indent-declaration ind))
6189
6190 (;-- Everything else
6191 t
6192 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
60618039
DN
6193 (indent-line-to val))))
6194
6341f357
DN
6195 (if (looking-at "[ \t]+$")
6196 (skip-chars-forward " \t"))
6197 indent-str ; Return indent data
6198 ))
6199
6200(defun verilog-current-indent-level ()
37ea4b9b 6201 "Return the indent-level of the current statement."
6341f357
DN
6202 (save-excursion
6203 (let (par-pos)
6204 (beginning-of-line)
6205 (setq par-pos (verilog-parenthesis-depth))
6206 (while par-pos
6207 (goto-char par-pos)
6208 (beginning-of-line)
6209 (setq par-pos (verilog-parenthesis-depth)))
6210 (skip-chars-forward " \t")
6211 (current-column))))
6212
6213(defun verilog-case-indent-level ()
37ea4b9b 6214 "Return the indent-level of the current statement.
6341f357
DN
6215Do not count named blocks or case-statements."
6216 (save-excursion
6217 (skip-chars-forward " \t")
6218 (cond
6219 ((looking-at verilog-named-block-re)
6220 (current-column))
a3a8b002 6221 ((and (not (looking-at verilog-extended-case-re))
6341f357
DN
6222 (looking-at "^[^:;]+[ \t]*:"))
6223 (verilog-re-search-forward ":" nil t)
6224 (skip-chars-forward " \t")
6225 (current-column))
6226 (t
6227 (current-column)))))
6228
6229(defun verilog-indent-comment ()
6230 "Indent current line as comment."
6231 (let* ((stcol
6232 (cond
6233 ((verilog-in-star-comment-p)
6234 (save-excursion
6235 (re-search-backward "/\\*" nil t)
6236 (1+(current-column))))
6237 (comment-column
6238 comment-column )
6239 (t
6240 (save-excursion
6241 (re-search-backward "//" nil t)
60618039 6242 (current-column))))))
6341f357
DN
6243 (indent-line-to stcol)
6244 stcol))
6245
6246(defun verilog-more-comment ()
6247 "Make more comment lines like the previous."
6248 (let* ((star 0)
6249 (stcol
6250 (cond
6251 ((verilog-in-star-comment-p)
6252 (save-excursion
6253 (setq star 1)
6254 (re-search-backward "/\\*" nil t)
6255 (1+(current-column))))
6256 (comment-column
6257 comment-column )
6258 (t
6259 (save-excursion
6260 (re-search-backward "//" nil t)
60618039 6261 (current-column))))))
6341f357
DN
6262 (progn
6263 (indent-to stcol)
6264 (if (and star
6265 (save-excursion
6266 (forward-line -1)
6267 (skip-chars-forward " \t")
6268 (looking-at "\*")))
6269 (insert "* ")))))
6270
6271(defun verilog-comment-indent (&optional arg)
6272 "Return the column number the line should be indented to.
6273ARG is ignored, for `comment-indent-function' compatibility."
6274 (cond
6275 ((verilog-in-star-comment-p)
6276 (save-excursion
6277 (re-search-backward "/\\*" nil t)
6278 (1+(current-column))))
6279 ( comment-column
6280 comment-column )
6281 (t
6282 (save-excursion
6283 (re-search-backward "//" nil t)
6284 (current-column)))))
6285
6286;;
6287
7e2a6000 6288(defun verilog-pretty-declarations (&optional quiet)
a3a8b002
DN
6289 "Line up declarations around point.
6290Be verbose about progress unless optional QUIET set."
6341f357 6291 (interactive)
fd9ea9d3
WS
6292 (let* ((m1 (make-marker))
6293 (e (point))
6294 el
6295 r
6296 (here (point))
6297 ind
6298 start
6299 startpos
6300 end
6301 endpos
6302 base-ind
6303 )
6304 (save-excursion
6305 (if (progn
6306; (verilog-beg-of-statement-1)
6307 (beginning-of-line)
a03c2342 6308 (verilog-forward-syntactic-ws)
fd9ea9d3 6309 (and (not (verilog-in-directive-p)) ;; could have `define input foo
a3a8b002 6310 (looking-at verilog-declaration-re)))
fd9ea9d3 6311 (progn
a03c2342
WS
6312 (if (verilog-parenthesis-depth)
6313 ;; in an argument list or parameter block
6314 (setq el (verilog-backward-up-list -1)
fd9ea9d3
WS
6315 start (progn
6316 (goto-char e)
a03c2342 6317 (verilog-backward-up-list 1)
fd9ea9d3
WS
6318 (forward-line) ;; ignore ( input foo,
6319 (verilog-re-search-forward verilog-declaration-re el 'move)
6320 (goto-char (match-beginning 0))
6321 (skip-chars-backward " \t")
6322 (point))
6323 startpos (set-marker (make-marker) start)
6324 end (progn
6325 (goto-char start)
a03c2342 6326 (verilog-backward-up-list -1)
fd9ea9d3
WS
6327 (forward-char -1)
6328 (verilog-backward-syntactic-ws)
6329 (point))
6330 endpos (set-marker (make-marker) end)
6331 base-ind (progn
6332 (goto-char start)
a03c2342
WS
6333 (forward-char 1)
6334 (skip-chars-forward " \t")
6335 (current-column))
fd9ea9d3
WS
6336 )
6337 ;; in a declaration block (not in argument list)
a03c2342 6338 (setq
fd9ea9d3
WS
6339 start (progn
6340 (verilog-beg-of-statement-1)
6341 (while (and (looking-at verilog-declaration-re)
6342 (not (bobp)))
6343 (skip-chars-backward " \t")
6344 (setq e (point))
6345 (beginning-of-line)
6346 (verilog-backward-syntactic-ws)
6347 (backward-char)
6348 (verilog-beg-of-statement-1))
6349 e)
6350 startpos (set-marker (make-marker) start)
6351 end (progn
6352 (goto-char here)
6353 (verilog-end-of-statement)
6354 (setq e (point)) ;Might be on last line
6355 (verilog-forward-syntactic-ws)
6356 (while (looking-at verilog-declaration-re)
fd9ea9d3
WS
6357 (verilog-end-of-statement)
6358 (setq e (point))
6359 (verilog-forward-syntactic-ws))
6360 e)
6361 endpos (set-marker (make-marker) end)
6362 base-ind (progn
6363 (goto-char start)
6364 (verilog-do-indent (verilog-calculate-indent))
6365 (verilog-forward-ws&directives)
6366 (current-column))))
6367 ;; OK, start and end are set
6368 (goto-char (marker-position startpos))
6369 (if (and (not quiet)
6370 (> (- end start) 100))
6371 (message "Lining up declarations..(please stand by)"))
6372 ;; Get the beginning of line indent first
6373 (while (progn (setq e (marker-position endpos))
6374 (< (point) e))
6375 (cond
a03c2342 6376 ((save-excursion (skip-chars-backward " \t")
fd9ea9d3
WS
6377 (bolp))
6378 (verilog-forward-ws&directives)
6379 (indent-line-to base-ind)
6380 (verilog-forward-ws&directives)
6381 (if (< (point) e)
6382 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6383 (t
6384 (just-one-space)
6385 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6386 ;;(forward-line)
6387 )
6388 ;; Now find biggest prefix
6389 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
6390 ;; Now indent each line.
6391 (goto-char (marker-position startpos))
6392 (while (progn (setq e (marker-position endpos))
6393 (setq r (- e (point)))
6394 (> r 0))
6395 (setq e (point))
6396 (unless quiet (message "%d" r))
a03c2342 6397 ;;(verilog-do-indent (verilog-calculate-indent)))
fd9ea9d3
WS
6398 (verilog-forward-ws&directives)
6399 (cond
6400 ((or (and verilog-indent-declaration-macros
6401 (looking-at verilog-declaration-re-2-macro))
6402 (looking-at verilog-declaration-re-2-no-macro))
6403 (let ((p (match-end 0)))
6404 (set-marker m1 p)
6405 (if (verilog-re-search-forward "[[#`]" p 'move)
6406 (progn
6407 (forward-char -1)
6408 (just-one-space)
6409 (goto-char (marker-position m1))
6410 (just-one-space)
6411 (indent-to ind))
6412 (progn
6413 (just-one-space)
6414 (indent-to ind)))))
6415 ((verilog-continued-line-1 (marker-position startpos))
6416 (goto-char e)
6417 (indent-line-to ind))
6418 ((verilog-in-struct-p)
6419 ;; could have a declaration of a user defined item
6420 (goto-char e)
6421 (verilog-end-of-statement))
6422 (t ; Must be comment or white space
6423 (goto-char e)
6424 (verilog-forward-ws&directives)
6425 (forward-line -1)))
6426 (forward-line 1))
6427 (unless quiet (message "")))))))
6341f357 6428
7e2a6000 6429(defun verilog-pretty-expr (&optional quiet myre)
9489a450
MM
6430 "Line up expressions around point, optionally QUIET with regexp MYRE ignored."
6431 (interactive)
6432 (if (not (verilog-in-comment-or-string-p))
6433 (save-excursion
6434 (let ((rexp (concat "^\\s-*" verilog-complete-reg)))
6435 (beginning-of-line)
6436 (if (and (not (looking-at rexp ))
6437 (looking-at verilog-assignment-operation-re)
6438 (save-excursion
6439 (goto-char (match-end 2))
6440 (and (not (verilog-in-attribute-p))
6441 (not (verilog-in-parameter-p))
6442 (not (verilog-in-comment-or-string-p)))))
6443 (let* ((here (point))
6444 (e) (r)
6445 (start
6446 (progn
6447 (beginning-of-line)
6448 (setq e (point))
6449 (verilog-backward-syntactic-ws)
6450 (beginning-of-line)
6451 (while (and (not (looking-at rexp ))
6452 (looking-at verilog-assignment-operation-re)
6453 (not (bobp))
6454 )
6455 (setq e (point))
6456 (verilog-backward-syntactic-ws)
6457 (beginning-of-line)
6458 ) ;Ack, need to grok `define
6459 e))
6460 (end
6461 (progn
6462 (goto-char here)
6463 (end-of-line)
6464 (setq e (point)) ;Might be on last line
6465 (verilog-forward-syntactic-ws)
6466 (beginning-of-line)
6467 (while (and
6468 (not (looking-at rexp ))
6469 (looking-at verilog-assignment-operation-re)
6470 (progn
6471 (end-of-line)
6472 (not (eq e (point)))))
6473 (setq e (point))
6474 (verilog-forward-syntactic-ws)
6475 (beginning-of-line)
6476 )
6477 e))
6478 (endpos (set-marker (make-marker) end))
6479 (ind)
6480 )
6481 (goto-char start)
6482 (verilog-do-indent (verilog-calculate-indent))
6483 (if (and (not quiet)
6484 (> (- end start) 100))
6485 (message "Lining up expressions..(please stand by)"))
6486
6487 ;; Set indent to minimum throughout region
6488 (while (< (point) (marker-position endpos))
6489 (beginning-of-line)
6490 (verilog-just-one-space verilog-assignment-operation-re)
6491 (beginning-of-line)
6492 (verilog-do-indent (verilog-calculate-indent))
6493 (end-of-line)
6494 (verilog-forward-syntactic-ws)
6495 )
6496
6497 ;; Now find biggest prefix
6498 (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
6499
6500 ;; Now indent each line.
6501 (goto-char start)
6502 (while (progn (setq e (marker-position endpos))
6503 (setq r (- e (point)))
6504 (> r 0))
6505 (setq e (point))
6506 (if (not quiet) (message "%d" r))
6507 (cond
6508 ((looking-at verilog-assignment-operation-re)
6509 (goto-char (match-beginning 2))
6510 (if (not (or (verilog-in-parenthesis-p) ;; leave attributes and comparisons alone
6511 (verilog-in-coverage-p)))
6512 (if (eq (char-after) ?=)
6513 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
6514 (indent-to ind)
6515 ))
6516 )
6517 ((verilog-continued-line-1 start)
6518 (goto-char e)
6519 (indent-line-to ind))
6520 (t ; Must be comment or white space
6521 (goto-char e)
6522 (verilog-forward-ws&directives)
6523 (forward-line -1))
6524 )
6525 (forward-line 1))
6526 (unless quiet (message ""))
6527 ))))))
6341f357
DN
6528
6529(defun verilog-just-one-space (myre)
6530 "Remove extra spaces around regular expression MYRE."
6531 (interactive)
6532 (if (and (not(looking-at verilog-complete-reg))
6533 (looking-at myre))
6534 (let ((p1 (match-end 1))
6535 (p2 (match-end 2)))
6536 (progn
6537 (goto-char p2)
9489a450 6538 (just-one-space)
6341f357 6539 (goto-char p1)
9489a450 6540 (just-one-space)))))
6341f357
DN
6541
6542(defun verilog-indent-declaration (baseind)
6543 "Indent current lines as declaration.
6544Line up the variable names based on previous declaration's indentation.
6545BASEIND is the base indent to offset everything."
6546 (interactive)
6547 (let ((pos (point-marker))
6548 (lim (save-excursion
6549 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
6550 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
6551 (point)))
6552 (ind)
6553 (val)
60618039 6554 (m1 (make-marker)))
7ea26faf
DN
6555 (setq val
6556 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6341f357
DN
6557 (indent-line-to val)
6558
6559 ;; Use previous declaration (in this module) as template.
a3a8b002
DN
6560 (if (or (eq 'all verilog-auto-lineup)
6561 (eq 'declarations verilog-auto-lineup))
6edb5716 6562 (if (verilog-re-search-backward
6341f357
DN
6563 (or (and verilog-indent-declaration-macros
6564 verilog-declaration-re-1-macro)
6565 verilog-declaration-re-1-no-macro) lim t)
6566 (progn
6567 (goto-char (match-end 0))
6568 (skip-chars-forward " \t")
6569 (setq ind (current-column))
6570 (goto-char pos)
7ea26faf
DN
6571 (setq val
6572 (+ baseind
6573 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6341f357
DN
6574 (indent-line-to val)
6575 (if (and verilog-indent-declaration-macros
6576 (looking-at verilog-declaration-re-2-macro))
6577 (let ((p (match-end 0)))
6578 (set-marker m1 p)
6579 (if (verilog-re-search-forward "[[#`]" p 'move)
6580 (progn
6581 (forward-char -1)
6582 (just-one-space)
6583 (goto-char (marker-position m1))
6584 (just-one-space)
60618039 6585 (indent-to ind))
6341f357
DN
6586 (if (/= (current-column) ind)
6587 (progn
6588 (just-one-space)
60618039 6589 (indent-to ind)))))
6341f357
DN
6590 (if (looking-at verilog-declaration-re-2-no-macro)
6591 (let ((p (match-end 0)))
6592 (set-marker m1 p)
6593 (if (verilog-re-search-forward "[[`#]" p 'move)
6594 (progn
6595 (forward-char -1)
6596 (just-one-space)
6597 (goto-char (marker-position m1))
6598 (just-one-space)
6599 (indent-to ind))
6600 (if (/= (current-column) ind)
6601 (progn
6602 (just-one-space)
7ea26faf
DN
6603 (indent-to ind))))))))))
6604 (goto-char pos)))
6341f357
DN
6605
6606(defun verilog-get-lineup-indent (b edpos)
6607 "Return the indent level that will line up several lines within the region.
6608Region is defined by B and EDPOS."
6609 (save-excursion
6610 (let ((ind 0) e)
6611 (goto-char b)
6612 ;; Get rightmost position
6613 (while (progn (setq e (marker-position edpos))
6614 (< (point) e))
6edb5716 6615 (if (verilog-re-search-forward
6341f357
DN
6616 (or (and verilog-indent-declaration-macros
6617 verilog-declaration-re-1-macro)
6618 verilog-declaration-re-1-no-macro) e 'move)
6619 (progn
6620 (goto-char (match-end 0))
6621 (verilog-backward-syntactic-ws)
6622 (if (> (current-column) ind)
6623 (setq ind (current-column)))
6624 (goto-char (match-end 0)))))
6625 (if (> ind 0)
6626 (1+ ind)
6627 ;; No lineup-string found
6628 (goto-char b)
6629 (end-of-line)
fd9ea9d3
WS
6630 (verilog-backward-syntactic-ws)
6631 ;;(skip-chars-backward " \t")
6341f357
DN
6632 (1+ (current-column))))))
6633
6634(defun verilog-get-lineup-indent-2 (myre b edpos)
6635 "Return the indent level that will line up several lines within the region."
6636 (save-excursion
6637 (let ((ind 0) e)
6638 (goto-char b)
6639 ;; Get rightmost position
6640 (while (progn (setq e (marker-position edpos))
6641 (< (point) e))
7e2a6000 6642 (if (and (verilog-re-search-forward myre e 'move)
9489a450 6643 (not (verilog-in-attribute-p))) ;; skip attribute exprs
6341f357 6644 (progn
7e2a6000 6645 (goto-char (match-beginning 2))
6341f357
DN
6646 (verilog-backward-syntactic-ws)
6647 (if (> (current-column) ind)
6648 (setq ind (current-column)))
7e2a6000
DN
6649 (goto-char (match-end 0)))
6650 ))
6341f357
DN
6651 (if (> ind 0)
6652 (1+ ind)
6653 ;; No lineup-string found
6654 (goto-char b)
6655 (end-of-line)
6656 (skip-chars-backward " \t")
6657 (1+ (current-column))))))
6658
6659(defun verilog-comment-depth (type val)
6660 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
6661 (save-excursion
6662 (let
6663 ((b (prog2
6664 (beginning-of-line)
6665 (point-marker)
6666 (end-of-line)))
6667 (e (point-marker)))
6668 (if (re-search-backward " /\\* \[#-\]# \[a-zA-Z\]+ \[0-9\]+ ## \\*/" b t)
6669 (progn
6670 (replace-match " /* -# ## */")
6671 (end-of-line))
6672 (progn
6673 (end-of-line)
6674 (insert " /* ## ## */"))))
6675 (backward-char 6)
6676 (insert
6677 (format "%s %d" type val))))
6678
6679;; \f
6680;;
6681;; Completion
6682;;
6683(defvar verilog-str nil)
6684(defvar verilog-all nil)
6685(defvar verilog-pred nil)
6686(defvar verilog-buffer-to-use nil)
6687(defvar verilog-flag nil)
6688(defvar verilog-toggle-completions nil
6689 "*True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
6690Repeated use of \\[verilog-complete-word] will show you all of them.
6691Normally, when there is more than one possible completion,
6692it displays a list of all possible completions.")
6693
6694
6695(defvar verilog-type-keywords
6696 '(
6697 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
6698 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
fd9ea9d3 6699 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
6341f357
DN
6700 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
6701 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
6702 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
6703 )
6704 "*Keywords for types used when completing a word in a declaration or parmlist.
0e5c8aed 6705\(integer, real, reg...)")
6341f357
DN
6706
6707(defvar verilog-cpp-keywords
6708 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
6709 "endif")
6710 "*Keywords to complete when at first word of a line in declarative scope.
0e5c8aed 6711\(initial, always, begin, assign...)
6341f357 6712The procedures and variables defined within the Verilog program
37ea4b9b 6713will be completed at runtime and should not be added to this list.")
6341f357
DN
6714
6715(defvar verilog-defun-keywords
6716 (append
6717 '(
6718 "always" "always_comb" "always_ff" "always_latch" "assign"
6719 "begin" "end" "generate" "endgenerate" "module" "endmodule"
6720 "specify" "endspecify" "function" "endfunction" "initial" "final"
6721 "task" "endtask" "primitive" "endprimitive"
6722 )
6723 verilog-type-keywords)
6724 "*Keywords to complete when at first word of a line in declarative scope.
0e5c8aed 6725\(initial, always, begin, assign...)
6341f357 6726The procedures and variables defined within the Verilog program
37ea4b9b 6727will be completed at runtime and should not be added to this list.")
6341f357
DN
6728
6729(defvar verilog-block-keywords
6730 '(
6731 "begin" "break" "case" "continue" "else" "end" "endfunction"
6732 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
6733 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
6734 "while")
6735 "*Keywords to complete when at first word of a line in behavioral scope.
0e5c8aed 6736\(begin, if, then, else, for, fork...)
6341f357 6737The procedures and variables defined within the Verilog program
37ea4b9b 6738will be completed at runtime and should not be added to this list.")
6341f357
DN
6739
6740(defvar verilog-tf-keywords
6741 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
6742 "*Keywords to complete when at first word of a line in a task or function.
0e5c8aed 6743\(begin, if, then, else, for, fork.)
6341f357 6744The procedures and variables defined within the Verilog program
37ea4b9b 6745will be completed at runtime and should not be added to this list.")
6341f357
DN
6746
6747(defvar verilog-case-keywords
6748 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
6749 "*Keywords to complete when at first word of a line in case scope.
0e5c8aed 6750\(begin, if, then, else, for, fork...)
6341f357 6751The procedures and variables defined within the Verilog program
37ea4b9b 6752will be completed at runtime and should not be added to this list.")
6341f357
DN
6753
6754(defvar verilog-separator-keywords
6755 '("else" "then" "begin")
6756 "*Keywords to complete when NOT standing at the first word of a statement.
0e5c8aed 6757\(else, then, begin...)
37ea4b9b
JB
6758Variables and function names defined within the Verilog program
6759will be completed at runtime and should not be added to this list.")
6341f357 6760
a03c2342
WS
6761(defvar verilog-gate-ios
6762 ;; All these have an implied {"input"...} at the end
6763 '(("and" "output")
6764 ("buf" "output")
6765 ("bufif0" "output")
6766 ("bufif1" "output")
6767 ("cmos" "output")
6768 ("nand" "output")
6769 ("nmos" "output")
6770 ("nor" "output")
6771 ("not" "output")
6772 ("notif0" "output")
6773 ("notif1" "output")
6774 ("or" "output")
6775 ("pmos" "output")
6776 ("pulldown" "output")
6777 ("pullup" "output")
6778 ("rcmos" "output")
6779 ("rnmos" "output")
6780 ("rpmos" "output")
6781 ("rtran" "inout" "inout")
6782 ("rtranif0" "inout" "inout")
6783 ("rtranif1" "inout" "inout")
6784 ("tran" "inout" "inout")
6785 ("tranif0" "inout" "inout")
6786 ("tranif1" "inout" "inout")
6787 ("xnor" "output")
6788 ("xor" "output"))
6789 "*Map of direction for each positional argument to each gate primitive.")
6790
6791(defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
6792 "*Keywords for gate primitives.")
6793
6341f357
DN
6794(defun verilog-string-diff (str1 str2)
6795 "Return index of first letter where STR1 and STR2 differs."
6796 (catch 'done
6797 (let ((diff 0))
6798 (while t
6799 (if (or (> (1+ diff) (length str1))
6800 (> (1+ diff) (length str2)))
6801 (throw 'done diff))
6802 (or (equal (aref str1 diff) (aref str2 diff))
6803 (throw 'done diff))
6804 (setq diff (1+ diff))))))
6805
6806;; Calculate all possible completions for functions if argument is `function',
6807;; completions for procedures if argument is `procedure' or both functions and
6808;; procedures otherwise.
6809
6810(defun verilog-func-completion (type)
6811 "Build regular expression for module/task/function names.
6812TYPE is 'module, 'tf for task or function, or t if unknown."
6813 (if (string= verilog-str "")
6814 (setq verilog-str "[a-zA-Z_]"))
6815 (let ((verilog-str (concat (cond
6816 ((eq type 'module) "\\<\\(module\\)\\s +")
6817 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
6818 (t "\\<\\(task\\|function\\|module\\)\\s +"))
6819 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
6820 match)
6821
6822 (if (not (looking-at verilog-defun-re))
6823 (verilog-re-search-backward verilog-defun-re nil t))
6824 (forward-char 1)
6825
6826 ;; Search through all reachable functions
6827 (goto-char (point-min))
6828 (while (verilog-re-search-forward verilog-str (point-max) t)
6829 (progn (setq match (buffer-substring (match-beginning 2)
6830 (match-end 2)))
6831 (if (or (null verilog-pred)
6832 (funcall verilog-pred match))
6833 (setq verilog-all (cons match verilog-all)))))
6834 (if (match-beginning 0)
6835 (goto-char (match-beginning 0)))))
6836
6837(defun verilog-get-completion-decl (end)
6838 "Macro for searching through current declaration (var, type or const)
6839for matches of `str' and adding the occurrence tp `all' through point END."
6840 (let ((re (or (and verilog-indent-declaration-macros
6841 verilog-declaration-re-2-macro)
6842 verilog-declaration-re-2-no-macro))
6843 decl-end match)
6844 ;; Traverse lines
6845 (while (and (< (point) end)
6846 (verilog-re-search-forward re end t))
6847 ;; Traverse current line
6848 (setq decl-end (save-excursion (verilog-declaration-end)))
6849 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
6850 (not (match-end 1)))
6851 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
6852 (if (string-match (concat "\\<" verilog-str) match)
6853 (if (or (null verilog-pred)
6854 (funcall verilog-pred match))
6855 (setq verilog-all (cons match verilog-all)))))
60618039
DN
6856 (forward-line 1)))
6857 verilog-all)
6341f357
DN
6858
6859(defun verilog-type-completion ()
6860 "Calculate all possible completions for types."
6861 (let ((start (point))
6862 goon)
6863 ;; Search for all reachable type declarations
6864 (while (or (verilog-beg-of-defun)
6865 (setq goon (not goon)))
6866 (save-excursion
6867 (if (and (< start (prog1 (save-excursion (verilog-end-of-defun)
6868 (point))
6869 (forward-char 1)))
6870 (verilog-re-search-forward
6871 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
6872 start t)
6873 (not (match-end 1)))
6874 ;; Check current type declaration
6875 (verilog-get-completion-decl start))))))
6876
6877(defun verilog-var-completion ()
6878 "Calculate all possible completions for variables (or constants)."
6879 (let ((start (point)))
6880 ;; Search for all reachable var declarations
6881 (verilog-beg-of-defun)
6882 (save-excursion
6883 ;; Check var declarations
6884 (verilog-get-completion-decl start))))
6885
6886(defun verilog-keyword-completion (keyword-list)
6887 "Give list of all possible completions of keywords in KEYWORD-LIST."
4f91a816 6888 (mapcar (lambda (s)
9489a450
MM
6889 (if (string-match (concat "\\<" verilog-str) s)
6890 (if (or (null verilog-pred)
6891 (funcall verilog-pred s))
6892 (setq verilog-all (cons s verilog-all)))))
6341f357
DN
6893 keyword-list))
6894
6895
6896(defun verilog-completion (verilog-str verilog-pred verilog-flag)
6897 "Function passed to `completing-read', `try-completion' or `all-completions'.
6898Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
6899must be a function to be called for every match to check if this should
37ea4b9b
JB
6900really be a match. If VERILOG-FLAG is t, the function returns a list of
6901all possible completions. If VERILOG-FLAG is nil it returns a string,
6902the longest possible completion, or t if VERILOG-STR is an exact match.
6903If VERILOG-FLAG is 'lambda, the function returns t if VERILOG-STR is an
6904exact match, nil otherwise."
6341f357
DN
6905 (save-excursion
6906 (let ((verilog-all nil))
6907 ;; Set buffer to use for searching labels. This should be set
6908 ;; within functions which use verilog-completions
6909 (set-buffer verilog-buffer-to-use)
6910
6911 ;; Determine what should be completed
6912 (let ((state (car (verilog-calculate-indent))))
6913 (cond ((eq state 'defun)
6914 (save-excursion (verilog-var-completion))
6915 (verilog-func-completion 'module)
6916 (verilog-keyword-completion verilog-defun-keywords))
6917
6918 ((eq state 'behavioral)
6919 (save-excursion (verilog-var-completion))
6920 (verilog-func-completion 'module)
6921 (verilog-keyword-completion verilog-defun-keywords))
6922
6923 ((eq state 'block)
6924 (save-excursion (verilog-var-completion))
6925 (verilog-func-completion 'tf)
6926 (verilog-keyword-completion verilog-block-keywords))
6927
6928 ((eq state 'case)
6929 (save-excursion (verilog-var-completion))
6930 (verilog-func-completion 'tf)
6931 (verilog-keyword-completion verilog-case-keywords))
6932
6933 ((eq state 'tf)
6934 (save-excursion (verilog-var-completion))
6935 (verilog-func-completion 'tf)
6936 (verilog-keyword-completion verilog-tf-keywords))
6937
6938 ((eq state 'cpp)
6939 (save-excursion (verilog-var-completion))
6940 (verilog-keyword-completion verilog-cpp-keywords))
6941
6942 ((eq state 'cparenexp)
6943 (save-excursion (verilog-var-completion)))
6944
6945 (t;--Anywhere else
6946 (save-excursion (verilog-var-completion))
6947 (verilog-func-completion 'both)
6948 (verilog-keyword-completion verilog-separator-keywords))))
6949
6950 ;; Now we have built a list of all matches. Give response to caller
6951 (verilog-completion-response))))
6952
6953(defun verilog-completion-response ()
6954 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
6955 ;; This was not called by all-completions
6956 (if (null verilog-all)
6957 ;; Return nil if there was no matching label
6958 nil
6959 ;; Get longest string common in the labels
6960 (let* ((elm (cdr verilog-all))
6961 (match (car verilog-all))
6962 (min (length match))
6963 tmp)
6964 (if (string= match verilog-str)
6965 ;; Return t if first match was an exact match
6966 (setq match t)
6967 (while (not (null elm))
6968 ;; Find longest common string
6969 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
6970 (progn
6971 (setq min tmp)
6972 (setq match (substring match 0 min))))
6973 ;; Terminate with match=t if this is an exact match
6974 (if (string= (car elm) verilog-str)
6975 (progn
6976 (setq match t)
6977 (setq elm nil))
6978 (setq elm (cdr elm)))))
6979 ;; If this is a test just for exact match, return nil ot t
6980 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
6981 nil
6982 match))))
6983 ;; If flag is t, this was called by all-completions. Return
6984 ;; list of all possible completions
6985 (verilog-flag
6986 verilog-all)))
6987
6988(defvar verilog-last-word-numb 0)
6989(defvar verilog-last-word-shown nil)
6990(defvar verilog-last-completions nil)
6991
6992(defun verilog-complete-word ()
6993 "Complete word at current point.
6994\(See also `verilog-toggle-completions', `verilog-type-keywords',
6995and `verilog-separator-keywords'.)"
6996 (interactive)
6997 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
6998 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
6999 (verilog-str (buffer-substring b e))
7000 ;; The following variable is used in verilog-completion
7001 (verilog-buffer-to-use (current-buffer))
7002 (allcomp (if (and verilog-toggle-completions
7003 (string= verilog-last-word-shown verilog-str))
7004 verilog-last-completions
7005 (all-completions verilog-str 'verilog-completion)))
7006 (match (if verilog-toggle-completions
7007 "" (try-completion
4f91a816 7008 verilog-str (mapcar (lambda (elm)
6341f357
DN
7009 (cons elm 0)) allcomp)))))
7010 ;; Delete old string
7011 (delete-region b e)
7012
7013 ;; Toggle-completions inserts whole labels
7014 (if verilog-toggle-completions
7015 (progn
7016 ;; Update entry number in list
7017 (setq verilog-last-completions allcomp
7018 verilog-last-word-numb
7019 (if (>= verilog-last-word-numb (1- (length allcomp)))
7020 0
7021 (1+ verilog-last-word-numb)))
7022 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
7023 ;; Display next match or same string if no match was found
7024 (if (not (null allcomp))
7025 (insert "" verilog-last-word-shown)
7026 (insert "" verilog-str)
7027 (message "(No match)")))
7028 ;; The other form of completion does not necessarily do that.
7029
7030 ;; Insert match if found, or the original string if no match
7031 (if (or (null match) (equal match 't))
7032 (progn (insert "" verilog-str)
7033 (message "(No match)"))
7034 (insert "" match))
7035 ;; Give message about current status of completion
7036 (cond ((equal match 't)
7037 (if (not (null (cdr allcomp)))
7038 (message "(Complete but not unique)")
7039 (message "(Sole completion)")))
7040 ;; Display buffer if the current completion didn't help
7041 ;; on completing the label.
7042 ((and (not (null (cdr allcomp))) (= (length verilog-str)
7043 (length match)))
7044 (with-output-to-temp-buffer "*Completions*"
7045 (display-completion-list allcomp))
7046 ;; Wait for a key press. Then delete *Completion* window
7047 (momentary-string-display "" (point))
7048 (delete-window (get-buffer-window (get-buffer "*Completions*")))
7049 )))))
7050
7051(defun verilog-show-completions ()
7052 "Show all possible completions at current point."
7053 (interactive)
7054 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7055 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7056 (verilog-str (buffer-substring b e))
7057 ;; The following variable is used in verilog-completion
7058 (verilog-buffer-to-use (current-buffer))
7059 (allcomp (if (and verilog-toggle-completions
7060 (string= verilog-last-word-shown verilog-str))
7061 verilog-last-completions
7062 (all-completions verilog-str 'verilog-completion))))
7063 ;; Show possible completions in a temporary buffer.
7064 (with-output-to-temp-buffer "*Completions*"
7065 (display-completion-list allcomp))
7066 ;; Wait for a key press. Then delete *Completion* window
7067 (momentary-string-display "" (point))
7068 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
7069
7070
7071(defun verilog-get-default-symbol ()
7072 "Return symbol around current point as a string."
7073 (save-excursion
7074 (buffer-substring (progn
7075 (skip-chars-backward " \t")
7076 (skip-chars-backward "a-zA-Z0-9_")
7077 (point))
7078 (progn
7079 (skip-chars-forward "a-zA-Z0-9_")
7080 (point)))))
7081
7082(defun verilog-build-defun-re (str &optional arg)
7083 "Return function/task/module starting with STR as regular expression.
7084With optional second ARG non-nil, STR is the complete name of the instruction."
7085 (if arg
7086 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
7087 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
7088
7089(defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
7090 "Function passed to `completing-read', `try-completion' or `all-completions'.
7091Returns a completion on any function name based on VERILOG-STR prefix. If
7092VERILOG-PRED is non-nil, it must be a function to be called for every match
7093to check if this should really be a match. If VERILOG-FLAG is t, the
7094function returns a list of all possible completions. If it is nil it
7095returns a string, the longest possible completion, or t if VERILOG-STR is
7096an exact match. If VERILOG-FLAG is 'lambda, the function returns t if
7097VERILOG-STR is an exact match, nil otherwise."
7098 (save-excursion
7099 (let ((verilog-all nil)
7100 match)
7101
7102 ;; Set buffer to use for searching labels. This should be set
7103 ;; within functions which use verilog-completions
7104 (set-buffer verilog-buffer-to-use)
7105
7106 (let ((verilog-str verilog-str))
7107 ;; Build regular expression for functions
7108 (if (string= verilog-str "")
7109 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
7110 (setq verilog-str (verilog-build-defun-re verilog-str)))
7111 (goto-char (point-min))
7112
7113 ;; Build a list of all possible completions
7114 (while (verilog-re-search-forward verilog-str nil t)
7115 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
7116 (if (or (null verilog-pred)
7117 (funcall verilog-pred match))
7118 (setq verilog-all (cons match verilog-all)))))
7119
7120 ;; Now we have built a list of all matches. Give response to caller
7121 (verilog-completion-response))))
7122
7123(defun verilog-goto-defun ()
a03c2342 7124 "Move to specified Verilog module/interface/task/function.
6341f357
DN
7125The default is a name found in the buffer around point.
7126If search fails, other files are checked based on
7127`verilog-library-flags'."
7128 (interactive)
7129 (let* ((default (verilog-get-default-symbol))
7130 ;; The following variable is used in verilog-comp-function
7131 (verilog-buffer-to-use (current-buffer))
7132 (label (if (not (string= default ""))
7133 ;; Do completion with default
a3a8b002
DN
7134 (completing-read (concat "Goto-Label: (default "
7135 default ") ")
6341f357
DN
7136 'verilog-comp-defun nil nil "")
7137 ;; There is no default value. Complete without it
a3a8b002 7138 (completing-read "Goto-Label: "
6341f357
DN
7139 'verilog-comp-defun nil nil "")))
7140 pt)
a3a8b002
DN
7141 ;; Make sure library paths are correct, in case need to resolve module
7142 (verilog-auto-reeval-locals)
7143 (verilog-getopt-flags)
6341f357
DN
7144 ;; If there was no response on prompt, use default value
7145 (if (string= label "")
7146 (setq label default))
7147 ;; Goto right place in buffer if label is not an empty string
7148 (or (string= label "")
7149 (progn
7150 (save-excursion
7151 (goto-char (point-min))
7ea26faf
DN
7152 (setq pt
7153 (re-search-forward (verilog-build-defun-re label t) nil t)))
6341f357
DN
7154 (when pt
7155 (goto-char pt)
7156 (beginning-of-line))
7157 pt)
60618039 7158 (verilog-goto-defun-file label))))
6341f357
DN
7159
7160;; Eliminate compile warning
7d55bf04 7161(defvar occur-pos-list)
6341f357
DN
7162
7163(defun verilog-showscopes ()
7164 "List all scopes in this module."
7165 (interactive)
7166 (let ((buffer (current-buffer))
7167 (linenum 1)
7168 (nlines 0)
7169 (first 1)
7170 (prevpos (point-min))
7171 (final-context-start (make-marker))
60618039 7172 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
6341f357
DN
7173 (with-output-to-temp-buffer "*Occur*"
7174 (save-excursion
7175 (message (format "Searching for %s ..." regexp))
7176 ;; Find next match, but give up if prev match was at end of buffer.
7177 (while (and (not (= prevpos (point-max)))
7178 (verilog-re-search-forward regexp nil t))
7179 (goto-char (match-beginning 0))
7180 (beginning-of-line)
7181 (save-match-data
7182 (setq linenum (+ linenum (count-lines prevpos (point)))))
7183 (setq prevpos (point))
7184 (goto-char (match-end 0))
7185 (let* ((start (save-excursion
7186 (goto-char (match-beginning 0))
7187 (forward-line (if (< nlines 0) nlines (- nlines)))
7188 (point)))
7189 (end (save-excursion
7190 (goto-char (match-end 0))
7191 (if (> nlines 0)
7192 (forward-line (1+ nlines))
7193 (forward-line 1))
7194 (point)))
7195 (tag (format "%3d" linenum))
7196 (empty (make-string (length tag) ?\ ))
7197 tem)
7198 (save-excursion
7199 (setq tem (make-marker))
7200 (set-marker tem (point))
7201 (set-buffer standard-output)
7202 (setq occur-pos-list (cons tem occur-pos-list))
7203 (or first (zerop nlines)
7204 (insert "--------\n"))
7205 (setq first nil)
7206 (insert-buffer-substring buffer start end)
7207 (backward-char (- end start))
7208 (setq tem (if (< nlines 0) (- nlines) nlines))
7209 (while (> tem 0)
7210 (insert empty ?:)
7211 (forward-line 1)
7212 (setq tem (1- tem)))
7213 (let ((this-linenum linenum))
7214 (set-marker final-context-start
7215 (+ (point) (- (match-end 0) (match-beginning 0))))
7216 (while (< (point) final-context-start)
7217 (if (null tag)
7218 (setq tag (format "%3d" this-linenum)))
7219 (insert tag ?:)))))))
7220 (set-buffer-modified-p nil))))
7221
7222
7223;; Highlight helper functions
7224(defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
7225(defun verilog-within-translate-off ()
7226 "Return point if within translate-off region, else nil."
7227 (and (save-excursion
7228 (re-search-backward
7229 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
7230 nil t))
7231 (equal "off" (match-string 2))
7232 (point)))
7233
7234(defun verilog-start-translate-off (limit)
7235 "Return point before translate-off directive if before LIMIT, else nil."
7236 (when (re-search-forward
7237 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7238 limit t)
7239 (match-beginning 0)))
7240
7241(defun verilog-back-to-start-translate-off (limit)
7242 "Return point before translate-off directive if before LIMIT, else nil."
7243 (when (re-search-backward
7244 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7245 limit t)
7246 (match-beginning 0)))
7247
7248(defun verilog-end-translate-off (limit)
7249 "Return point after translate-on directive if before LIMIT, else nil."
7250
7251 (re-search-forward (concat
7252 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
7253
7254(defun verilog-match-translate-off (limit)
7255 "Match a translate-off block, setting `match-data' and returning t, else nil.
7256Bound search by LIMIT."
7257 (when (< (point) limit)
7258 (let ((start (or (verilog-within-translate-off)
7259 (verilog-start-translate-off limit)))
7260 (case-fold-search t))
7261 (when start
7262 (let ((end (or (verilog-end-translate-off limit) limit)))
7263 (set-match-data (list start end))
7264 (goto-char end))))))
7265
7266(defun verilog-font-lock-match-item (limit)
7267 "Match, and move over, any declaration item after point.
7268Bound search by LIMIT. Adapted from
7269`font-lock-match-c-style-declaration-item-and-skip-to-next'."
7270 (condition-case nil
7271 (save-restriction
7272 (narrow-to-region (point-min) limit)
7273 ;; match item
7274 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
7275 (save-match-data
7276 (goto-char (match-end 1))
7277 ;; move to next item
7278 (if (looking-at "\\(\\s-*,\\)")
7279 (goto-char (match-end 1))
7280 (end-of-line) t))))
7281 (error nil)))
7282
7283
7284;; Added by Subbu Meiyappan for Header
7285
7286(defun verilog-header ()
a3a8b002
DN
7287 "Insert a standard Verilog file header.
7288See also `verilog-sk-header' for an alternative format."
6341f357
DN
7289 (interactive)
7290 (let ((start (point)))
7291 (insert "\
7292//-----------------------------------------------------------------------------
7293// Title : <title>
7294// Project : <project>
7295//-----------------------------------------------------------------------------
7296// File : <filename>
7297// Author : <author>
7298// Created : <credate>
7299// Last modified : <moddate>
7300//-----------------------------------------------------------------------------
7301// Description :
7302// <description>
7303//-----------------------------------------------------------------------------
7304// Copyright (c) <copydate> by <company> This model is the confidential and
7305// proprietary property of <company> and the possession or use of this
7306// file requires a written license from <company>.
7307//------------------------------------------------------------------------------
7308// Modification history :
7309// <modhist>
7310//-----------------------------------------------------------------------------
7311
7312")
7313 (goto-char start)
7314 (search-forward "<filename>")
7315 (replace-match (buffer-name) t t)
7316 (search-forward "<author>") (replace-match "" t t)
7317 (insert (user-full-name))
7318 (insert " <" (user-login-name) "@" (system-name) ">")
7319 (search-forward "<credate>") (replace-match "" t t)
6ca0ff73 7320 (verilog-insert-date)
6341f357 7321 (search-forward "<moddate>") (replace-match "" t t)
6ca0ff73 7322 (verilog-insert-date)
6341f357 7323 (search-forward "<copydate>") (replace-match "" t t)
6ca0ff73 7324 (verilog-insert-year)
6341f357 7325 (search-forward "<modhist>") (replace-match "" t t)
6ca0ff73 7326 (verilog-insert-date)
6341f357
DN
7327 (insert " : created")
7328 (goto-char start)
7329 (let (string)
7330 (setq string (read-string "title: "))
7331 (search-forward "<title>")
7332 (replace-match string t t)
7333 (setq string (read-string "project: " verilog-project))
6341f357
DN
7334 (setq verilog-project string)
7335 (search-forward "<project>")
7336 (replace-match string t t)
7337 (setq string (read-string "Company: " verilog-company))
6341f357
DN
7338 (setq verilog-company string)
7339 (search-forward "<company>")
7340 (replace-match string t t)
7341 (search-forward "<company>")
7342 (replace-match string t t)
7343 (search-forward "<company>")
7344 (replace-match string t t)
7345 (search-backward "<description>")
60618039 7346 (replace-match "" t t))))
6341f357 7347
6ca0ff73 7348;; verilog-header Uses the verilog-insert-date function
6341f357 7349
6ca0ff73 7350(defun verilog-insert-date ()
6341f357
DN
7351 "Insert date from the system."
7352 (interactive)
a3a8b002
DN
7353 (if verilog-date-scientific-format
7354 (insert (format-time-string "%Y/%m/%d"))
7355 (insert (format-time-string "%d.%m.%Y"))))
6341f357 7356
6ca0ff73 7357(defun verilog-insert-year ()
6341f357
DN
7358 "Insert year from the system."
7359 (interactive)
a3a8b002 7360 (insert (format-time-string "%Y")))
6341f357
DN
7361
7362\f
7363;;
7364;; Signal list parsing
7365;;
7366
7367;; Elements of a signal list
a03c2342
WS
7368(defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
7369 (list name bits comment mem enum signed type multidim modport))
6341f357
DN
7370(defsubst verilog-sig-name (sig)
7371 (car sig))
7372(defsubst verilog-sig-bits (sig)
7373 (nth 1 sig))
7374(defsubst verilog-sig-comment (sig)
7375 (nth 2 sig))
7376(defsubst verilog-sig-memory (sig)
7377 (nth 3 sig))
7378(defsubst verilog-sig-enum (sig)
7379 (nth 4 sig))
7380(defsubst verilog-sig-signed (sig)
7381 (nth 5 sig))
7382(defsubst verilog-sig-type (sig)
7383 (nth 6 sig))
7384(defsubst verilog-sig-multidim (sig)
7385 (nth 7 sig))
7386(defsubst verilog-sig-multidim-string (sig)
7387 (if (verilog-sig-multidim sig)
7388 (let ((str "") (args (verilog-sig-multidim sig)))
7389 (while args
7390 (setq str (concat str (car args)))
7391 (setq args (cdr args)))
7392 str)))
a3a8b002
DN
7393(defsubst verilog-sig-modport (sig)
7394 (nth 8 sig))
6341f357
DN
7395(defsubst verilog-sig-width (sig)
7396 (verilog-make-width-expression (verilog-sig-bits sig)))
7397
9489a450
MM
7398(defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
7399 (list outputs-del outputs-imm temps inputs))
7400(defsubst verilog-alw-get-outputs-delayed (sigs)
6341f357 7401 (nth 0 sigs))
9489a450 7402(defsubst verilog-alw-get-outputs-immediate (sigs)
a03c2342 7403 (nth 1 sigs))
9489a450 7404(defsubst verilog-alw-get-temps (sigs)
a03c2342 7405 (nth 2 sigs))
9489a450 7406(defsubst verilog-alw-get-inputs (sigs)
6341f357 7407 (nth 3 sigs))
9489a450
MM
7408(defsubst verilog-alw-get-uses-delayed (sigs)
7409 (nth 0 sigs))
6341f357 7410
a03c2342
WS
7411(defsubst verilog-modi-new (name fob pt type)
7412 (vector name fob pt type))
7413(defsubst verilog-modi-name (modi)
7414 (aref modi 0))
7415(defsubst verilog-modi-file-or-buffer (modi)
7416 (aref modi 1))
7417(defsubst verilog-modi-get-point (modi)
7418 (aref modi 2))
7419(defsubst verilog-modi-get-type (modi) ;; "module" or "interface"
7420 (aref modi 3))
7421(defsubst verilog-modi-get-decls (modi)
7422 (verilog-modi-cache-results modi 'verilog-read-decls))
7423(defsubst verilog-modi-get-sub-decls (modi)
7424 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
7425
7426;; Signal reading for given module
7427;; Note these all take modi's - as returned from verilog-modi-current
9489a450
MM
7428(defsubst verilog-decls-new (out inout in vars unuseds assigns consts gparams interfaces)
7429 (vector out inout in vars unuseds assigns consts gparams interfaces))
a03c2342
WS
7430(defsubst verilog-decls-get-outputs (decls)
7431 (aref decls 0))
7432(defsubst verilog-decls-get-inouts (decls)
7433 (aref decls 1))
7434(defsubst verilog-decls-get-inputs (decls)
7435 (aref decls 2))
9489a450 7436(defsubst verilog-decls-get-vars (decls)
a03c2342 7437 (aref decls 3))
9489a450
MM
7438;;(defsubst verilog-decls-get-unused (decls)
7439;; (aref decls 4))
a03c2342
WS
7440(defsubst verilog-decls-get-assigns (decls)
7441 (aref decls 5))
7442(defsubst verilog-decls-get-consts (decls)
7443 (aref decls 6))
7444(defsubst verilog-decls-get-gparams (decls)
7445 (aref decls 7))
7446(defsubst verilog-decls-get-interfaces (decls)
7447 (aref decls 8))
7448
7449(defsubst verilog-subdecls-new (out inout in intf intfd)
7450 (vector out inout in intf intfd))
7451(defsubst verilog-subdecls-get-outputs (subdecls)
7452 (aref subdecls 0))
7453(defsubst verilog-subdecls-get-inouts (subdecls)
7454 (aref subdecls 1))
7455(defsubst verilog-subdecls-get-inputs (subdecls)
7456 (aref subdecls 2))
7457(defsubst verilog-subdecls-get-interfaces (subdecls)
7458 (aref subdecls 3))
7459(defsubst verilog-subdecls-get-interfaced (subdecls)
7460 (aref subdecls 4))
7461
6341f357 7462(defun verilog-signals-not-in (in-list not-list)
37ea4b9b
JB
7463 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7464Also remove any duplicates in IN-LIST.
6341f357 7465Signals must be in standard (base vector) form."
a03c2342
WS
7466 ;; This function is hot, so implemented as O(1)
7467 (cond ((eval-when-compile (fboundp 'make-hash-table))
7468 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7469 out-list)
7470 (while not-list
7471 (puthash (car (car not-list)) t ht)
7472 (setq not-list (cdr not-list)))
7473 (while in-list
7474 (when (not (gethash (car (car in-list)) ht))
7475 (setq out-list (cons (car in-list) out-list))
7476 (puthash (car (car in-list)) t ht))
7477 (setq in-list (cdr in-list)))
7478 (nreverse out-list)))
7479 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7480 (t
7481 (let (out-list)
7482 (while in-list
7483 (if (not (or (assoc (car (car in-list)) not-list)
7484 (assoc (car (car in-list)) out-list)))
7485 (setq out-list (cons (car in-list) out-list)))
7486 (setq in-list (cdr in-list)))
7487 (nreverse out-list)))))
6341f357
DN
7488;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
7489
6341f357 7490(defun verilog-signals-memory (in-list)
53964682 7491 "Return list of signals in IN-LIST that are memorized (multidimensional)."
6341f357
DN
7492 (let (out-list)
7493 (while in-list
7494 (if (nth 3 (car in-list))
7495 (setq out-list (cons (car in-list) out-list)))
7496 (setq in-list (cdr in-list)))
7497 out-list))
7498;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
7499
7500(defun verilog-signals-sort-compare (a b)
7501 "Compare signal A and B for sorting."
7502 (string< (car a) (car b)))
7503
7504(defun verilog-signals-not-params (in-list)
7505 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
7506 (let (out-list)
7507 (while in-list
7508 (unless (boundp (intern (concat "vh-" (car (car in-list)))))
7509 (setq out-list (cons (car in-list) out-list)))
7510 (setq in-list (cdr in-list)))
7511 (nreverse out-list)))
7512
7513(defun verilog-signals-combine-bus (in-list)
301b181a 7514 "Return a list of signals in IN-LIST, with buses combined.
6341f357
DN
7515Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
7516 (let (combo buswarn
7517 out-list
7518 sig highbit lowbit ; Temp information about current signal
7519 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
7520 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
a3a8b002 7521 sv-modport
6341f357
DN
7522 bus)
7523 ;; Shove signals so duplicated signals will be adjacent
7524 (setq in-list (sort in-list `verilog-signals-sort-compare))
7525 (while in-list
7526 (setq sig (car in-list))
7527 ;; No current signal; form from existing details
7528 (unless sv-name
7529 (setq sv-name (verilog-sig-name sig)
7530 sv-highbit nil
7531 sv-busstring nil
7532 sv-comment (verilog-sig-comment sig)
7533 sv-memory (verilog-sig-memory sig)
7534 sv-enum (verilog-sig-enum sig)
7535 sv-signed (verilog-sig-signed sig)
7536 sv-type (verilog-sig-type sig)
7537 sv-multidim (verilog-sig-multidim sig)
a3a8b002 7538 sv-modport (verilog-sig-modport sig)
6341f357 7539 combo ""
60618039 7540 buswarn ""))
6341f357
DN
7541 ;; Extract bus details
7542 (setq bus (verilog-sig-bits sig))
9489a450 7543 (setq bus (and bus (verilog-simplify-range-expression bus)))
6341f357
DN
7544 (cond ((and bus
7545 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
7ea26faf
DN
7546 (setq highbit (string-to-number (match-string 1 bus))
7547 lowbit (string-to-number
7548 (match-string 2 bus))))
6341f357 7549 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
7ea26faf 7550 (setq highbit (string-to-number (match-string 1 bus))
6341f357
DN
7551 lowbit highbit))))
7552 ;; Combine bits in bus
7553 (if sv-highbit
7554 (setq sv-highbit (max highbit sv-highbit)
7555 sv-lowbit (min lowbit sv-lowbit))
7556 (setq sv-highbit highbit
7557 sv-lowbit lowbit)))
7558 (bus
7559 ;; String, probably something like `preproc:0
7560 (setq sv-busstring bus)))
7561 ;; Peek ahead to next signal
7562 (setq in-list (cdr in-list))
7563 (setq sig (car in-list))
7564 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
7565 ;; Combine with this signal
7ea26faf
DN
7566 (when (and sv-busstring
7567 (not (equal sv-busstring (verilog-sig-bits sig))))
6341f357
DN
7568 (when nil ;; Debugging
7569 (message (concat "Warning, can't merge into single bus "
7570 sv-name bus
7571 ", the AUTOs may be wrong")))
7572 (setq buswarn ", Couldn't Merge"))
7573 (if (verilog-sig-comment sig) (setq combo ", ..."))
7574 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
7575 sv-enum (or sv-enum (verilog-sig-enum sig))
7576 sv-signed (or sv-signed (verilog-sig-signed sig))
7577 sv-type (or sv-type (verilog-sig-type sig))
a3a8b002
DN
7578 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
7579 sv-modport (or sv-modport (verilog-sig-modport sig))))
6341f357
DN
7580 ;; Doesn't match next signal, add to queue, zero in prep for next
7581 ;; Note sig may also be nil for the last signal in the list
7582 (t
7583 (setq out-list
a03c2342
WS
7584 (cons (verilog-sig-new
7585 sv-name
7ea26faf
DN
7586 (or sv-busstring
7587 (if sv-highbit
7588 (concat "[" (int-to-string sv-highbit) ":"
7589 (int-to-string sv-lowbit) "]")))
60618039 7590 (concat sv-comment combo buswarn)
a3a8b002 7591 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
a03c2342 7592 out-list)
60618039 7593 sv-name nil))))
6341f357
DN
7594 ;;
7595 out-list))
7596
7597(defun verilog-sig-tieoff (sig &optional no-width)
60618039 7598 "Return tieoff expression for given SIG, with appropriate width.
6341f357
DN
7599Ignore width if optional NO-WIDTH is set."
7600 (let* ((width (if no-width nil (verilog-sig-width sig))))
7601 (concat
7602 (if (and verilog-active-low-regexp
7603 (string-match verilog-active-low-regexp (verilog-sig-name sig)))
7604 "~" "")
7605 (cond ((not width)
7606 "0")
7607 ((string-match "^[0-9]+$" width)
7608 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
7609 (t
7610 (concat "{" width "{1'b0}}"))))))
7611
7612;;
7613;; Port/Wire/Etc Reading
7614;;
7615
7616(defun verilog-read-inst-backward-name ()
7617 "Internal. Move point back to beginning of inst-name."
7618 (verilog-backward-open-paren)
7619 (let (done)
7620 (while (not done)
7621 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil) ; ] isn't word boundary
7622 (cond ((looking-at ")")
7623 (verilog-backward-open-paren))
7624 (t (setq done t)))))
7625 (while (looking-at "\\]")
7626 (verilog-backward-open-bracket)
7627 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil))
7628 (skip-chars-backward "a-zA-Z0-9`_$"))
7629
a03c2342
WS
7630(defun verilog-read-inst-module-matcher ()
7631 "Set match data 0 with module_name when point is inside instantiation."
7632 (verilog-read-inst-backward-name)
7633 ;; Skip over instantiation name
7634 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
7635 ;; Check for parameterized instantiations
7636 (when (looking-at ")")
7637 (verilog-backward-open-paren)
7638 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
7639 (skip-chars-backward "a-zA-Z0-9'_$")
7640 (looking-at "[a-zA-Z0-9`_\$]+")
7641 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7642 (buffer-substring-no-properties (match-beginning 0) (match-end 0))
7643 ;; Caller assumes match-beginning/match-end is still set
7644 )
7645
6341f357
DN
7646(defun verilog-read-inst-module ()
7647 "Return module_name when point is inside instantiation."
7648 (save-excursion
a03c2342 7649 (verilog-read-inst-module-matcher)))
6341f357
DN
7650
7651(defun verilog-read-inst-name ()
7652 "Return instance_name when point is inside instantiation."
7653 (save-excursion
7654 (verilog-read-inst-backward-name)
7655 (looking-at "[a-zA-Z0-9`_\$]+")
d63b01e1 7656 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6341f357
DN
7657 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
7658
7659(defun verilog-read-module-name ()
7660 "Return module name when after its ( or ;."
7661 (save-excursion
7662 (re-search-backward "[(;]")
9489a450
MM
7663 ;; Due to "module x import y (" we must search for declaration begin
7664 (verilog-re-search-backward-quick verilog-defun-re nil nil)
7665 (goto-char (match-end 0))
7666 (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_\$]+" nil nil)
d63b01e1 7667 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
a3a8b002
DN
7668 (verilog-symbol-detick
7669 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
6341f357 7670
4c5e69c6
DN
7671(defun verilog-read-inst-param-value ()
7672 "Return list of parameters and values when point is inside instantiation."
7673 (save-excursion
7674 (verilog-read-inst-backward-name)
7675 ;; Skip over instantiation name
7676 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
7677 ;; If there are parameterized instantiations
7678 (when (looking-at ")")
7679 (let ((end-pt (point))
7680 params
7681 param-name paren-beg-pt param-value)
7682 (verilog-backward-open-paren)
7683 (while (verilog-re-search-forward-quick "\\." end-pt t)
7684 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_\$]\\)" nil nil)
7685 (skip-chars-backward "a-zA-Z0-9'_$")
7686 (looking-at "[a-zA-Z0-9`_\$]+")
7687 (setq param-name (buffer-substring-no-properties
7688 (match-beginning 0) (match-end 0)))
7689 (verilog-re-search-forward-quick "(" nil nil)
7690 (setq paren-beg-pt (point))
7691 (verilog-forward-close-paren)
7692 (setq param-value (verilog-string-remove-spaces
7693 (buffer-substring-no-properties
7694 paren-beg-pt (1- (point)))))
7695 (setq params (cons (list param-name param-value) params)))
7696 params))))
7697
6341f357
DN
7698(defun verilog-read-auto-params (num-param &optional max-param)
7699 "Return parameter list inside auto.
7700Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
7701 (let ((olist))
7702 (save-excursion
7703 ;; /*AUTOPUNT("parameter", "parameter")*/
7704 (search-backward "(")
7705 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
7706 (setq olist (cons (match-string 1) olist))
7707 (goto-char (match-end 0))))
7708 (or (eq nil num-param)
7709 (<= num-param (length olist))
7710 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
7711 (if (eq max-param nil) (setq max-param num-param))
7712 (or (eq nil max-param)
7713 (>= max-param (length olist))
7714 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
7715 (nreverse olist)))
7716
7717(defun verilog-read-decls ()
7718 "Compute signal declaration information for the current module at point.
7719Return a array of [outputs inouts inputs wire reg assign const]."
7720 (let ((end-mod-point (or (verilog-get-end-of-defun t) (point-max)))
a3a8b002 7721 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
9489a450
MM
7722 in-modport ign-prop
7723 sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
a3a8b002
DN
7724 sigs-gparam sigs-intf
7725 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
7726 modport)
6341f357 7727 (save-excursion
9489a450 7728 (verilog-beg-of-defun-quick)
6341f357
DN
7729 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
7730 (while (< (point) end-mod-point)
a03c2342 7731 ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
6341f357
DN
7732 (cond
7733 ((looking-at "//")
7734 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
7735 (setq enum (match-string 1)))
7736 (search-forward "\n"))
7737 ((looking-at "/\\*")
7738 (forward-char 2)
a03c2342 7739 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
6341f357
DN
7740 (setq enum (match-string 1)))
7741 (or (search-forward "*/")
7742 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
7743 ((looking-at "(\\*")
9489a450
MM
7744 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
7745 (forward-char 1)
7746 (or (search-forward "*)")
6341f357
DN
7747 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
7748 ((eq ?\" (following-char))
7749 (or (re-search-forward "[^\\]\"" nil t) ;; don't forward-char first, since we look for a non backslash first
7750 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
7751 ((eq ?\; (following-char))
a3a8b002 7752 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
9489a450 7753 v2kargs-ok nil in-modport nil ign-prop nil)
6341f357
DN
7754 (forward-char 1))
7755 ((eq ?= (following-char))
7756 (setq rvalue t newsig nil)
7757 (forward-char 1))
14862301
SM
7758 ((and (eq ?, (following-char))
7759 (eq paren sig-paren))
7760 (setq rvalue nil)
7761 (forward-char 1))
7762 ;; ,'s can occur inside {} & funcs
7763 ((looking-at "[{(]")
7764 (setq paren (1+ paren))
7765 (forward-char 1))
7766 ((looking-at "[})]")
7767 (setq paren (1- paren))
7768 (forward-char 1)
7769 (when (< paren sig-paren)
7770 (setq expect-signal nil))) ; ) that ends variables inside v2k arg list
6341f357
DN
7771 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
7772 (goto-char (match-end 0))
7773 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
8468f78b
WS
7774 (setcar (cdr (cdr (cdr newsig)))
7775 (if (verilog-sig-memory newsig)
7776 (concat (verilog-sig-memory newsig) (match-string 1))
7777 (match-string 1))))
6341f357
DN
7778 (vec ;; Multidimensional
7779 (setq multidim (cons vec multidim))
7780 (setq vec (verilog-string-replace-matches
7781 "\\s-+" "" nil nil (match-string 1))))
7782 (t ;; Bit width
7783 (setq vec (verilog-string-replace-matches
7784 "\\s-+" "" nil nil (match-string 1))))))
7785 ;; Normal or escaped identifier -- note we remember the \ if escaped
7786 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
7787 (goto-char (match-end 0))
7788 (setq keywd (match-string 1))
86a4c7ac 7789 (when (string-match "^\\\\" (match-string 1))
6341f357 7790 (setq keywd (concat keywd " "))) ;; Escaped ID needs space at end
86a4c7ac
DN
7791 ;; Add any :: package names to same identifier
7792 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
7793 (goto-char (match-end 0))
7794 (setq keywd (concat keywd "::" (match-string 1)))
7795 (when (string-match "^\\\\" (match-string 1))
7796 (setq keywd (concat keywd " ")))) ;; Escaped ID needs space at end
6341f357
DN
7797 (cond ((equal keywd "input")
7798 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
a3a8b002 7799 expect-signal 'sigs-in io t modport nil))
6341f357
DN
7800 ((equal keywd "output")
7801 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
a3a8b002 7802 expect-signal 'sigs-out io t modport nil))
6341f357
DN
7803 ((equal keywd "inout")
7804 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
a3a8b002
DN
7805 expect-signal 'sigs-inout io t modport nil))
7806 ((equal keywd "parameter")
7807 (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
7808 expect-signal 'sigs-gparam io t modport nil))
9489a450
MM
7809 ((member keywd '("wire"
7810 "tri" "tri0" "tri1" "triand" "trior" "wand" "wor"
7811 "reg" "trireg"
a3a8b002 7812 "byte" "shortint" "int" "longint" "integer" "time"
a03c2342
WS
7813 "bit" "logic"
7814 "shortreal" "real" "realtime"
7815 "string" "event" "chandle"))
6341f357 7816 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
9489a450 7817 expect-signal 'sigs-var modport nil)))
6341f357
DN
7818 ((equal keywd "assign")
7819 (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
a3a8b002
DN
7820 expect-signal 'sigs-assign modport nil))
7821 ((member keywd '("supply0" "supply1" "supply"
7822 "localparam" "genvar"))
6341f357 7823 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
a3a8b002 7824 expect-signal 'sigs-const modport nil)))
9489a450
MM
7825 ((member keywd '("signed" "unsigned"))
7826 (setq signed keywd))
7827 ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
7828 (setq ign-prop t))
a3a8b002
DN
7829 ((member keywd '("class" "clocking" "covergroup" "function"
7830 "property" "randsequence" "sequence" "task"))
9489a450
MM
7831 (unless ign-prop
7832 (setq functask (1+ functask))))
a3a8b002
DN
7833 ((member keywd '("endclass" "endclocking" "endgroup" "endfunction"
7834 "endproperty" "endsequence" "endtask"))
6341f357 7835 (setq functask (1- functask)))
a03c2342
WS
7836 ((equal keywd "modport")
7837 (setq in-modport t))
a3a8b002 7838 ;; Ifdef? Ignore name of define
a03c2342 7839 ((member keywd '("`ifdef" "`ifndef" "`elsif"))
6341f357 7840 (setq rvalue t))
a3a8b002 7841 ;; Type?
6341f357
DN
7842 ((verilog-typedef-name-p keywd)
7843 (setq typedefed keywd))
a3a8b002
DN
7844 ;; Interface with optional modport in v2k arglist?
7845 ;; Skip over parsing modport, and take the interface name as the type
7846 ((and v2kargs-ok
7847 (eq paren 1)
a03c2342
WS
7848 (not rvalue)
7849 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
a3a8b002
DN
7850 (when (match-end 2) (goto-char (match-end 2)))
7851 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed keywd multidim nil sig-paren paren
7852 expect-signal 'sigs-intf io t modport (match-string 2)))
a03c2342
WS
7853 ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
7854 ((looking-at "\\s-*\\.")
7855 (goto-char (match-end 0))
7856 (when (not rvalue)
7857 (setq expect-signal nil)))
a3a8b002 7858 ;; New signal, maybe?
6341f357 7859 ((and expect-signal
6341f357 7860 (not rvalue)
a03c2342
WS
7861 (eq functask 0)
7862 (not in-modport)
6341f357
DN
7863 (not (member keywd verilog-keywords)))
7864 ;; Add new signal to expect-signal's variable
a03c2342 7865 (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
6341f357
DN
7866 (set expect-signal (cons newsig
7867 (symbol-value expect-signal))))))
7868 (t
7869 (forward-char 1)))
7870 (skip-syntax-forward " "))
7871 ;; Return arguments
a03c2342
WS
7872 (verilog-decls-new (nreverse sigs-out)
7873 (nreverse sigs-inout)
7874 (nreverse sigs-in)
9489a450
MM
7875 (nreverse sigs-var)
7876 nil
a03c2342
WS
7877 (nreverse sigs-assign)
7878 (nreverse sigs-const)
7879 (nreverse sigs-gparam)
7880 (nreverse sigs-intf)))))
7881
7882(defvar verilog-read-sub-decls-in-interfaced nil
7883 "For `verilog-read-sub-decls', process next signal as under interfaced block.")
7884
7885(defvar verilog-read-sub-decls-gate-ios nil
7886 "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
6341f357 7887
60618039
DN
7888(eval-when-compile
7889 ;; Prevent compile warnings; these are let's, not globals
7890 ;; Do not remove the eval-when-compile
7891 ;; - we want a error when we are debugging this code if they are refed.
7892 (defvar sigs-in)
7893 (defvar sigs-inout)
a3a8b002 7894 (defvar sigs-out)
a03c2342
WS
7895 (defvar sigs-intf)
7896 (defvar sigs-intfd))
5509c6ad
DN
7897
7898(defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
37ea4b9b 7899 "For `verilog-read-sub-decls-line', add a signal."
a03c2342
WS
7900 ;; sig eq t to indicate .name syntax
7901 ;;(message "vrsds: %s(%S)" port sig)
7902 (let ((dotname (eq sig t))
7903 portdata)
6341f357
DN
7904 (when sig
7905 (setq port (verilog-symbol-detick-denumber port))
a03c2342 7906 (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
6341f357
DN
7907 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
7908 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
7909 (unless (or (not sig)
7910 (equal sig "")) ;; Ignore .foo(1'b1) assignments
a03c2342
WS
7911 (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
7912 (equal "inout" verilog-read-sub-decls-gate-ios))
7913 (setq sigs-inout
7914 (cons (verilog-sig-new
7915 sig
7916 (if dotname (verilog-sig-bits portdata) vec)
8468f78b
WS
7917 (concat "To/From " comment)
7918 (verilog-sig-memory portdata)
7919 nil
a03c2342
WS
7920 (verilog-sig-signed portdata)
7921 (verilog-sig-type portdata)
7922 multidim nil)
7923 sigs-inout)))
7924 ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
7925 (equal "output" verilog-read-sub-decls-gate-ios))
7926 (setq sigs-out
7927 (cons (verilog-sig-new
7928 sig
7929 (if dotname (verilog-sig-bits portdata) vec)
8468f78b
WS
7930 (concat "From " comment)
7931 (verilog-sig-memory portdata)
7932 nil
a03c2342
WS
7933 (verilog-sig-signed portdata)
7934 (verilog-sig-type portdata)
7935 multidim nil)
7936 sigs-out)))
7937 ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
7938 (equal "input" verilog-read-sub-decls-gate-ios))
7939 (setq sigs-in
7940 (cons (verilog-sig-new
7941 sig
7942 (if dotname (verilog-sig-bits portdata) vec)
8468f78b
WS
7943 (concat "To " comment)
7944 (verilog-sig-memory portdata)
7945 nil
a03c2342
WS
7946 (verilog-sig-signed portdata)
7947 (verilog-sig-type portdata)
7948 multidim nil)
7949 sigs-in)))
a3a8b002 7950 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
a03c2342
WS
7951 (setq sigs-intf
7952 (cons (verilog-sig-new
7953 sig
7954 (if dotname (verilog-sig-bits portdata) vec)
8468f78b
WS
7955 (concat "To/From " comment)
7956 (verilog-sig-memory portdata)
7957 nil
a03c2342
WS
7958 (verilog-sig-signed portdata)
7959 (verilog-sig-type portdata)
7960 multidim nil)
7961 sigs-intf)))
7962 ((setq portdata (and verilog-read-sub-decls-in-interfaced
9489a450 7963 (assoc port (verilog-decls-get-vars submoddecls))))
a03c2342
WS
7964 (setq sigs-intfd
7965 (cons (verilog-sig-new
7966 sig
7967 (if dotname (verilog-sig-bits portdata) vec)
8468f78b
WS
7968 (concat "To/From " comment)
7969 (verilog-sig-memory portdata)
7970 nil
a03c2342
WS
7971 (verilog-sig-signed portdata)
7972 (verilog-sig-type portdata)
7973 multidim nil)
7974 sigs-intf)))
6341f357
DN
7975 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
7976 )))))
7977
a3a8b002
DN
7978(defun verilog-read-sub-decls-expr (submoddecls comment port expr)
7979 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
7980 ;;(message "vrsde: '%s'" expr)
7981 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
7982 (setq expr (verilog-string-replace-matches "/\\*\\(\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
7983 ;; Remove front operators
7984 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
7985 ;;
7986 (cond
7987 ;; {..., a, b} requires us to recurse on a,b
a03c2342
WS
7988 ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
7989 ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
14862301 7990 (unless verilog-auto-ignore-concat
a03c2342 7991 (let ((mlst (split-string (match-string 1 expr) "[{},]"))
14862301
SM
7992 mstr)
7993 (while (setq mstr (pop mlst))
7994 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
a3a8b002
DN
7995 (t
7996 (let (sig vec multidim)
a03c2342
WS
7997 ;; Remove leading reduction operators, etc
7998 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
7999 ;;(message "vrsde-ptop: '%s'" expr)
a3a8b002
DN
8000 (cond ;; Find \signal. Final space is part of escaped signal name
8001 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
8002 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8003 (setq sig (match-string 1 expr)
8004 expr (substring expr (match-end 0))))
8005 ;; Find signal
a03c2342 8006 ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
a3a8b002
DN
8007 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8008 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
8009 expr (substring expr (match-end 0)))))
8010 ;; Find [vector] or [multi][multi][multi][vector]
8011 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
8012 ;;(message "vrsde-v: '%s'" (match-string 1 expr))
8013 (when vec (setq multidim (cons vec multidim)))
8014 (setq vec (match-string 1 expr)
8015 expr (substring expr (match-end 0))))
8016 ;; If found signal, and nothing unrecognized, add the signal
8017 ;;(message "vrsde-rem: '%s'" expr)
8018 (when (and sig (string-match "^\\s-*$" expr))
8019 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))))
8020
5509c6ad 8021(defun verilog-read-sub-decls-line (submoddecls comment)
a03c2342
WS
8022 "For `verilog-read-sub-decls', read lines of port defs until none match.
8023Inserts the list of signals found, using submodi to look up each port."
a3a8b002 8024 (let (done port)
6341f357
DN
8025 (save-excursion
8026 (forward-line 1)
8027 (while (not done)
8028 ;; Get port name
8029 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
8030 (setq port (match-string 1))
8031 (goto-char (match-end 0)))
a03c2342 8032 ;; .\escaped (
6341f357
DN
8033 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
8034 (setq port (concat (match-string 1) " ")) ;; escaped id's need trailing space
8035 (goto-char (match-end 0)))
a03c2342
WS
8036 ;; .name
8037 ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
8038 (verilog-read-sub-decls-sig
8039 submoddecls comment (match-string 1) t ; sig==t for .name
8040 nil nil) ; vec multidim
8041 (setq port nil))
8042 ;; .\escaped_name
8043 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
8044 (verilog-read-sub-decls-sig
8045 submoddecls comment (concat (match-string 1) " ") t ; sig==t for .name
8046 nil nil) ; vec multidim
8047 (setq port nil))
8048 ;; random
6341f357
DN
8049 ((looking-at "\\s-*\\.[^(]*(")
8050 (setq port nil) ;; skip this line
8051 (goto-char (match-end 0)))
8052 (t
8053 (setq port nil done t))) ;; Unknown, ignore rest of line
a3a8b002
DN
8054 ;; Get signal name. Point is at the first-non-space after (
8055 ;; We intentionally ignore (non-escaped) signals with .s in them
8056 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
6341f357 8057 (when port
a03c2342 8058 (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
a3a8b002
DN
8059 (verilog-read-sub-decls-sig
8060 submoddecls comment port
8061 (verilog-string-remove-spaces (match-string 1)) ; sig
8062 nil nil)) ; vec multidim
8063 ;;
a03c2342 8064 ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
a3a8b002
DN
8065 (verilog-read-sub-decls-sig
8066 submoddecls comment port
8067 (verilog-string-remove-spaces (match-string 1)) ; sig
8068 (match-string 2) nil)) ; vec multidim
8069 ;; Fastpath was above looking-at's.
8070 ;; For something more complicated invoke a parser
8071 ((looking-at "[^)]+")
8072 (verilog-read-sub-decls-expr
8073 submoddecls comment port
8074 (buffer-substring
fd9ea9d3 8075 (point) (1- (progn (search-backward "(") ; start at (
9489a450
MM
8076 (verilog-forward-sexp-ign-cmt 1)
8077 (point)))))))) ; expr
6341f357
DN
8078 ;;
8079 (forward-line 1)))))
8080
a03c2342
WS
8081(defun verilog-read-sub-decls-gate (submoddecls comment submod end-inst-point)
8082 "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
8083Inserts the list of signals found."
8084 (save-excursion
8085 (let ((iolist (cdr (assoc submod verilog-gate-ios))))
8086 (while (< (point) end-inst-point)
8087 ;; Get primitive's signal name, as will never have port, and no trailing )
8088 (cond ((looking-at "//")
8089 (search-forward "\n"))
8090 ((looking-at "/\\*")
8091 (or (search-forward "*/")
8092 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8093 ((looking-at "(\\*")
9489a450
MM
8094 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8095 (forward-char 1)
8096 (or (search-forward "*)")
a03c2342
WS
8097 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8098 ;; On pins, parse and advance to next pin
8099 ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
8100 ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
8101 (goto-char (match-end 0))
8102 (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
8103 iolist (cdr iolist))
8104 (verilog-read-sub-decls-expr
8105 submoddecls comment "primitive_port"
8106 (match-string 0)))
8107 (t
8108 (forward-char 1)
8109 (skip-syntax-forward " ")))))))
8110
6341f357
DN
8111(defun verilog-read-sub-decls ()
8112 "Internally parse signals going to modules under this module.
8113Return a array of [ outputs inouts inputs ] signals for modules that are
8114instantiated in this module. For example if declare A A (.B(SIG)) and SIG
8115is a output, then SIG will be included in the list.
8116
8117This only works on instantiations created with /*AUTOINST*/ converted by
8118\\[verilog-auto-inst]. Otherwise, it would have to read in the whole
8119component library to determine connectivity of the design.
8120
8121One work around for this problem is to manually create // Inputs and //
8122Outputs comments above subcell signals, for example:
8123
1dd4b004 8124 module ModuleName (
6341f357
DN
8125 // Outputs
8126 .out (out),
8127 // Inputs
8128 .in (in));"
8129 (save-excursion
8130 (let ((end-mod-point (verilog-get-end-of-defun t))
8131 st-point end-inst-point
8132 ;; below 3 modified by verilog-read-sub-decls-line
a03c2342 8133 sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
9489a450
MM
8134 (verilog-beg-of-defun-quick)
8135 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
6341f357
DN
8136 (save-excursion
8137 (goto-char (match-beginning 0))
9489a450 8138 (unless (verilog-inside-comment-or-string-p)
6341f357
DN
8139 ;; Attempt to snarf a comment
8140 (let* ((submod (verilog-read-inst-module))
8141 (inst (verilog-read-inst-name))
a03c2342 8142 (subprim (member submod verilog-gate-keywords))
5509c6ad
DN
8143 (comment (concat inst " of " submod ".v"))
8144 submodi submoddecls)
a03c2342
WS
8145 (cond
8146 (subprim
8147 (setq submodi `primitive
8148 submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
8149 comment (concat inst " of " submod))
6341f357 8150 (verilog-backward-open-paren)
9489a450
MM
8151 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8152 (point))
6341f357 8153 st-point (point))
a03c2342
WS
8154 (forward-char 1)
8155 (verilog-read-sub-decls-gate submoddecls comment submod end-inst-point))
8156 ;; Non-primitive
8157 (t
8158 (when (setq submodi (verilog-modi-lookup submod t))
8159 (setq submoddecls (verilog-modi-get-decls submodi)
8160 verilog-read-sub-decls-gate-ios nil)
8161 (verilog-backward-open-paren)
9489a450
MM
8162 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8163 (point))
a03c2342
WS
8164 st-point (point))
8165 ;; This could have used a list created by verilog-auto-inst
8166 ;; However I want it to be runnable even on user's manually added signals
8167 (let ((verilog-read-sub-decls-in-interfaced t))
8168 (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
8169 (verilog-read-sub-decls-line submoddecls comment))) ;; Modifies sigs-ifd
8170 (goto-char st-point)
8171 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
8172 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8173 (goto-char st-point)
8174 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
8175 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8176 (goto-char st-point)
8177 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
8178 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-inout
8179 (goto-char st-point)
8180 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
8181 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-in
8182 )))))))
6341f357
DN
8183 ;; Combine duplicate bits
8184 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
a03c2342
WS
8185 (verilog-subdecls-new
8186 (verilog-signals-combine-bus (nreverse sigs-out))
8187 (verilog-signals-combine-bus (nreverse sigs-inout))
8188 (verilog-signals-combine-bus (nreverse sigs-in))
8189 (verilog-signals-combine-bus (nreverse sigs-intf))
8190 (verilog-signals-combine-bus (nreverse sigs-intfd))))))
6341f357
DN
8191
8192(defun verilog-read-inst-pins ()
37ea4b9b 8193 "Return an array of [ pins ] for the current instantiation at point.
6341f357
DN
8194For example if declare A A (.B(SIG)) then B will be included in the list."
8195 (save-excursion
8196 (let ((end-mod-point (point)) ;; presume at /*AUTOINST*/ point
8197 pins pin)
8198 (verilog-backward-open-paren)
8199 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
8200 (setq pin (match-string 1))
9489a450 8201 (unless (verilog-inside-comment-or-string-p)
6341f357
DN
8202 (setq pins (cons (list pin) pins))
8203 (when (looking-at "(")
9489a450 8204 (verilog-forward-sexp-ign-cmt 1))))
6341f357
DN
8205 (vector pins))))
8206
8207(defun verilog-read-arg-pins ()
37ea4b9b 8208 "Return an array of [ pins ] for the current argument declaration at point."
6341f357
DN
8209 (save-excursion
8210 (let ((end-mod-point (point)) ;; presume at /*AUTOARG*/ point
8211 pins pin)
8212 (verilog-backward-open-paren)
8213 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
8214 (setq pin (match-string 1))
9489a450 8215 (unless (verilog-inside-comment-or-string-p)
6341f357
DN
8216 (setq pins (cons (list pin) pins))))
8217 (vector pins))))
8218
8219(defun verilog-read-auto-constants (beg end-mod-point)
8220 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
8221 ;; Insert new
8222 (save-excursion
8223 (let (sig-list tpl-end-pt)
8224 (goto-char beg)
8225 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
8226 (if (not (looking-at "\\s *("))
8227 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
8228 (search-forward "(" end-mod-point)
8229 (setq tpl-end-pt (save-excursion
8230 (backward-char 1)
9489a450 8231 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
6341f357
DN
8232 (backward-char 1)
8233 (point)))
8234 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
8235 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
8236 sig-list)))
8237
a03c2342
WS
8238(defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
8239(make-variable-buffer-local 'verilog-cache-has-lisp)
8240
8241(defun verilog-read-auto-lisp-present ()
8242 "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
6341f357 8243 (save-excursion
9489a450 8244 (goto-char (point-min))
a03c2342
WS
8245 (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
8246
8247(defun verilog-read-auto-lisp (start end)
8248 "Look for and evaluate a AUTO_LISP between START and END.
8249Must call `verilog-read-auto-lisp-present' before this function."
8250 ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
8251 (when verilog-cache-has-lisp
8252 (save-excursion
8253 (goto-char start)
8254 (while (re-search-forward "\\<AUTO_LISP(" end t)
8255 (backward-char)
8256 (let* ((beg-pt (prog1 (point)
9489a450 8257 (verilog-forward-sexp-cmt 1))) ;; Closing paren
a03c2342
WS
8258 (end-pt (point)))
8259 (eval-region beg-pt end-pt nil))))))
6341f357 8260
60618039
DN
8261(eval-when-compile
8262 ;; Prevent compile warnings; these are let's, not globals
8263 ;; Do not remove the eval-when-compile
8264 ;; - we want a error when we are debugging this code if they are refed.
8265 (defvar sigs-in)
9489a450
MM
8266 (defvar sigs-out-d)
8267 (defvar sigs-out-i)
8268 (defvar sigs-out-unk)
a03c2342 8269 (defvar sigs-temp)
60618039 8270 (defvar vector-skip-list))
6341f357
DN
8271
8272(defun verilog-read-always-signals-recurse
a03c2342 8273 (exit-keywd rvalue temp-next)
6341f357
DN
8274 "Recursive routine for parentheses/bracket matching.
8275EXIT-KEYWD is expression to stop at, nil if top level.
8276RVALUE is true if at right hand side of equal.
8277IGNORE-NEXT is true to ignore next token, fake from inside case statement."
8278 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ;; true if after a ; we are looking for rvalue
a03c2342
WS
8279 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
8280 ignore-next)
8281 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
6341f357
DN
8282 (while (not (or (eobp) gotend))
8283 (cond
8284 ((looking-at "//")
8285 (search-forward "\n"))
8286 ((looking-at "/\\*")
8287 (or (search-forward "*/")
8288 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8289 ((looking-at "(\\*")
9489a450
MM
8290 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8291 (forward-char 1)
8292 (or (search-forward "*)")
6341f357
DN
8293 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8294 (t (setq keywd (buffer-substring-no-properties
8295 (point)
8296 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8297 (forward-char 1))
8298 (point)))
8299 sig-last-tolk sig-tolk
8300 sig-tolk nil)
14862301 8301 ;;(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))))
6341f357
DN
8302 (cond
8303 ((equal keywd "\"")
8304 (or (re-search-forward "[^\\]\"" nil t)
8305 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8306 ;; else at top level loop, keep parsing
8307 ((and end-else-check (equal keywd "else"))
8308 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
8309 ;; no forward movement, want to see else in lower loop
8310 (setq end-else-check nil))
8311 ;; End at top level loop
8312 ((and end-else-check (looking-at "[^ \t\n\f]"))
8313 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
8314 (setq gotend t))
8315 ;; Final statement?
8316 ((and exit-keywd (equal keywd exit-keywd))
8317 (setq gotend t)
8318 (forward-char (length keywd)))
8319 ;; Standard tokens...
8320 ((equal keywd ";")
8321 (setq ignore-next nil rvalue semi-rvalue)
8322 ;; Final statement at top level loop?
8323 (when (not exit-keywd)
8324 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
8325 (setq end-else-check t))
8326 (forward-char 1))
8327 ((equal keywd "'")
14862301 8328 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
6341f357
DN
8329 (goto-char (match-end 0))
8330 (forward-char 1)))
8331 ((equal keywd ":") ;; Case statement, begin/end label, x?y:z
8332 (cond ((equal "endcase" exit-keywd) ;; case x: y=z; statement next
8333 (setq ignore-next nil rvalue nil))
8334 ((equal "?" exit-keywd) ;; x?y:z rvalue
8335 ) ;; NOP
a3a8b002
DN
8336 ((equal "]" exit-keywd) ;; [x:y] rvalue
8337 ) ;; NOP
6341f357
DN
8338 (got-sig ;; label: statement
8339 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
8340 ((not rvalue) ;; begin label
8341 (setq ignore-next t rvalue nil)))
8342 (forward-char 1))
8343 ((equal keywd "=")
9489a450
MM
8344 (when got-sig
8345 ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
8346 (set got-list (cons got-sig (symbol-value got-list)))
8347 (setq got-sig nil))
8348 (when (not rvalue)
8349 (if (eq (char-before) ?< )
8350 (setq sigs-out-d (append sigs-out-d sigs-out-unk)
8351 sigs-out-unk nil)
8352 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8353 sigs-out-unk nil)))
6341f357
DN
8354 (setq ignore-next nil rvalue t)
8355 (forward-char 1))
8356 ((equal keywd "?")
8357 (forward-char 1)
8358 (verilog-read-always-signals-recurse ":" rvalue nil))
8359 ((equal keywd "[")
8360 (forward-char 1)
8361 (verilog-read-always-signals-recurse "]" t nil))
8362 ((equal keywd "(")
8363 (forward-char 1)
8364 (cond (sig-last-tolk ;; Function call; zap last signal
8365 (setq got-sig nil)))
8366 (cond ((equal last-keywd "for")
a03c2342
WS
8367 ;; temp-next: Variables on LHS are lvalues, but generally we want
8368 ;; to ignore them, assuming they are loop increments
8369 (verilog-read-always-signals-recurse ";" nil t)
6341f357
DN
8370 (verilog-read-always-signals-recurse ";" t nil)
8371 (verilog-read-always-signals-recurse ")" nil nil))
8372 (t (verilog-read-always-signals-recurse ")" t nil))))
8373 ((equal keywd "begin")
8374 (skip-syntax-forward "w_")
8375 (verilog-read-always-signals-recurse "end" nil nil)
8376 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
8377 (setq ignore-next nil rvalue semi-rvalue)
8378 (if (not exit-keywd) (setq end-else-check t)))
a03c2342 8379 ((member keywd '("case" "casex" "casez"))
6341f357
DN
8380 (skip-syntax-forward "w_")
8381 (verilog-read-always-signals-recurse "endcase" t nil)
8382 (setq ignore-next nil rvalue semi-rvalue)
8383 (if (not exit-keywd) (setq gotend t))) ;; top level begin/end
8384 ((string-match "^[$`a-zA-Z_]" keywd) ;; not exactly word constituent
a03c2342 8385 (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
6341f357
DN
8386 (setq ignore-next t))
8387 ((or ignore-next
8388 (member keywd verilog-keywords)
8389 (string-match "^\\$" keywd)) ;; PLI task
8390 (setq ignore-next nil))
8391 (t
8392 (setq keywd (verilog-symbol-detick-denumber keywd))
8393 (when got-sig
a03c2342
WS
8394 (set got-list (cons got-sig (symbol-value got-list)))
8395 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
6341f357 8396 )
a03c2342
WS
8397 (setq got-list (cond (temp-next 'sigs-temp)
8398 (rvalue 'sigs-in)
9489a450 8399 (t 'sigs-out-unk))
6341f357 8400 got-sig (if (or (not keywd)
a03c2342 8401 (assoc keywd (symbol-value got-list)))
6341f357 8402 nil (list keywd nil nil))
a03c2342 8403 temp-next nil
6341f357
DN
8404 sig-tolk t)))
8405 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8406 (t
8407 (forward-char 1)))
8408 ;; End of non-comment token
60618039 8409 (setq last-keywd keywd)))
6341f357
DN
8410 (skip-syntax-forward " "))
8411 ;; Append the final pending signal
8412 (when got-sig
a03c2342
WS
8413 ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
8414 (set got-list (cons got-sig (symbol-value got-list)))
6341f357
DN
8415 (setq got-sig nil))
8416 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
8417 ))
8418
8419(defun verilog-read-always-signals ()
8420 "Parse always block at point and return list of (outputs inout inputs)."
6341f357
DN
8421 (save-excursion
8422 (let* (;;(dbg "")
9489a450 8423 sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
6341f357
DN
8424 (search-forward ")")
8425 (verilog-read-always-signals-recurse nil nil nil)
9489a450
MM
8426 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8427 sigs-out-unk nil)
14862301 8428 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg ""))
6341f357 8429 ;; Return what was found
9489a450 8430 (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
6341f357
DN
8431
8432(defun verilog-read-instants ()
8433 "Parse module at point and return list of ( ( file instance ) ... )."
9489a450 8434 (verilog-beg-of-defun-quick)
6341f357
DN
8435 (let* ((end-mod-point (verilog-get-end-of-defun t))
8436 (state nil)
8437 (instants-list nil))
8438 (save-excursion
8439 (while (< (point) end-mod-point)
8440 ;; Stay at level 0, no comments
8441 (while (progn
8442 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
8443 (or (> (car state) 0) ; in parens
8444 (nth 5 state) ; comment
8445 ))
8446 (forward-line 1))
8447 (beginning-of-line)
8448 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
8449 ;;(if (looking-at "^\\(.+\\)$")
8450 (let ((module (match-string 1))
8451 (instant (match-string 2)))
8452 (if (not (member module verilog-keywords))
8453 (setq instants-list (cons (list module instant) instants-list)))))
60618039 8454 (forward-line 1)))
6341f357
DN
8455 instants-list))
8456
8457
8458(defun verilog-read-auto-template (module)
8459 "Look for a auto_template for the instantiation of the given MODULE.
8460If found returns the signal name connections. Return REGEXP and
37ea4b9b 8461list of ( (signal_name connection_name)... )."
6341f357
DN
8462 (save-excursion
8463 ;; Find beginning
8464 (let ((tpl-regexp "\\([0-9]+\\)")
9489a450 8465 (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
6341f357 8466 (templateno 0)
a03c2342 8467 (pt (point))
6341f357 8468 tpl-sig-list tpl-wild-list tpl-end-pt rep)
a03c2342
WS
8469 ;; Note this search is expensive, as we hunt from mod-begin to point
8470 ;; for every instantiation. Likewise in verilog-read-auto-lisp.
8471 ;; So, we look first for an exact string rather than a slow regexp.
8472 ;; Someday we may keep a cache of every template, but this would also
8473 ;; need to record the relative position of each AUTOINST, as multiple
8474 ;; templates exist for each module, and we're inserting lines.
6341f357 8475 (cond ((or
a03c2342
WS
8476 (verilog-re-search-backward-substr
8477 "AUTO_TEMPLATE"
8478 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
8479 ;; Also try forward of this AUTOINST
8480 ;; This is for historical support; this isn't speced as working
8481 (progn
8482 (goto-char pt)
8483 (verilog-re-search-forward-substr
8484 "AUTO_TEMPLATE"
8485 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
6341f357
DN
8486 (goto-char (match-end 0))
8487 ;; Parse "REGEXP"
a03c2342
WS
8488 ;; We reserve @"..." for future lisp expressions that evaluate
8489 ;; once-per-AUTOINST
14862301 8490 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
6341f357
DN
8491 (setq tpl-regexp (match-string 1))
8492 (goto-char (match-end 0)))
8493 (search-forward "(")
8494 ;; Parse lines in the template
8495 (when verilog-auto-inst-template-numbers
8496 (save-excursion
9489a450
MM
8497 (let ((pre-pt (point)))
8498 (goto-char (point-min))
8499 (while (search-forward "AUTO_TEMPLATE" pre-pt t)
8500 (setq templateno (1+ templateno)))
8501 (while (< (point) pre-pt)
8502 (forward-line 1)
8503 (setq lineno (1+ lineno))))))
6341f357
DN
8504 (setq tpl-end-pt (save-excursion
8505 (backward-char 1)
9489a450 8506 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
6341f357
DN
8507 (backward-char 1)
8508 (point)))
8509 ;;
8510 (while (< (point) tpl-end-pt)
8511 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
9489a450
MM
8512 (setq tpl-sig-list
8513 (cons (list
8514 (match-string-no-properties 1)
8515 (match-string-no-properties 2)
8516 templateno lineno)
8517 tpl-sig-list))
6341f357
DN
8518 (goto-char (match-end 0)))
8519 ;; Regexp form??
8520 ((looking-at
d63b01e1 8521 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
6341f357
DN
8522 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8523 (setq rep (match-string-no-properties 3))
8524 (goto-char (match-end 0))
8525 (setq tpl-wild-list
8526 (cons (list
8527 (concat "^"
8528 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
8529 (match-string 1))
8530 "$")
8531 rep
8532 templateno lineno)
8533 tpl-wild-list)))
8534 ((looking-at "[ \t\f]+")
8535 (goto-char (match-end 0)))
8536 ((looking-at "\n")
8537 (setq lineno (1+ lineno))
8538 (goto-char (match-end 0)))
8539 ((looking-at "//")
9489a450
MM
8540 (search-forward "\n")
8541 (setq lineno (1+ lineno)))
6341f357
DN
8542 ((looking-at "/\\*")
8543 (forward-char 2)
8544 (or (search-forward "*/")
8545 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8546 (t
8547 (error "%s: AUTO_TEMPLATE parsing error: %s"
8548 (verilog-point-text)
60618039 8549 (progn (looking-at ".*$") (match-string 0))))))
6341f357
DN
8550 ;; Return
8551 (vector tpl-regexp
8552 (list tpl-sig-list tpl-wild-list)))
8553 ;; If no template found
8554 (t (vector tpl-regexp nil))))))
8555;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
8556
8557(defun verilog-set-define (defname defvalue &optional buffer enumname)
8558 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
8559Optionally associate it with the specified enumeration ENUMNAME."
9a529312 8560 (with-current-buffer (or buffer (current-buffer))
6341f357
DN
8561 (let ((mac (intern (concat "vh-" defname))))
8562 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
8563 ;; Need to define to a constant if no value given
14862301 8564 (set (make-local-variable mac)
6341f357
DN
8565 (if (equal defvalue "") "1" defvalue)))
8566 (if enumname
8567 (let ((enumvar (intern (concat "venum-" enumname))))
8568 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
a3a8b002 8569 (unless (boundp enumvar) (set enumvar nil))
175069ef 8570 (add-to-list (make-local-variable enumvar) defname)))))
6341f357
DN
8571
8572(defun verilog-read-defines (&optional filename recurse subcall)
8573 "Read `defines and parameters for the current file, or optional FILENAME.
8574If the filename is provided, `verilog-library-flags' will be used to
8575resolve it. If optional RECURSE is non-nil, recurse through `includes.
8576
8577Parameters must be simple assignments to constants, or have their own
8578\"parameter\" label rather than a list of parameters. Thus:
8579
8580 parameter X = 5, Y = 10; // Ok
8581 parameter X = {1'b1, 2'h2}; // Ok
8582 parameter X = {1'b1, 2'h2}, Y = 10; // Bad, make into 2 parameter lines
8583
8584Defines must be simple text substitutions, one on a line, starting
8585at the beginning of the line. Any ifdefs or multiline comments around the
8586define are ignored.
8587
8588Defines are stored inside Emacs variables using the name vh-{definename}.
8589
8590This function is useful for setting vh-* variables. The file variables
8591feature can be used to set defines that `verilog-mode' can see; put at the
8592*END* of your file something like:
8593
8594 // Local Variables:
8595 // vh-macro:\"macro_definition\"
8596 // End:
8597
8598If macros are defined earlier in the same file and you want their values,
8599you can read them automatically (provided `enable-local-eval' is on):
8600
8601 // Local Variables:
8602 // eval:(verilog-read-defines)
8603 // eval:(verilog-read-defines \"group_standard_includes.v\")
8604 // End:
8605
8606Note these are only read when the file is first visited, you must use
8607\\[find-alternate-file] RET to have these take effect after editing them!
8608
8609If you want to disable the \"Process `eval' or hook local variables\"
8610warning message, you need to add to your .emacs file:
8611
8612 (setq enable-local-eval t)"
8613 (let ((origbuf (current-buffer)))
8614 (save-excursion
8615 (unless subcall (verilog-getopt-flags))
8616 (when filename
8617 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
8618 (if fns
8619 (set-buffer (find-file-noselect (car fns)))
8620 (error (concat (verilog-point-text)
8621 ": Can't find verilog-read-defines file: " filename)))))
8622 (when recurse
8623 (goto-char (point-min))
8624 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
a03c2342
WS
8625 (let ((inc (verilog-string-replace-matches
8626 "\"" "" nil nil (match-string-no-properties 1))))
9489a450 8627 (unless (verilog-inside-comment-or-string-p)
6341f357
DN
8628 (verilog-read-defines inc recurse t)))))
8629 ;; Read `defines
8630 ;; note we don't use verilog-re... it's faster this way, and that
8631 ;; function has problems when comments are at the end of the define
8632 (goto-char (point-min))
8633 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
8634 (let ((defname (match-string-no-properties 1))
8635 (defvalue (match-string-no-properties 2)))
8636 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
8637 (verilog-set-define defname defvalue origbuf)))
8638 ;; Hack: Read parameters
8639 (goto-char (point-min))
8640 (while (re-search-forward
9489a450 8641 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
a3a8b002 8642 (let (enumname)
6341f357
DN
8643 ;; The primary way of getting defines is verilog-read-decls
8644 ;; However, that isn't called yet for included files, so we'll add another scheme
8645 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8646 (setq enumname (match-string-no-properties 1)))
6341f357 8647 (forward-comment 999)
9489a450
MM
8648 (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
8649 "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
a03c2342
WS
8650 (verilog-set-define (match-string-no-properties 1)
8651 (match-string-no-properties 2) origbuf enumname)
6341f357 8652 (goto-char (match-end 0))
60618039 8653 (forward-comment 999)))))))
6341f357
DN
8654
8655(defun verilog-read-includes ()
8656 "Read `includes for the current file.
8657This will find all of the `includes which are at the beginning of lines,
8658ignoring any ifdefs or multiline comments around them.
8659`verilog-read-defines' is then performed on the current and each included
8660file.
8661
8662It is often useful put at the *END* of your file something like:
8663
8664 // Local Variables:
8665 // eval:(verilog-read-defines)
8666 // eval:(verilog-read-includes)
8667 // End:
8668
8669Note includes are only read when the file is first visited, you must use
8670\\[find-alternate-file] RET to have these take effect after editing them!
8671
8672It is good to get in the habit of including all needed files in each .v
8673file that needs it, rather than waiting for compile time. This will aid
8674this process, Verilint, and readability. To prevent defining the same
8675variable over and over when many modules are compiled together, put a test
8676around the inside each include file:
8677
8678foo.v (a include):
8679 `ifdef _FOO_V // include if not already included
8680 `else
8681 `define _FOO_V
8682 ... contents of file
8683 `endif // _FOO_V"
8684;;slow: (verilog-read-defines nil t))
8685 (save-excursion
8686 (verilog-getopt-flags)
8687 (goto-char (point-min))
8688 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
8689 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
8690 (verilog-read-defines inc nil t)))))
8691
8692(defun verilog-read-signals (&optional start end)
8693 "Return a simple list of all possible signals in the file.
8694Bounded by optional region from START to END. Overly aggressive but fast.
37ea4b9b 8695Some macros and such are also found and included. For dinotrace.el."
6341f357
DN
8696 (let (sigs-all keywd)
8697 (progn;save-excursion
8698 (goto-char (or start (point-min)))
8699 (setq end (or end (point-max)))
8700 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
8701 (forward-char -1)
8702 (cond
8703 ((looking-at "//")
8704 (search-forward "\n"))
8705 ((looking-at "/\\*")
8706 (search-forward "*/"))
8707 ((looking-at "(\\*")
8708 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
8709 (search-forward "*)")))
8710 ((eq ?\" (following-char))
8711 (re-search-forward "[^\\]\"")) ;; don't forward-char first, since we look for a non backslash first
8712 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
8713 (goto-char (match-end 0))
8714 (setq keywd (match-string-no-properties 1))
8715 (or (member keywd verilog-keywords)
8716 (member keywd sigs-all)
8717 (setq sigs-all (cons keywd sigs-all))))
60618039 8718 (t (forward-char 1))))
6341f357
DN
8719 ;; Return list
8720 sigs-all)))
8721
8722;;
8723;; Argument file parsing
8724;;
8725
8726(defun verilog-getopt (arglist)
8727 "Parse -f, -v etc arguments in ARGLIST list or string."
8728 (unless (listp arglist) (setq arglist (list arglist)))
8729 (let ((space-args '())
8730 arg next-param)
8731 ;; Split on spaces, so users can pass whole command lines
8732 (while arglist
8733 (setq arg (car arglist)
8734 arglist (cdr arglist))
8735 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
8736 (setq space-args (append space-args
8737 (list (match-string-no-properties 1 arg))))
8738 (setq arg (match-string 2 arg))))
8739 ;; Parse arguments
8740 (while space-args
8741 (setq arg (car space-args)
8742 space-args (cdr space-args))
8743 (cond
8744 ;; Need another arg
8745 ((equal arg "-f")
8746 (setq next-param arg))
8747 ((equal arg "-v")
8748 (setq next-param arg))
8749 ((equal arg "-y")
8750 (setq next-param arg))
8751 ;; +libext+(ext1)+(ext2)...
8752 ((string-match "^\\+libext\\+\\(.*\\)" arg)
8753 (setq arg (match-string 1 arg))
8754 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
8755 (verilog-add-list-unique `verilog-library-extensions
8756 (match-string 1 arg))
8757 (setq arg (match-string 2 arg))))
8758 ;;
8759 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; -Ddefine=val
8760 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ;; -Ddefine
8761 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; +define+val
8762 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ;; +define+define
8763 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
8764 ;;
8765 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ;; +incdir+dir
8766 (string-match "^-I\\(.*\\)" arg)) ;; -Idir
8767 (verilog-add-list-unique `verilog-library-directories
a3a8b002 8768 (match-string 1 (substitute-in-file-name arg))))
6341f357
DN
8769 ;; Ignore
8770 ((equal "+librescan" arg))
8771 ((string-match "^-U\\(.*\\)" arg)) ;; -Udefine
8772 ;; Second parameters
8773 ((equal next-param "-f")
8774 (setq next-param nil)
a3a8b002 8775 (verilog-getopt-file (substitute-in-file-name arg)))
6341f357
DN
8776 ((equal next-param "-v")
8777 (setq next-param nil)
a3a8b002
DN
8778 (verilog-add-list-unique `verilog-library-files
8779 (substitute-in-file-name arg)))
6341f357
DN
8780 ((equal next-param "-y")
8781 (setq next-param nil)
a3a8b002
DN
8782 (verilog-add-list-unique `verilog-library-directories
8783 (substitute-in-file-name arg)))
6341f357
DN
8784 ;; Filename
8785 ((string-match "^[^-+]" arg)
a3a8b002
DN
8786 (verilog-add-list-unique `verilog-library-files
8787 (substitute-in-file-name arg)))
6341f357 8788 ;; Default - ignore; no warning
60618039 8789 ))))
6341f357
DN
8790;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
8791
8792(defun verilog-getopt-file (filename)
37ea4b9b 8793 "Read Verilog options from the specified FILENAME."
6341f357
DN
8794 (save-excursion
8795 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
8796 (orig-buffer (current-buffer))
8797 line)
8798 (if fns
8799 (set-buffer (find-file-noselect (car fns)))
8800 (error (concat (verilog-point-text)
5509c6ad 8801 ": Can't find verilog-getopt-file -f file: " filename)))
6341f357
DN
8802 (goto-char (point-min))
8803 (while (not (eobp))
3ba6b2ee 8804 (setq line (buffer-substring (point) (point-at-eol)))
6341f357
DN
8805 (forward-line 1)
8806 (when (string-match "//" line)
8807 (setq line (substring line 0 (match-beginning 0))))
14862301 8808 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
6341f357
DN
8809 (verilog-getopt line))))))
8810
8811(defun verilog-getopt-flags ()
8812 "Convert `verilog-library-flags' into standard library variables."
8813 ;; If the flags are local, then all the outputs should be local also
8814 (when (local-variable-p `verilog-library-flags (current-buffer))
7ea26faf
DN
8815 (mapc 'make-local-variable '(verilog-library-extensions
8816 verilog-library-directories
8817 verilog-library-files
8818 verilog-library-flags)))
6341f357 8819 ;; Allow user to customize
9489a450 8820 (verilog-run-hooks 'verilog-before-getopt-flags-hook)
6341f357
DN
8821 ;; Process arguments
8822 (verilog-getopt verilog-library-flags)
8823 ;; Allow user to customize
9489a450 8824 (verilog-run-hooks 'verilog-getopt-flags-hook))
6341f357
DN
8825
8826(defun verilog-add-list-unique (varref object)
8827 "Append to VARREF list the given OBJECT,
37ea4b9b 8828unless it is already a member of the variable's list."
6341f357
DN
8829 (unless (member object (symbol-value varref))
8830 (set varref (append (symbol-value varref) (list object))))
8831 varref)
8832;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
8833
a03c2342
WS
8834(defun verilog-current-flags ()
8835 "Convert `verilog-library-flags' and similar variables to command line.
8836Used for __FLAGS__ in `verilog-expand-command'."
8837 (let ((cmd (mapconcat `concat verilog-library-flags " ")))
8838 (when (equal cmd "")
8839 (setq cmd (concat
8840 "+libext+" (mapconcat `concat verilog-library-extensions "+")
8841 (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
8842 verilog-library-directories "")
8843 (mapconcat (lambda (i) (concat " -v " i))
8844 verilog-library-files ""))))
8845 cmd))
8846;;(verilog-current-flags)
8847
6341f357 8848\f
5509c6ad
DN
8849;;
8850;; Cached directory support
8851;;
8852
8853(defvar verilog-dir-cache-preserving nil
8854 "If set, the directory cache is enabled, and file system changes are ignored.
8855See `verilog-dir-exists-p' and `verilog-dir-files'.")
8856
8857;; If adding new cached variable, add also to verilog-preserve-dir-cache
8858(defvar verilog-dir-cache-list nil
8859 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
8860(defvar verilog-dir-cache-lib-filenames nil
8861 "Cached data for `verilog-library-filenames'.")
8862
8863(defmacro verilog-preserve-dir-cache (&rest body)
8864 "Execute the BODY forms, allowing directory cache preservation within BODY.
8865This means that changes inside BODY made to the file system will not be
8866seen by the `verilog-dir-files' and related functions."
a03c2342 8867 `(let ((verilog-dir-cache-preserving (current-buffer))
5509c6ad
DN
8868 verilog-dir-cache-list
8869 verilog-dir-cache-lib-filenames)
8870 (progn ,@body)))
8871
8872(defun verilog-dir-files (dirname)
8873 "Return all filenames in the DIRNAME directory.
8874Relative paths depend on the `default-directory'.
8875Results are cached if inside `verilog-preserve-dir-cache'."
8876 (unless verilog-dir-cache-preserving
8877 (setq verilog-dir-cache-list nil)) ;; Cache disabled
8878 ;; We don't use expand-file-name on the dirname to make key, as it's slow
8879 (let* ((cache-key (list dirname default-directory))
8880 (fass (assoc cache-key verilog-dir-cache-list))
8881 exp-dirname data)
8882 (cond (fass ;; Return data from cache hit
8883 (nth 1 fass))
8884 (t
8885 (setq exp-dirname (expand-file-name dirname)
8886 data (and (file-directory-p exp-dirname)
8887 (directory-files exp-dirname nil nil nil)))
8888 ;; Note we also encache nil for non-existing dirs.
8889 (setq verilog-dir-cache-list (cons (list cache-key data)
8890 verilog-dir-cache-list))
8891 data))))
8892;; Miss-and-hit test:
8893;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
8894;; (prin1 (verilog-dir-files ".")) nil)
8895
8896(defun verilog-dir-file-exists-p (filename)
8897 "Return true if FILENAME exists.
8898Like `file-exists-p' but results are cached if inside
8899`verilog-preserve-dir-cache'."
8900 (let* ((dirname (file-name-directory filename))
8901 ;; Correct for file-name-nondirectory returning same if no slash.
8902 (dirnamed (if (or (not dirname) (equal dirname filename))
8903 default-directory dirname))
8904 (flist (verilog-dir-files dirnamed)))
8905 (and flist
8906 (member (file-name-nondirectory filename) flist)
8907 t)))
8908;;(verilog-dir-file-exists-p "verilog-mode.el")
8909;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
8910
8911\f
6341f357
DN
8912;;
8913;; Module name lookup
8914;;
8915
8916(defun verilog-module-inside-filename-p (module filename)
a03c2342 8917 "Return modi if MODULE is specified inside FILENAME, else nil.
6341f357
DN
8918Allows version control to check out the file if need be."
8919 (and (or (file-exists-p filename)
7ea26faf
DN
8920 (and (fboundp 'vc-backend)
8921 (vc-backend filename)))
a03c2342 8922 (let (modi type)
9a529312 8923 (with-current-buffer (find-file-noselect filename)
14862301
SM
8924 (save-excursion
8925 (goto-char (point-min))
8926 (while (and
8927 ;; It may be tempting to look for verilog-defun-re,
8928 ;; don't, it slows things down a lot!
9489a450 8929 (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
a03c2342 8930 (setq type (match-string-no-properties 0))
14862301
SM
8931 (verilog-re-search-forward-quick "[(;]" nil t))
8932 (if (equal module (verilog-read-module-name))
a03c2342
WS
8933 (setq modi (verilog-modi-new module filename (point) type))))
8934 modi)))))
6341f357
DN
8935
8936(defun verilog-is-number (symbol)
8937 "Return true if SYMBOL is number-like."
8938 (or (string-match "^[0-9 \t:]+$" symbol)
8939 (string-match "^[---]*[0-9]+$" symbol)
60618039 8940 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
6341f357
DN
8941
8942(defun verilog-symbol-detick (symbol wing-it)
37ea4b9b 8943 "Return an expanded SYMBOL name without any defines.
6341f357
DN
8944If the variable vh-{symbol} is defined, return that value.
8945If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
8946 (while (and symbol (string-match "^`" symbol))
8947 (setq symbol (substring symbol 1))
8948 (setq symbol
8949 (if (boundp (intern (concat "vh-" symbol)))
8950 ;; Emacs has a bug where boundp on a buffer-local
8951 ;; variable in only one buffer returns t in another.
8952 ;; This can confuse, so check for nil.
8953 (let ((val (eval (intern (concat "vh-" symbol)))))
8954 (if (eq val nil)
8955 (if wing-it symbol nil)
8956 val))
8957 (if wing-it symbol nil))))
8958 symbol)
8959;;(verilog-symbol-detick "`mod" nil)
8960
8961(defun verilog-symbol-detick-denumber (symbol)
8962 "Return SYMBOL with defines converted and any numbers dropped to nil."
8963 (when (string-match "^`" symbol)
8964 ;; This only will work if the define is a simple signal, not
8965 ;; something like a[b]. Sorry, it should be substituted into the parser
8966 (setq symbol
8967 (verilog-string-replace-matches
8968 "\[[^0-9: \t]+\]" "" nil nil
8969 (or (verilog-symbol-detick symbol nil)
8970 (if verilog-auto-sense-defines-constant
8971 "0"
8972 symbol)))))
8973 (if (verilog-is-number symbol)
8974 nil
8975 symbol))
8976
8977(defun verilog-symbol-detick-text (text)
37ea4b9b 8978 "Return TEXT without any known defines.
6341f357
DN
8979If the variable vh-{symbol} is defined, substitute that value."
8980 (let ((ok t) symbol val)
8981 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
8982 (setq symbol (match-string 1 text))
a3a8b002 8983 ;;(message symbol)
6341f357
DN
8984 (cond ((and
8985 (boundp (intern (concat "vh-" symbol)))
8986 ;; Emacs has a bug where boundp on a buffer-local
8987 ;; variable in only one buffer returns t in another.
8988 ;; This can confuse, so check for nil.
8989 (setq val (eval (intern (concat "vh-" symbol)))))
8990 (setq text (replace-match val nil nil text)))
8991 (t (setq ok nil)))))
8992 text)
8993;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
8994
8995(defun verilog-expand-dirnames (&optional dirnames)
8996 "Return a list of existing directories given a list of wildcarded DIRNAMES.
8997Or, just the existing dirnames themselves if there are no wildcards."
5509c6ad
DN
8998 ;; Note this function is performance critical.
8999 ;; Do not call anything that requires disk access that cannot be cached.
6341f357
DN
9000 (interactive)
9001 (unless dirnames (error "`verilog-library-directories' should include at least '.'"))
9002 (setq dirnames (reverse dirnames)) ; not nreverse
9003 (let ((dirlist nil)
5509c6ad 9004 pattern dirfile dirfiles dirname root filename rest basefile)
6341f357
DN
9005 (while dirnames
9006 (setq dirname (substitute-in-file-name (car dirnames))
9007 dirnames (cdr dirnames))
9008 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ;; root
9009 "\\([^/\\]*[*?][^/\\]*\\)" ;; filename with *?
9010 "\\(.*\\)") ;; rest
9011 dirname)
9012 (setq root (match-string 1 dirname)
9013 filename (match-string 2 dirname)
9014 rest (match-string 3 dirname)
9015 pattern filename)
9016 ;; now replace those * and ? with .+ and .
9017 ;; use ^ and /> to get only whole file names
6341f357
DN
9018 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
9019 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
5509c6ad
DN
9020 pattern (concat "^" pattern "$")
9021 dirfiles (verilog-dir-files root))
6341f357 9022 (while dirfiles
5509c6ad
DN
9023 (setq basefile (car dirfiles)
9024 dirfile (expand-file-name (concat root basefile rest))
6341f357 9025 dirfiles (cdr dirfiles))
5509c6ad
DN
9026 (if (and (string-match pattern basefile)
9027 ;; Don't allow abc/*/rtl to match abc/rtl via ..
9028 (not (equal basefile "."))
9029 (not (equal basefile ".."))
9030 (file-directory-p dirfile))
60618039 9031 (setq dirlist (cons dirfile dirlist)))))
6341f357
DN
9032 ;; Defaults
9033 (t
9034 (if (file-directory-p dirname)
60618039 9035 (setq dirlist (cons dirname dirlist))))))
6341f357
DN
9036 dirlist))
9037;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
9038
0e5c8aed 9039(defun verilog-library-filenames (filename &optional current check-ext)
5509c6ad 9040 "Return a search path to find the given FILENAME or module name.
0e5c8aed
DN
9041Uses the optional CURRENT filename or buffer-file-name, plus
9042`verilog-library-directories' and `verilog-library-extensions'
9043variables to build the path. With optional CHECK-EXT also check
9044`verilog-library-extensions'."
9045 (unless current (setq current (buffer-file-name)))
5509c6ad
DN
9046 (unless verilog-dir-cache-preserving
9047 (setq verilog-dir-cache-lib-filenames nil))
9048 (let* ((cache-key (list filename current check-ext))
9049 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
9050 chkdirs chkdir chkexts fn outlist)
9051 (cond (fass ;; Return data from cache hit
9052 (nth 1 fass))
9053 (t
9054 ;; Note this expand can't be easily cached, as we need to
9055 ;; pick up buffer-local variables for newly read sub-module files
9056 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
9057 (while chkdirs
9058 (setq chkdir (expand-file-name (car chkdirs)
9059 (file-name-directory current))
9060 chkexts (if check-ext verilog-library-extensions `("")))
9061 (while chkexts
9062 (setq fn (expand-file-name (concat filename (car chkexts))
9063 chkdir))
9064 ;;(message "Check for %s" fn)
9065 (if (verilog-dir-file-exists-p fn)
9066 (setq outlist (cons (expand-file-name
9067 fn (file-name-directory current))
9068 outlist)))
9069 (setq chkexts (cdr chkexts)))
9070 (setq chkdirs (cdr chkdirs)))
9071 (setq outlist (nreverse outlist))
9072 (setq verilog-dir-cache-lib-filenames
9073 (cons (list cache-key outlist)
9074 verilog-dir-cache-lib-filenames))
9075 outlist))))
6341f357
DN
9076
9077(defun verilog-module-filenames (module current)
9078 "Return a search path to find the given MODULE name.
9079Uses the CURRENT filename, `verilog-library-extensions',
9080`verilog-library-directories' and `verilog-library-files'
9081variables to build the path."
9082 ;; Return search locations for it
9083 (append (list current) ; first, current buffer
9084 (verilog-library-filenames module current t)
9085 verilog-library-files)) ; finally, any libraries
9086
9087;;
9088;; Module Information
9089;;
9090;; Many of these functions work on "modi" a module information structure
9091;; A modi is: [module-name-string file-name begin-point]
9092
9093(defvar verilog-cache-enabled t
9094 "If true, enable caching of signals, etc. Set to nil for debugging to make things SLOW!")
9095
9096(defvar verilog-modi-cache-list nil
9097 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
9098For speeding up verilog-modi-get-* commands.
9099Buffer-local.")
7ea26faf
DN
9100(make-variable-buffer-local 'verilog-modi-cache-list)
9101
6341f357
DN
9102(defvar verilog-modi-cache-preserve-tick nil
9103 "Modification tick after which the cache is still considered valid.
5509c6ad 9104Use `verilog-preserve-modi-cache' to set it.")
6341f357
DN
9105(defvar verilog-modi-cache-preserve-buffer nil
9106 "Modification tick after which the cache is still considered valid.
5509c6ad 9107Use `verilog-preserve-modi-cache' to set it.")
a03c2342
WS
9108(defvar verilog-modi-cache-current-enable nil
9109 "If true, allow caching `verilog-modi-current', set by let().")
9110(defvar verilog-modi-cache-current nil
9111 "Currently active `verilog-modi-current', if any, set by let().")
9112(defvar verilog-modi-cache-current-max nil
9113 "Current endmodule point for `verilog-modi-cache-current', if any.")
6341f357
DN
9114
9115(defun verilog-modi-current ()
a03c2342
WS
9116 "Return the modi structure for the module currently at point, possibly cached."
9117 (cond ((and verilog-modi-cache-current
9118 (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
9119 (<= (point) verilog-modi-cache-current-max))
9120 ;; Slow assertion, for debugging the cache:
9121 ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
9122 verilog-modi-cache-current)
9123 (verilog-modi-cache-current-enable
9124 (setq verilog-modi-cache-current (verilog-modi-current-get)
9125 verilog-modi-cache-current-max
9126 ;; The cache expires when we pass "endmodule" as then the
9127 ;; current modi may change to the next module
9128 ;; This relies on the AUTOs generally inserting, not deleting text
9129 (save-excursion
9130 (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
9131 verilog-modi-cache-current)
9132 (t
9133 (verilog-modi-current-get))))
9134
9135(defun verilog-modi-current-get ()
6341f357 9136 "Return the modi structure for the module currently at point."
a03c2342 9137 (let* (name type pt)
6341f357
DN
9138 ;; read current module's name
9139 (save-excursion
9140 (verilog-re-search-backward-quick verilog-defun-re nil nil)
a03c2342 9141 (setq type (match-string-no-properties 0))
6341f357
DN
9142 (verilog-re-search-forward-quick "(" nil nil)
9143 (setq name (verilog-read-module-name))
9144 (setq pt (point)))
a03c2342
WS
9145 ;; return modi - note this vector built two places
9146 (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
6341f357 9147
a03c2342
WS
9148(defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
9149(make-variable-buffer-local 'verilog-modi-lookup-cache)
9150(defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
9151(defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
6341f357
DN
9152
9153(defun verilog-modi-lookup (module allow-cache &optional ignore-error)
9154 "Find the file and point at which MODULE is defined.
9155If ALLOW-CACHE is set, check and remember cache of previous lookups.
9156Return modi if successful, else print message unless IGNORE-ERROR is true."
a03c2342
WS
9157 (let* ((current (or (buffer-file-name) (current-buffer)))
9158 modi)
9159 ;; Check cache
9160 ;;(message "verilog-modi-lookup: %s" module)
9161 (cond ((and verilog-modi-lookup-cache
6341f357
DN
9162 verilog-cache-enabled
9163 allow-cache
a03c2342 9164 (setq modi (gethash module verilog-modi-lookup-cache))
6341f357 9165 (equal verilog-modi-lookup-last-current current)
a03c2342
WS
9166 ;; Iff hit is in current buffer, then tick must match
9167 (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
9168 (not (equal current (verilog-modi-file-or-buffer modi)))))
9169 ;;(message "verilog-modi-lookup: HIT %S" modi)
9170 modi)
9171 ;; Miss
6341f357
DN
9172 (t (let* ((realmod (verilog-symbol-detick module t))
9173 (orig-filenames (verilog-module-filenames realmod current))
9174 (filenames orig-filenames)
a03c2342
WS
9175 mif)
9176 (while (and filenames (not mif))
9177 (if (not (setq mif (verilog-module-inside-filename-p realmod (car filenames))))
6341f357 9178 (setq filenames (cdr filenames))))
a03c2342
WS
9179 ;; mif has correct form to become later elements of modi
9180 (cond (mif (setq modi mif))
9181 (t (setq modi nil)
6341f357
DN
9182 (or ignore-error
9183 (error (concat (verilog-point-text)
9184 ": Can't locate " module " module definition"
9185 (if (not (equal module realmod))
9186 (concat " (Expanded macro to " realmod ")")
9187 "")
9188 "\n Check the verilog-library-directories variable."
9189 "\n I looked in (if not listed, doesn't exist):\n\t"
60618039 9190 (mapconcat 'concat orig-filenames "\n\t"))))))
a03c2342
WS
9191 (when (eval-when-compile (fboundp 'make-hash-table))
9192 (unless verilog-modi-lookup-cache
9193 (setq verilog-modi-lookup-cache
9194 (make-hash-table :test 'equal :rehash-size 4.0)))
9195 (puthash module modi verilog-modi-lookup-cache))
9196 (setq verilog-modi-lookup-last-current current
9197 verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
9198 modi))
6341f357
DN
9199
9200(defun verilog-modi-filename (modi)
37ea4b9b 9201 "Filename of MODI, or name of buffer if it's never been saved."
6341f357
DN
9202 (if (bufferp (verilog-modi-file-or-buffer modi))
9203 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
9204 (buffer-name (verilog-modi-file-or-buffer modi)))
9205 (verilog-modi-file-or-buffer modi)))
9206
9207(defun verilog-modi-goto (modi)
9208 "Move point/buffer to specified MODI."
9209 (or modi (error "Passed unfound modi to goto, check earlier"))
9210 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
9211 (verilog-modi-file-or-buffer modi)
9212 (find-file-noselect (verilog-modi-file-or-buffer modi))))
37ea4b9b 9213 (or (equal major-mode `verilog-mode) ;; Put into Verilog mode to get syntax
6341f357 9214 (verilog-mode))
a03c2342 9215 (goto-char (verilog-modi-get-point modi)))
6341f357
DN
9216
9217(defun verilog-goto-defun-file (module)
9218 "Move point to the file at which a given MODULE is defined."
9219 (interactive "sGoto File for Module: ")
9220 (let* ((modi (verilog-modi-lookup module nil)))
9221 (when modi
9222 (verilog-modi-goto modi)
9223 (switch-to-buffer (current-buffer)))))
9224
9225(defun verilog-modi-cache-results (modi function)
9226 "Run on MODI the given FUNCTION. Locate the module in a file.
9227Cache the output of function so next call may have faster access."
5509c6ad
DN
9228 (let (fass)
9229 (save-excursion ;; Cache is buffer-local so can't avoid this.
6341f357 9230 (verilog-modi-goto modi)
5509c6ad 9231 (if (and (setq fass (assoc (list modi function)
6341f357
DN
9232 verilog-modi-cache-list))
9233 ;; Destroy caching when incorrect; Modified or file changed
9234 (not (and verilog-cache-enabled
a03c2342 9235 (or (equal (buffer-chars-modified-tick) (nth 1 fass))
6341f357
DN
9236 (and verilog-modi-cache-preserve-tick
9237 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
9238 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
9239 (equal (visited-file-modtime) (nth 2 fass)))))
9240 (setq verilog-modi-cache-list nil
9241 fass nil))
9242 (cond (fass
5509c6ad
DN
9243 ;; Return data from cache hit
9244 (nth 3 fass))
6341f357
DN
9245 (t
9246 ;; Read from file
86a4c7ac 9247 ;; Clear then restore any highlighting to make emacs19 happy
6341f357
DN
9248 (let ((fontlocked (when (and (boundp 'font-lock-mode)
9249 font-lock-mode)
7cb1c4d7 9250 (font-lock-mode 0)
5509c6ad
DN
9251 t))
9252 func-returns)
6341f357 9253 (setq func-returns (funcall function))
5509c6ad 9254 (when fontlocked (font-lock-mode t))
9c059794
DN
9255 ;; Cache for next time
9256 (setq verilog-modi-cache-list
5509c6ad 9257 (cons (list (list modi function)
a03c2342 9258 (buffer-chars-modified-tick)
9c059794
DN
9259 (visited-file-modtime)
9260 func-returns)
5509c6ad
DN
9261 verilog-modi-cache-list))
9262 func-returns))))))
6341f357
DN
9263
9264(defun verilog-modi-cache-add (modi function element sig-list)
9265 "Add function return results to the module cache.
9266Update MODI's cache for given FUNCTION so that the return ELEMENT of that
9267function now contains the additional SIG-LIST parameters."
9268 (let (fass)
9269 (save-excursion
9270 (verilog-modi-goto modi)
5509c6ad 9271 (if (setq fass (assoc (list modi function)
6341f357
DN
9272 verilog-modi-cache-list))
9273 (let ((func-returns (nth 3 fass)))
9274 (aset func-returns element
9275 (append sig-list (aref func-returns element))))))))
9276
5509c6ad 9277(defmacro verilog-preserve-modi-cache (&rest body)
6341f357
DN
9278 "Execute the BODY forms, allowing cache preservation within BODY.
9279This means that changes to the buffer will not result in the cache being
9280flushed. If the changes affect the modsig state, they must call the
9281modsig-cache-add-* function, else the results of later calls may be
9282incorrect. Without this, changes are assumed to be adding/removing signals
9283and invalidating the cache."
a03c2342 9284 `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
6341f357
DN
9285 (verilog-modi-cache-preserve-buffer (current-buffer)))
9286 (progn ,@body)))
9287
6341f357
DN
9288
9289(defun verilog-signals-matching-enum (in-list enum)
9290 "Return all signals in IN-LIST matching the given ENUM."
9291 (let (out-list)
9292 (while in-list
9293 (if (equal (verilog-sig-enum (car in-list)) enum)
9294 (setq out-list (cons (car in-list) out-list)))
9295 (setq in-list (cdr in-list)))
9296 ;; New scheme
9297 (let* ((enumvar (intern (concat "venum-" enum)))
9298 (enumlist (and (boundp enumvar) (eval enumvar))))
9299 (while enumlist
9300 (add-to-list 'out-list (list (car enumlist)))
9301 (setq enumlist (cdr enumlist))))
9302 (nreverse out-list)))
9303
e2076c2c
DN
9304(defun verilog-signals-matching-regexp (in-list regexp)
9305 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
a3a8b002 9306 (if (or (not regexp) (equal regexp ""))
e2076c2c
DN
9307 in-list
9308 (let (out-list)
9309 (while in-list
9310 (if (string-match regexp (verilog-sig-name (car in-list)))
9311 (setq out-list (cons (car in-list) out-list)))
9312 (setq in-list (cdr in-list)))
9313 (nreverse out-list))))
9314
6341f357
DN
9315(defun verilog-signals-not-matching-regexp (in-list regexp)
9316 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
a3a8b002 9317 (if (or (not regexp) (equal regexp ""))
6341f357
DN
9318 in-list
9319 (let (out-list)
9320 (while in-list
9321 (if (not (string-match regexp (verilog-sig-name (car in-list))))
9322 (setq out-list (cons (car in-list) out-list)))
9323 (setq in-list (cdr in-list)))
9324 (nreverse out-list))))
9325
a3a8b002 9326(defun verilog-signals-matching-dir-re (in-list decl-type regexp)
0e5c8aed 9327 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
a3a8b002
DN
9328if non-nil."
9329 (if (or (not regexp) (equal regexp ""))
9330 in-list
9331 (let (out-list to-match)
9332 (while in-list
9333 ;; Note verilog-insert-one-definition matches on this order
9334 (setq to-match (concat
9335 decl-type
9336 " " (verilog-sig-signed (car in-list))
9337 " " (verilog-sig-multidim (car in-list))
9338 (verilog-sig-bits (car in-list))))
9339 (if (string-match regexp to-match)
9340 (setq out-list (cons (car in-list) out-list)))
9341 (setq in-list (cdr in-list)))
9342 (nreverse out-list))))
9343
6341f357 9344;; Combined
5509c6ad 9345(defun verilog-decls-get-signals (decls)
9489a450 9346 "Return all declared signals, excluding 'assign' statements."
6341f357 9347 (append
5509c6ad
DN
9348 (verilog-decls-get-outputs decls)
9349 (verilog-decls-get-inouts decls)
9350 (verilog-decls-get-inputs decls)
9489a450 9351 (verilog-decls-get-vars decls)
5509c6ad
DN
9352 (verilog-decls-get-consts decls)
9353 (verilog-decls-get-gparams decls)))
9354
9355(defun verilog-decls-get-ports (decls)
6341f357 9356 (append
5509c6ad
DN
9357 (verilog-decls-get-outputs decls)
9358 (verilog-decls-get-inouts decls)
9359 (verilog-decls-get-inputs decls)))
6341f357
DN
9360
9361(defsubst verilog-modi-cache-add-outputs (modi sig-list)
9362 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
9363(defsubst verilog-modi-cache-add-inouts (modi sig-list)
9364 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
9365(defsubst verilog-modi-cache-add-inputs (modi sig-list)
9366 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
9489a450 9367(defsubst verilog-modi-cache-add-vars (modi sig-list)
6341f357 9368 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
6341f357
DN
9369
9370(defun verilog-signals-from-signame (signame-list)
9371 "Return signals in standard form from SIGNAME-LIST, a simple list of signal names."
9372 (mapcar (function (lambda (name) (list name nil nil)))
9373 signame-list))
9374\f
9375;;
9376;; Auto creation utilities
9377;;
9378
495ab0d5
DN
9379(defun verilog-auto-re-search-do (search-for func)
9380 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
6341f357 9381 (goto-char (point-min))
9489a450 9382 (while (verilog-re-search-forward-quick search-for nil t)
495ab0d5 9383 (funcall func)))
6341f357 9384
6341f357 9385(defun verilog-insert-one-definition (sig type indent-pt)
60618039 9386 "Print out a definition for SIG of the given TYPE,
6341f357
DN
9387with appropriate INDENT-PT indentation."
9388 (indent-to indent-pt)
a3a8b002 9389 ;; Note verilog-signals-matching-dir-re matches on this order
6341f357 9390 (insert type)
a3a8b002
DN
9391 (when (verilog-sig-modport sig)
9392 (insert "." (verilog-sig-modport sig)))
6341f357
DN
9393 (when (verilog-sig-signed sig)
9394 (insert " " (verilog-sig-signed sig)))
9395 (when (verilog-sig-multidim sig)
9396 (insert " " (verilog-sig-multidim-string sig)))
9397 (when (verilog-sig-bits sig)
9398 (insert " " (verilog-sig-bits sig)))
9399 (indent-to (max 24 (+ indent-pt 16)))
9400 (unless (= (char-syntax (preceding-char)) ?\ )
9401 (insert " ")) ; Need space between "]name" if indent-to did nothing
8468f78b
WS
9402 (insert (verilog-sig-name sig))
9403 (when (verilog-sig-memory sig)
9404 (insert " " (verilog-sig-memory sig))))
6341f357 9405
9489a450
MM
9406(defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
9407 "Print out a definition for MODI's list of SIGS of the given DIRECTION,
6341f357 9408with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
9489a450
MM
9409format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
9410When MODI is non-null, also add to modi-cache, for tracking."
9411 (when modi
9412 (cond ((equal direction "wire")
9413 (verilog-modi-cache-add-vars modi sigs))
9414 ((equal direction "reg")
9415 (verilog-modi-cache-add-vars modi sigs))
9416 ((equal direction "output")
9417 (verilog-modi-cache-add-outputs modi sigs)
9418 (when verilog-auto-declare-nettype
9419 (verilog-modi-cache-add-vars modi sigs)))
9420 ((equal direction "input")
9421 (verilog-modi-cache-add-inputs modi sigs)
9422 (when verilog-auto-declare-nettype
9423 (verilog-modi-cache-add-vars modi sigs)))
9424 ((equal direction "inout")
9425 (verilog-modi-cache-add-inouts modi sigs)
9426 (when verilog-auto-declare-nettype
9427 (verilog-modi-cache-add-vars modi sigs)))
9428 ((equal direction "interface"))
9429 (t
9430 (error "Unsupported verilog-insert-definition direction: %s" direction))))
6341f357
DN
9431 (or dont-sort
9432 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
9433 (while sigs
9434 (let ((sig (car sigs)))
9435 (verilog-insert-one-definition
9436 sig
9437 ;; Want "type x" or "output type x", not "wire type x"
9489a450
MM
9438 (cond ((or (verilog-sig-type sig)
9439 verilog-auto-wire-type)
6341f357 9440 (concat
9489a450
MM
9441 (when (member direction '("input" "output" "inout"))
9442 (concat direction " "))
9443 (or (verilog-sig-type sig)
9444 verilog-auto-wire-type)))
9445 ((and verilog-auto-declare-nettype
9446 (member direction '("input" "output" "inout")))
9447 (concat direction " " verilog-auto-declare-nettype))
9448 (t
9449 direction))
6341f357
DN
9450 indent-pt)
9451 (insert (if v2k "," ";"))
9452 (if (or (not (verilog-sig-comment sig))
9453 (equal "" (verilog-sig-comment sig)))
9454 (insert "\n")
9455 (indent-to (max 48 (+ indent-pt 40)))
a03c2342 9456 (verilog-insert "// " (verilog-sig-comment sig) "\n"))
6341f357
DN
9457 (setq sigs (cdr sigs)))))
9458
9459(eval-when-compile
9460 (if (not (boundp 'indent-pt))
9461 (defvar indent-pt nil "Local used by insert-indent")))
9462
9463(defun verilog-insert-indent (&rest stuff)
9464 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
9465Presumes that any newlines end a list element."
9466 (let ((need-indent t))
9467 (while stuff
9468 (if need-indent (indent-to indent-pt))
9469 (setq need-indent nil)
a03c2342 9470 (verilog-insert (car stuff))
6341f357
DN
9471 (setq need-indent (string-match "\n$" (car stuff))
9472 stuff (cdr stuff)))))
9473;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
9474
9475(defun verilog-repair-open-comma ()
a3a8b002
DN
9476 "Insert comma if previous argument is other than a open parenthesis or endif."
9477 ;; We can't just search backward for ) as it might be inside another expression.
9478 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
6341f357 9479 (save-excursion
9489a450 9480 (verilog-backward-syntactic-ws-quick)
a3a8b002
DN
9481 (when (and (not (save-excursion ;; Not beginning (, or existing ,
9482 (backward-char 1)
9483 (looking-at "[(,]")))
9484 (not (save-excursion ;; Not `endif, or user define
9485 (backward-char 1)
9486 (skip-chars-backward "[a-zA-Z0-9_`]")
9487 (looking-at "`"))))
9488 (insert ","))))
6341f357
DN
9489
9490(defun verilog-repair-close-comma ()
9491 "If point is at a comma followed by a close parenthesis, fix it.
9492This repairs those mis-inserted by a AUTOARG."
9493 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
9494 (save-excursion
9495 (verilog-forward-close-paren)
9496 (backward-char 1)
9489a450 9497 (verilog-backward-syntactic-ws-quick)
6341f357
DN
9498 (backward-char 1)
9499 (when (looking-at ",")
9500 (delete-char 1))))
9501
9502(defun verilog-get-list (start end)
9503 "Return the elements of a comma separated list between START and END."
9504 (interactive)
9505 (let ((my-list (list))
9506 my-string)
9507 (save-excursion
9508 (while (< (point) end)
9509 (when (re-search-forward "\\([^,{]+\\)" end t)
9510 (setq my-string (verilog-string-remove-spaces (match-string 1)))
9511 (setq my-list (nconc my-list (list my-string) ))
9512 (goto-char (match-end 0))))
9513 my-list)))
9514
9515(defun verilog-make-width-expression (range-exp)
9516 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
9517 ;; strip off the []
9518 (cond ((not range-exp)
9519 "1")
9520 (t
9521 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
9522 (setq range-exp (match-string 1 range-exp)))
9523 (cond ((not range-exp)
9524 "1")
a3a8b002 9525 ;; [#:#] We can compute a numeric result
7ea26faf
DN
9526 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
9527 range-exp)
9528 (int-to-string
9529 (1+ (abs (- (string-to-number (match-string 1 range-exp))
9530 (string-to-number (match-string 2 range-exp)))))))
a3a8b002
DN
9531 ;; [PARAM-1:0] can just return PARAM
9532 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
9533 (match-string 1 range-exp))
9534 ;; [arbitrary] need math
6341f357 9535 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
7ea26faf 9536 (concat "(1+(" (match-string 1 range-exp) ")"
60618039
DN
9537 (if (equal "0" (match-string 2 range-exp))
9538 "" ;; Don't bother with -(0)
6341f357
DN
9539 (concat "-(" (match-string 2 range-exp) ")"))
9540 ")"))
9541 (t nil)))))
9542;;(verilog-make-width-expression "`A:`B")
9543
9489a450
MM
9544(defun verilog-simplify-range-expression (expr)
9545 "Return a simplified range expression with constants eliminated from EXPR."
9546 ;; Note this is always called with brackets; ie [z] or [z:z]
9547 (if (not (string-match "[---+*()]" expr))
9548 expr ;; short-circuit
9549 (let ((out expr)
9550 (last-pass ""))
9551 (while (not (equal last-pass out))
9552 (setq last-pass out)
9553 ;; Prefix regexp needs beginning of match, or some symbol of
9554 ;; lesser or equal precedence. We assume the [:]'s exist in expr.
9555 ;; Ditto the end.
9556 (while (string-match
9557 (concat "\\([[({:*+-]\\)" ; - must be last
9558 "(\\<\\([0-9A-Za-z_]+\\))"
9559 "\\([])}:*+-]\\)")
9560 out)
9561 (setq out (replace-match "\\1\\2\\3" nil nil out)))
9562 ;; For precedence do * before +/-
9563 (while (string-match
9564 (concat "\\([[({:*+-]\\)"
9565 "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
9566 "\\([])}:*+-]\\)")
9567 out)
9568 (setq out (replace-match
9569 (concat (match-string 1 out)
9570 (int-to-string (* (string-to-number (match-string 2 out))
9571 (string-to-number (match-string 4 out))))
9572 (match-string 5 out))
9573 nil nil out)))
9574 (while (string-match
9575 (concat "\\([[({:+-]\\)" ; No * here as higher prec
9576 "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
9577 "\\([])}:+-]\\)")
9578 out)
9579 (let ((pre (match-string 1 out))
9580 (lhs (string-to-number (match-string 2 out)))
9581 (rhs (string-to-number (match-string 4 out)))
9582 (post (match-string 5 out))
9583 val)
9584 (when (equal pre "-")
9585 (setq lhs (- lhs)))
9586 (setq val (if (equal (match-string 3 out) "-")
9587 (- lhs rhs)
9588 (+ lhs rhs))
9589 out (replace-match
9590 (concat (if (and (equal pre "-")
9591 (< val 0))
9592 "" ;; Not "--20" but just "-20"
9593 pre)
9594 (int-to-string val)
9595 post)
9596 nil nil out)) )))
9597 out)))
9598;;(verilog-simplify-range-expression "[1:3]") ;; 1
9599;;(verilog-simplify-range-expression "[(1):3]") ;; 1
9600;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ;;20
9601;;(verilog-simplify-range-expression "[(2*3+6*7)]") ;; 48
9602;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ;; FOO*4-2
9603;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ;; FOO*4+0
9604;;(verilog-simplify-range-expression "[(func(BAR))]") ;; func(BAR)
9605;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ;; FOO-0
4c5e69c6 9606
6341f357
DN
9607(defun verilog-typedef-name-p (variable-name)
9608 "Return true if the VARIABLE-NAME is a type definition."
9609 (when verilog-typedef-regexp
9610 (string-match verilog-typedef-regexp variable-name)))
9611\f
9612;;
9613;; Auto deletion
9614;;
9615
9616(defun verilog-delete-autos-lined ()
9617 "Delete autos that occupy multiple lines, between begin and end comments."
9489a450
MM
9618 ;; The newline must not have a comment property, so we must
9619 ;; delete the end auto's newline, not the first newline
9620 (forward-line 1)
6341f357 9621 (let ((pt (point)))
6341f357
DN
9622 (when (and
9623 (looking-at "\\s-*// Beginning")
9624 (search-forward "// End of automatic" nil t))
9625 ;; End exists
9626 (end-of-line)
9489a450
MM
9627 (forward-line 1)
9628 (delete-region pt (point)))))
6341f357 9629
0e5c8aed
DN
9630(defun verilog-delete-empty-auto-pair ()
9631 "Delete begin/end auto pair at point, if empty."
9632 (forward-line 0)
9633 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
9634 "\\s-*// End of automatics\n"))
9635 (delete-region (point) (save-excursion (forward-line 2) (point)))))
9636
6341f357 9637(defun verilog-forward-close-paren ()
37ea4b9b
JB
9638 "Find the close parenthesis that match the current point.
9639Ignore other close parenthesis with matching open parens."
6341f357
DN
9640 (let ((parens 1))
9641 (while (> parens 0)
9642 (unless (verilog-re-search-forward-quick "[()]" nil t)
9643 (error "%s: Mismatching ()" (verilog-point-text)))
9644 (cond ((= (preceding-char) ?\( )
9645 (setq parens (1+ parens)))
9646 ((= (preceding-char) ?\) )
9647 (setq parens (1- parens)))))))
9648
9649(defun verilog-backward-open-paren ()
37ea4b9b
JB
9650 "Find the open parenthesis that match the current point.
9651Ignore other open parenthesis with matching close parens."
6341f357
DN
9652 (let ((parens 1))
9653 (while (> parens 0)
9654 (unless (verilog-re-search-backward-quick "[()]" nil t)
9655 (error "%s: Mismatching ()" (verilog-point-text)))
9656 (cond ((= (following-char) ?\) )
9657 (setq parens (1+ parens)))
9658 ((= (following-char) ?\( )
9659 (setq parens (1- parens)))))))
9660
9661(defun verilog-backward-open-bracket ()
37ea4b9b
JB
9662 "Find the open bracket that match the current point.
9663Ignore other open bracket with matching close bracket."
6341f357
DN
9664 (let ((parens 1))
9665 (while (> parens 0)
9666 (unless (verilog-re-search-backward-quick "[][]" nil t)
9667 (error "%s: Mismatching []" (verilog-point-text)))
9668 (cond ((= (following-char) ?\] )
9669 (setq parens (1+ parens)))
9670 ((= (following-char) ?\[ )
9671 (setq parens (1- parens)))))))
9672
9673(defun verilog-delete-to-paren ()
9674 "Delete the automatic inst/sense/arg created by autos.
9489a450 9675Deletion stops at the matching end parenthesis, outside comments."
6341f357
DN
9676 (delete-region (point)
9677 (save-excursion
9678 (verilog-backward-open-paren)
9489a450 9679 (verilog-forward-sexp-ign-cmt 1) ;; Moves to paren that closes argdecl's
6341f357
DN
9680 (backward-char 1)
9681 (point))))
9682
9683(defun verilog-auto-star-safe ()
9684 "Return if a .* AUTOINST is safe to delete or expand.
9685It was created by the AUTOS themselves, or by the user."
9686 (and verilog-auto-star-expand
9489a450
MM
9687 (looking-at
9688 (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
6341f357
DN
9689
9690(defun verilog-delete-auto-star-all ()
9691 "Delete a .* AUTOINST, if it is safe."
9692 (when (verilog-auto-star-safe)
9693 (verilog-delete-to-paren)))
9694
9695(defun verilog-delete-auto-star-implicit ()
9696 "Delete all .* implicit connections created by `verilog-auto-star'.
9697This function will be called automatically at save unless
9698`verilog-auto-star-save' is set, any non-templated expanded pins will be
9699removed."
9700 (interactive)
9701 (let (paren-pt indent have-close-paren)
9702 (save-excursion
9703 (goto-char (point-min))
9704 ;; We need to match these even outside of comments.
9705 ;; For reasonable performance, we don't check if inside comments, sorry.
9706 (while (re-search-forward "// Implicit \\.\\*" nil t)
9707 (setq paren-pt (point))
9708 (beginning-of-line)
9709 (setq have-close-paren
9710 (save-excursion
9711 (when (search-forward ");" paren-pt t)
9712 (setq indent (current-indentation))
9713 t)))
9714 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
9715 (when have-close-paren
9716 ;; Delete extra commentary
9717 (save-excursion
9718 (while (progn
9719 (forward-line -1)
9489a450 9720 (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
6341f357
DN
9721 (delete-region (match-beginning 0) (match-end 0))))
9722 ;; If it is simple, we can put the ); on the same line as the last text
9723 (let ((rtn-pt (point)))
9724 (save-excursion
9725 (while (progn (backward-char 1)
9726 (looking-at "[ \t\n\f]")))
9727 (when (looking-at ",")
9728 (delete-region (+ 1 (point)) rtn-pt))))
9729 (when (bolp)
9730 (indent-to indent))
9731 (insert ");\n")
9732 ;; Still need to kill final comma - always is one as we put one after the .*
9733 (re-search-backward ",")
9734 (delete-char 1))))))
9735
9736(defun verilog-delete-auto ()
9737 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
9738Use \\[verilog-auto] to re-insert the updated AUTOs.
9739
9740The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
9741called before and after this function, respectively."
9742 (interactive)
9743 (save-excursion
9744 (if (buffer-file-name)
9745 (find-file-noselect (buffer-file-name))) ;; To check we have latest version
a03c2342
WS
9746 (verilog-save-no-change-functions
9747 (verilog-save-scan-cache
9748 ;; Allow user to customize
9489a450 9749 (verilog-run-hooks 'verilog-before-delete-auto-hook)
a03c2342
WS
9750
9751 ;; Remove those that have multi-line insertions, possibly with parameters
9489a450
MM
9752 ;; We allow anything beginning with AUTO, so that users can add their own
9753 ;; patterns
a03c2342 9754 (verilog-auto-re-search-do
9489a450 9755 (concat "/\\*AUTO[A-Za-z0-9_]+"
a03c2342
WS
9756 ;; Optional parens or quoted parameter or .* for (((...)))
9757 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
9758 "\\*/")
9759 'verilog-delete-autos-lined)
9760 ;; Remove those that are in parenthesis
9761 (verilog-auto-re-search-do
9762 (concat "/\\*"
9763 (eval-when-compile
9764 (verilog-regexp-words
9765 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
9766 "AUTOSENSE")))
9767 "\\*/")
9768 'verilog-delete-to-paren)
9769 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
9770 (verilog-auto-re-search-do "\\.\\*"
9771 'verilog-delete-auto-star-all)
9772 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
9773 (goto-char (point-min))
9489a450 9774 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
a03c2342 9775 (replace-match ""))
6341f357 9776
a03c2342 9777 ;; Final customize
9489a450 9778 (verilog-run-hooks 'verilog-delete-auto-hook)))))
6341f357
DN
9779\f
9780;;
9781;; Auto inject
9782;;
9783
9784(defun verilog-inject-auto ()
9785 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
9786
9787Any always @ blocks with sensitivity lists that match computed lists will
9788be replaced with /*AS*/ comments.
9789
37ea4b9b
JB
9790Any cells will get /*AUTOINST*/ added to the end of the pin list.
9791Pins with have identical names will be deleted.
6341f357
DN
9792
9793Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
9794support adding new ports. You may wish to delete older ports yourself.
9795
9796For example:
9797
1dd4b004 9798 module ExampInject (i, o);
6341f357
DN
9799 input i;
9800 input j;
9801 output o;
9802 always @ (i or j)
9803 o = i | j;
1dd4b004
DN
9804 InstModule instName
9805 (.foobar(baz),
9806 j(j));
6341f357
DN
9807 endmodule
9808
9809Typing \\[verilog-inject-auto] will make this into:
9810
1dd4b004 9811 module ExampInject (i, o/*AUTOARG*/
6341f357
DN
9812 // Inputs
9813 j);
9814 input i;
9815 output o;
9816 always @ (/*AS*/i or j)
9817 o = i | j;
1dd4b004
DN
9818 InstModule instName
9819 (.foobar(baz),
9820 /*AUTOINST*/
9821 // Outputs
9822 j(j));
6341f357
DN
9823 endmodule"
9824 (interactive)
9825 (verilog-auto t))
9826
9827(defun verilog-inject-arg ()
9828 "Inject AUTOARG into new code. See `verilog-inject-auto'."
9829 ;; Presume one module per file.
9830 (save-excursion
9831 (goto-char (point-min))
9832 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
9833 (let ((endmodp (save-excursion
9834 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
9835 (point))))
9836 ;; See if there's already a comment .. inside a comment so not verilog-re-search
9837 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
9838 (verilog-re-search-forward-quick ";" nil t)
9839 (backward-char 1)
9489a450 9840 (verilog-backward-syntactic-ws-quick)
6341f357
DN
9841 (backward-char 1) ; Moves to paren that closes argdecl's
9842 (when (looking-at ")")
a03c2342 9843 (verilog-insert "/*AUTOARG*/")))))))
6341f357
DN
9844
9845(defun verilog-inject-sense ()
9846 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
9847 (save-excursion
9848 (goto-char (point-min))
9849 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
5509c6ad 9850 (let* ((start-pt (point))
9c059794 9851 (modi (verilog-modi-current))
5509c6ad 9852 (moddecls (verilog-modi-get-decls modi))
9c059794
DN
9853 pre-sigs
9854 got-sigs)
6341f357 9855 (backward-char 1)
9489a450 9856 (verilog-forward-sexp-ign-cmt 1)
6341f357 9857 (backward-char 1) ;; End )
9489a450 9858 (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
6341f357
DN
9859 (setq pre-sigs (verilog-signals-from-signame
9860 (verilog-read-signals start-pt (point)))
5509c6ad 9861 got-sigs (verilog-auto-sense-sigs moddecls nil))
6341f357
DN
9862 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
9863 (verilog-signals-not-in got-sigs pre-sigs)))
9864 (delete-region start-pt (point))
a03c2342 9865 (verilog-insert "/*AS*/")))))))
6341f357
DN
9866
9867(defun verilog-inject-inst ()
9868 "Inject AUTOINST into new code. See `verilog-inject-auto'."
9869 (save-excursion
9870 (goto-char (point-min))
9871 ;; It's hard to distinguish modules; we'll instead search for pins.
9872 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_\$]+\\s *(\\s *[a-zA-Z0-9`_\$]+\\s *)" nil t)
9873 (verilog-backward-open-paren) ;; Inst start
9874 (cond
9875 ((= (preceding-char) ?\#) ;; #(...) parameter section, not pin. Skip.
9876 (forward-char 1)
9877 (verilog-forward-close-paren)) ;; Parameters done
9878 (t
9879 (forward-char 1)
9880 (let ((indent-pt (+ (current-column)))
9881 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
9489a450 9882 (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
6341f357
DN
9883 (goto-char end-pt)) ;; Already there, continue search with next instance
9884 (t
9885 ;; Delete identical interconnect
9886 (let ((case-fold-search nil)) ;; So we don't convert upper-to-lower, etc
9489a450 9887 (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_\$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
6341f357
DN
9888 (delete-region (match-beginning 0) (match-end 0))
9889 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ;; Keep it correct
9890 (while (or (looking-at "[ \t\n\f,]+")
9891 (looking-at "//[^\n]*"))
9892 (delete-region (match-beginning 0) (match-end 0))
9893 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
9894 (verilog-forward-close-paren)
9895 (backward-char 1)
9896 ;; Not verilog-re-search, as we don't want to strip comments
9897 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
9898 (delete-region (match-beginning 0) (match-end 0)))
a03c2342
WS
9899 (verilog-insert "\n")
9900 (verilog-insert-indent "/*AUTOINST*/")))))))))
6341f357 9901\f
9489a450
MM
9902;;
9903;; Auto diff
9904;;
9905
9906(defun verilog-diff-buffers-p (b1 b2 &optional whitespace)
9907 "Return nil if buffers B1 and B2 have same contents.
9908Else, return point in B1 that first mismatches.
9909If optional WHITESPACE true, ignore whitespace."
9910 (save-excursion
9911 (let* ((case-fold-search nil) ;; compare-buffer-substrings cares
9912 (p1 (with-current-buffer b1 (goto-char (point-min))))
9913 (p2 (with-current-buffer b2 (goto-char (point-min))))
9914 (maxp1 (with-current-buffer b1 (point-max)))
9915 (maxp2 (with-current-buffer b2 (point-max)))
301b181a 9916 (op1 -1) (op2 -1)
9489a450
MM
9917 progress size)
9918 (while (not (and (eq p1 op1) (eq p2 op2)))
9919 ;; If both windows have whitespace optionally skip over it.
9920 (when whitespace
9921 ;; skip-syntax-* doesn't count \n
9922 (with-current-buffer b1
9923 (goto-char p1)
9924 (skip-chars-forward " \t\n\r\f\v")
9925 (setq p1 (point)))
9926 (with-current-buffer b2
9927 (goto-char p2)
9928 (skip-chars-forward " \t\n\r\f\v")
9929 (setq p2 (point))))
9930 (setq size (min (- maxp1 p1) (- maxp2 p2)))
9931 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
9932 b1 p1 (+ size p1)))
9933 (setq progress (if (zerop progress) size (1- (abs progress))))
9934 (setq op1 p1 op2 p2
9935 p1 (+ p1 progress)
9936 p2 (+ p2 progress)))
9937 ;; Return value
9938 (if (and (eq p1 maxp1) (eq p2 maxp2))
9939 nil p1))))
9940
9941(defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
9942 "View the differences between file F1 and buffer B2.
9943This requires the external program `diff-command' to be in your `exec-path',
9944and uses `diff-switches' in which you may want to have \"-u\" flag.
9945Ignores WHITESPACE if t, and writes output to stdout if SHOW."
9946 ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
9947 ;; call `diff' as `diff' has different calling semantics on different
9948 ;; versions of Emacs.
9949 (if (not (file-exists-p f1))
9950 (message "Buffer %s has no associated file on disc" (buffer-name b2))
9951 (with-temp-buffer "*Verilog-Diff*"
9952 (let ((outbuf (current-buffer))
9953 (f2 (make-temp-file "vm-diff-auto-")))
9954 (unwind-protect
9955 (progn
9956 (with-current-buffer b2
9957 (save-restriction
9958 (widen)
9959 (write-region (point-min) (point-max) f2 nil 'nomessage)))
9960 (call-process diff-command nil outbuf t
9961 diff-switches ;; User may want -u in diff-switches
9962 (if whitespace "-b" "")
9963 f1 f2)
9964 ;; Print out results. Alternatively we could have call-processed
9965 ;; ourself, but this way we can reuse diff switches
9966 (when show
9967 (with-current-buffer outbuf (message "%s" (buffer-string))))))
9968 (sit-for 0)
9969 (when (file-exists-p f2)
9970 (delete-file f2))))))
9971
9972(defun verilog-diff-report (b1 b2 diffpt)
9973 "Report differences detected with `verilog-diff-auto'.
9974Differences are between buffers B1 and B2, starting at point
9975DIFFPT. This function is called via `verilog-diff-function'."
9976 (let ((name1 (with-current-buffer b1 (buffer-file-name))))
9977 (message "%%Warning: %s:%d: Difference in AUTO expansion found"
9978 name1 (with-current-buffer b1 (1+ (count-lines (point-min) (point)))))
9979 (cond (noninteractive
9980 (verilog-diff-file-with-buffer name1 b2 t t))
9981 (t
9982 (ediff-buffers b1 b2)))))
9983
9984(defun verilog-diff-auto ()
9985 "Expand AUTOs in a temporary buffer and indicate any changes.
9986Whitespace differences are ignored to determine identicalness, but
9987once a difference is detected, whitespace differences may be shown.
9988
9989To call this from the command line, see \\[verilog-batch-diff-auto].
9990
9991The action on differences is selected with
9992`verilog-diff-function'. The default is `verilog-diff-report'
9993which will report an error and run `ediff' in interactive mode,
9994or `diff' in batch mode."
9995 (interactive)
9996 (let ((b1 (current-buffer)) b2 diffpt
9997 (name1 (buffer-file-name))
9998 (newname "*Verilog-Diff*"))
9999 (save-excursion
10000 (when (get-buffer newname)
10001 (kill-buffer newname))
10002 (setq b2 (let (buffer-file-name) ;; Else clone is upset
10003 (clone-buffer newname)))
10004 (with-current-buffer b2
10005 ;; auto requires the filename, but can't have same filename in two
10006 ;; buffers; so override both b1 and b2's names
10007 (let ((buffer-file-name name1))
10008 (unwind-protect
10009 (progn
10010 (with-current-buffer b1 (setq buffer-file-name nil))
10011 (verilog-auto)
10012 (when (not verilog-auto-star-save)
10013 (verilog-delete-auto-star-implicit)))
10014 ;; Restore name if unwind
10015 (with-current-buffer b1 (setq buffer-file-name name1)))))
10016 ;;
10017 (setq diffpt (verilog-diff-buffers-p b1 b2 t))
10018 (cond ((not diffpt)
10019 (unless noninteractive (message "AUTO expansion identical"))
10020 (kill-buffer newname)) ;; Nice to cleanup after oneself
10021 (t
10022 (funcall verilog-diff-function b1 b2 diffpt)))
10023 ;; Return result of compare
10024 diffpt)))
10025
10026\f
6341f357
DN
10027;;
10028;; Auto save
10029;;
10030
10031(defun verilog-auto-save-check ()
10032 "On saving see if we need auto update."
10033 (cond ((not verilog-auto-save-policy)) ; disabled
10034 ((not (save-excursion
10035 (save-match-data
10036 (let ((case-fold-search nil))
10037 (goto-char (point-min))
10038 (re-search-forward "AUTO" nil t))))))
10039 ((eq verilog-auto-save-policy 'force)
10040 (verilog-auto))
10041 ((not (buffer-modified-p)))
a03c2342 10042 ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
6341f357
DN
10043 ((eq verilog-auto-save-policy 'detect)
10044 (verilog-auto))
10045 (t
10046 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
10047 (verilog-auto))
10048 ;; Don't ask again if didn't update
a03c2342 10049 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
6341f357
DN
10050 (when (not verilog-auto-star-save)
10051 (verilog-delete-auto-star-implicit))
10052 nil) ;; Always return nil -- we don't write the file ourselves
10053
10054(defun verilog-auto-read-locals ()
10055 "Return file local variable segment at bottom of file."
10056 (save-excursion
10057 (goto-char (point-max))
10058 (if (re-search-backward "Local Variables:" nil t)
10059 (buffer-substring-no-properties (point) (point-max))
10060 "")))
10061
10062(defun verilog-auto-reeval-locals (&optional force)
10063 "Read file local variable segment at bottom of file if it has changed.
10064If FORCE, always reread it."
6341f357
DN
10065 (let ((curlocal (verilog-auto-read-locals)))
10066 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
175069ef 10067 (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
a3a8b002
DN
10068 ;; Note this may cause this function to be recursively invoked,
10069 ;; because hack-local-variables may call (verilog-mode)
6341f357
DN
10070 ;; The above when statement will prevent it from recursing forever.
10071 (hack-local-variables)
10072 t)))
10073\f
10074;;
10075;; Auto creation
10076;;
10077
10078(defun verilog-auto-arg-ports (sigs message indent-pt)
10079 "Print a list of ports for a AUTOINST.
10080Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
10081 (when sigs
a3a8b002
DN
10082 (when verilog-auto-arg-sort
10083 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
6341f357
DN
10084 (insert "\n")
10085 (indent-to indent-pt)
10086 (insert message)
10087 (insert "\n")
10088 (let ((space ""))
10089 (indent-to indent-pt)
10090 (while sigs
10091 (cond ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
10092 (insert "\n")
10093 (indent-to indent-pt))
10094 (t (insert space)))
10095 (insert (verilog-sig-name (car sigs)) ",")
10096 (setq sigs (cdr sigs)
10097 space " ")))))
10098
10099(defun verilog-auto-arg ()
10100 "Expand AUTOARG statements.
10101Replace the argument declarations at the beginning of the
10102module with ones automatically derived from input and output
10103statements. This can be dangerous if the module is instantiated
10104using position-based connections, so use only name-based when
10105instantiating the resulting module. Long lines are split based
10106on the `fill-column', see \\[set-fill-column].
10107
10108Limitations:
301b181a 10109 Concatenation and outputting partial buses is not supported.
6341f357
DN
10110
10111 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10112
10113For example:
10114
1dd4b004 10115 module ExampArg (/*AUTOARG*/);
6341f357
DN
10116 input i;
10117 output o;
10118 endmodule
10119
10120Typing \\[verilog-auto] will make this into:
10121
1dd4b004 10122 module ExampArg (/*AUTOARG*/
6341f357
DN
10123 // Outputs
10124 o,
10125 // Inputs
10126 i
10127 );
10128 input i;
10129 output o;
10130 endmodule
10131
a3a8b002
DN
10132The argument declarations may be printed in declaration order to best suit
10133order based instantiations, or alphabetically, based on the
10134`verilog-auto-arg-sort' variable.
10135
6341f357
DN
10136Any ports declared between the ( and /*AUTOARG*/ are presumed to be
10137predeclared and are not redeclared by AUTOARG. AUTOARG will make a
37ea4b9b
JB
10138conservative guess on adding a comma for the first signal, if you have
10139any ifdefs or complicated expressions before the AUTOARG you will need
10140to choose the comma yourself.
6341f357
DN
10141
10142Avoid declaring ports manually, as it makes code harder to maintain."
10143 (save-excursion
5509c6ad
DN
10144 (let* ((modi (verilog-modi-current))
10145 (moddecls (verilog-modi-get-decls modi))
9c059794 10146 (skip-pins (aref (verilog-read-arg-pins) 0)))
6341f357
DN
10147 (verilog-repair-open-comma)
10148 (verilog-auto-arg-ports (verilog-signals-not-in
5509c6ad 10149 (verilog-decls-get-outputs moddecls)
6341f357
DN
10150 skip-pins)
10151 "// Outputs"
10152 verilog-indent-level-declaration)
10153 (verilog-auto-arg-ports (verilog-signals-not-in
5509c6ad 10154 (verilog-decls-get-inouts moddecls)
6341f357
DN
10155 skip-pins)
10156 "// Inouts"
10157 verilog-indent-level-declaration)
10158 (verilog-auto-arg-ports (verilog-signals-not-in
5509c6ad 10159 (verilog-decls-get-inputs moddecls)
6341f357
DN
10160 skip-pins)
10161 "// Inputs"
10162 verilog-indent-level-declaration)
10163 (verilog-repair-close-comma)
10164 (unless (eq (char-before) ?/ )
10165 (insert "\n"))
60618039 10166 (indent-to verilog-indent-level-declaration))))
6341f357
DN
10167
10168(defun verilog-auto-inst-port-map (port-st)
10169 nil)
10170
6341f357
DN
10171(defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
10172(defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
a3a8b002 10173(defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
6341f357
DN
10174(defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10175(defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
10176(defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
fd9ea9d3
WS
10177(defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10178(defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
6341f357 10179
4c5e69c6 10180(defun verilog-auto-inst-port (port-st indent-pt tpl-list tpl-num for-star par-values)
6341f357
DN
10181 "Print out a instantiation connection for this PORT-ST.
10182Insert to INDENT-PT, use template TPL-LIST.
10183@ are instantiation numbers, replaced with TPL-NUM.
60618039 10184@\"(expression @)\" are evaluated, with @ as a variable.
4c5e69c6
DN
10185If FOR-STAR add comment it is a .* expansion.
10186If PAR-VALUES replace final strings with these parameter values."
6341f357
DN
10187 (let* ((port (verilog-sig-name port-st))
10188 (tpl-ass (or (assoc port (car tpl-list))
10189 (verilog-auto-inst-port-map port-st)))
10190 ;; vl-* are documented for user use
10191 (vl-name (verilog-sig-name port-st))
10192 (vl-width (verilog-sig-width port-st))
a3a8b002 10193 (vl-modport (verilog-sig-modport port-st))
a03c2342 10194 (vl-mbits (if (verilog-sig-multidim port-st)
fd9ea9d3 10195 (verilog-sig-multidim-string port-st) ""))
6341f357
DN
10196 (vl-bits (if (or verilog-auto-inst-vector
10197 (not (assoc port vector-skip-list))
10198 (not (equal (verilog-sig-bits port-st)
10199 (verilog-sig-bits (assoc port vector-skip-list)))))
10200 (or (verilog-sig-bits port-st) "")
10201 ""))
4c5e69c6 10202 (case-fold-search nil)
7cb1c4d7
DN
10203 (check-values par-values)
10204 tpl-net)
4c5e69c6
DN
10205 ;; Replace parameters in bit-width
10206 (when (and check-values
10207 (not (equal vl-bits "")))
10208 (while check-values
10209 (setq vl-bits (verilog-string-replace-matches
10210 (concat "\\<" (nth 0 (car check-values)) "\\>")
10211 (concat "(" (nth 1 (car check-values)) ")")
10212 t t vl-bits)
9489a450
MM
10213 vl-mbits (verilog-string-replace-matches
10214 (concat "\\<" (nth 0 (car check-values)) "\\>")
10215 (concat "(" (nth 1 (car check-values)) ")")
10216 t t vl-mbits)
4c5e69c6 10217 check-values (cdr check-values)))
9489a450
MM
10218 (setq vl-bits (verilog-simplify-range-expression vl-bits)
10219 vl-mbits (verilog-simplify-range-expression vl-mbits)
10220 vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
7cb1c4d7 10221 ;; Default net value if not found
a3a8b002
DN
10222 (setq tpl-net (concat port
10223 (if vl-modport (concat "." vl-modport) "")
10224 (if (verilog-sig-multidim port-st)
9489a450 10225 (concat "/*" vl-mbits vl-bits "*/")
a3a8b002 10226 (concat vl-bits))))
6341f357
DN
10227 ;; Find template
10228 (cond (tpl-ass ; Template of exact port name
10229 (setq tpl-net (nth 1 tpl-ass)))
10230 ((nth 1 tpl-list) ; Wildcards in template, search them
10231 (let ((wildcards (nth 1 tpl-list)))
10232 (while wildcards
10233 (when (string-match (nth 0 (car wildcards)) port)
10234 (setq tpl-ass (car wildcards) ; so allow @ parsing
10235 tpl-net (replace-match (nth 1 (car wildcards))
10236 t nil port)))
10237 (setq wildcards (cdr wildcards))))))
10238 ;; Parse Templated variable
10239 (when tpl-ass
10240 ;; Evaluate @"(lispcode)"
10241 (when (string-match "@\".*[^\\]\"" tpl-net)
10242 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
10243 (setq tpl-net
10244 (concat
10245 (substring tpl-net 0 (match-beginning 0))
10246 (save-match-data
10247 (let* ((expr (match-string 1 tpl-net))
10248 (value
10249 (progn
10250 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
10251 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
10252 (prin1 (eval (car (read-from-string expr)))
10253 (lambda (ch) ())))))
10254 (if (numberp value) (setq value (number-to-string value)))
60618039 10255 value))
6341f357
DN
10256 (substring tpl-net (match-end 0))))))
10257 ;; Replace @ and [] magic variables in final output
10258 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
60618039 10259 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
4c5e69c6 10260 ;; Insert it
6341f357
DN
10261 (indent-to indent-pt)
10262 (insert "." port)
a03c2342
WS
10263 (unless (and verilog-auto-inst-dot-name
10264 (equal port tpl-net))
10265 (indent-to verilog-auto-inst-column)
10266 (insert "(" tpl-net ")"))
10267 (insert ",")
6341f357
DN
10268 (cond (tpl-ass
10269 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10270 verilog-auto-inst-column))
9489a450
MM
10271 ;; verilog-insert requires the complete comment in one call - including the newline
10272 (cond ((equal verilog-auto-inst-template-numbers `lhs)
10273 (verilog-insert " // Templated"
10274 " LHS: " (nth 0 tpl-ass)
10275 "\n"))
10276 (verilog-auto-inst-template-numbers
10277 (verilog-insert " // Templated"
10278 " T" (int-to-string (nth 2 tpl-ass))
10279 " L" (int-to-string (nth 3 tpl-ass))
10280 "\n"))
10281 (t
10282 (verilog-insert " // Templated\n"))))
6341f357
DN
10283 (for-star
10284 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10285 verilog-auto-inst-column))
9489a450
MM
10286 (verilog-insert " // Implicit .\*\n")) ;For some reason the . or * must be escaped...
10287 (t
10288 (insert "\n")))))
6341f357
DN
10289;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
10290;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
10291;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
10292
9489a450
MM
10293(defun verilog-auto-inst-port-list (sig-list indent-pt tpl-list tpl-num for-star par-values)
10294 "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
10295 (when verilog-auto-inst-sort
10296 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
10297 (mapc (lambda (port)
10298 (verilog-auto-inst-port port indent-pt
10299 tpl-list tpl-num for-star par-values))
10300 sig-list))
10301
6341f357
DN
10302(defun verilog-auto-inst-first ()
10303 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
10304 ;; Do we need a trailing comma?
10305 ;; There maybe a ifdef or something similar before us. What a mess. Thus
86a4c7ac 10306 ;; to avoid trouble we only insert on preceding ) or *.
6341f357
DN
10307 ;; Insert first port on new line
10308 (insert "\n") ;; Must insert before search, so point will move forward if insert comma
10309 (save-excursion
9489a450 10310 (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
6341f357
DN
10311 (when (looking-at ")\\|\\*") ;; Generally don't insert, unless we are fairly sure
10312 (forward-char 1)
10313 (insert ","))))
10314
10315(defun verilog-auto-star ()
10316 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
10317
10318If `verilog-auto-star-expand' is set, .* pins are treated if they were
37ea4b9b 10319AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
6341f357
DN
10320will also ignore any .* that are not last in your pin list (this prevents
10321it from deleting pins following the .* when it expands the AUTOINST.)
10322
10323On writing your file, unless `verilog-auto-star-save' is set, any
10324non-templated expanded pins will be removed. You may do this at any time
10325with \\[verilog-delete-auto-star-implicit].
10326
10327If you are converting a module to use .* for the first time, you may wish
10328to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
10329
10330See `verilog-auto-inst' for examples, templates, and more information."
10331 (when (verilog-auto-star-safe)
10332 (verilog-auto-inst)))
10333
10334(defun verilog-auto-inst ()
10335 "Expand AUTOINST statements, as part of \\[verilog-auto].
a03c2342
WS
10336Replace the pin connections to an instantiation or interface
10337declaration with ones automatically derived from the module or
10338interface header of the instantiated item.
6341f357
DN
10339
10340If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
10341and delete them before saving unless `verilog-auto-star-save' is set.
10342See `verilog-auto-star' for more information.
10343
9489a450
MM
10344The pins are printed in declaration order or alphabetically,
10345based on the `verilog-auto-inst-sort' variable.
10346
6341f357
DN
10347Limitations:
10348 Module names must be resolvable to filenames by adding a
10349 `verilog-library-extensions', and being found in the same directory, or
10350 by changing the variable `verilog-library-flags' or
10351 `verilog-library-directories'. Macros `modname are translated through the
10352 vh-{name} Emacs variable, if that is not found, it just ignores the `.
10353
10354 In templates you must have one signal per line, ending in a ), or ));,
10355 and have proper () nesting, including a final ); to end the template.
10356
10357 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10358
37ea4b9b 10359 SystemVerilog multidimensional input/output has only experimental support.
6341f357 10360
a03c2342
WS
10361 SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
10362
4c5e69c6
DN
10363 Parameters referenced by the instantiation will remain symbolic, unless
10364 `verilog-auto-inst-param-value' is set.
10365
a03c2342
WS
10366 Gate primitives (and/or) may have AUTOINST for the purpose of
10367 AUTOWIRE declarations, etc. Gates are the only case when
10368 position based connections are passed.
10369
1dd4b004 10370For example, first take the submodule InstModule.v:
6341f357 10371
a03c2342 10372 module InstModule (o,i);
6341f357
DN
10373 output [31:0] o;
10374 input i;
10375 wire [31:0] o = {32{i}};
10376 endmodule
10377
10378This is then used in a upper level module:
10379
a03c2342 10380 module ExampInst (o,i);
6341f357
DN
10381 output o;
10382 input i;
1dd4b004
DN
10383 InstModule instName
10384 (/*AUTOINST*/);
6341f357
DN
10385 endmodule
10386
10387Typing \\[verilog-auto] will make this into:
10388
a03c2342 10389 module ExampInst (o,i);
6341f357
DN
10390 output o;
10391 input i;
1dd4b004
DN
10392 InstModule instName
10393 (/*AUTOINST*/
10394 // Outputs
10395 .ov (ov[31:0]),
10396 // Inputs
10397 .i (i));
6341f357
DN
10398 endmodule
10399
10400Where the list of inputs and outputs came from the inst module.
10401\f
10402Exceptions:
10403
10404 Unless you are instantiating a module multiple times, or the module is
37ea4b9b 10405 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
6341f357 10406 It just makes for unmaintainable code. To sanitize signal names, try
855b42a2 10407 vrename from URL `http://www.veripool.org'.
6341f357
DN
10408
10409 When you need to violate this suggestion there are two ways to list
10410 exceptions, placing them before the AUTOINST, or using templates.
10411
10412 Any ports defined before the /*AUTOINST*/ are not included in the list of
10413 automatics. This is similar to making a template as described below, but
10414 is restricted to simple connections just like you normally make. Also note
10415 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
10416 you have the appropriate // Input or // Output comment, and exactly the
10417 same line formatting as AUTOINST itself uses.
10418
1dd4b004
DN
10419 InstModule instName
10420 (// Inputs
10421 .i (my_i_dont_mess_with_it),
10422 /*AUTOINST*/
10423 // Outputs
10424 .ov (ov[31:0]));
6341f357
DN
10425
10426\f
10427Templates:
10428
10429 For multiple instantiations based upon a single template, create a
10430 commented out template:
10431
1dd4b004 10432 /* InstModule AUTO_TEMPLATE (
6341f357
DN
10433 .sig3 (sigz[]),
10434 );
10435 */
10436
37ea4b9b 10437 Templates go ABOVE the instantiation(s). When an instantiation is
6341f357
DN
10438 expanded `verilog-mode' simply searches up for the closest template.
10439 Thus you can have multiple templates for the same module, just alternate
37ea4b9b 10440 between the template for an instantiation and the instantiation itself.
6341f357
DN
10441
10442 The module name must be the same as the name of the module in the
10443 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
10444 words and capitalized. Only signals that must be different for each
10445 instantiation need to be listed.
10446
10447 Inside a template, a [] in a connection name (with nothing else inside
10448 the brackets) will be replaced by the same bus subscript as it is being
10449 connected to, or the [] will be removed if it is a single bit signal.
10450 Generally it is a good idea to do this for all connections in a template,
10451 as then they will work for any width signal, and with AUTOWIRE. See
10452 PTL_BUS becoming PTL_BUSNEW below.
10453
10454 If you have a complicated template, set `verilog-auto-inst-template-numbers'
10455 to see which regexps are matching. Don't leave that mode set after
10456 debugging is completed though, it will result in lots of extra differences
10457 and merge conflicts.
10458
10459 For example:
10460
1dd4b004 10461 /* InstModule AUTO_TEMPLATE (
6341f357
DN
10462 .ptl_bus (ptl_busnew[]),
10463 );
10464 */
1dd4b004 10465 InstModule ms2m (/*AUTOINST*/);
6341f357
DN
10466
10467 Typing \\[verilog-auto] will make this into:
10468
1dd4b004 10469 InstModule ms2m (/*AUTOINST*/
6341f357
DN
10470 // Outputs
10471 .NotInTemplate (NotInTemplate),
10472 .ptl_bus (ptl_busnew[3:0]), // Templated
10473 ....
9489a450
MM
10474
10475\f
10476Multiple Module Templates:
10477
10478 The same template lines can be applied to multiple modules with
10479 the syntax as follows:
10480
10481 /* InstModuleA AUTO_TEMPLATE
10482 InstModuleB AUTO_TEMPLATE
10483 InstModuleC AUTO_TEMPLATE
10484 InstModuleD AUTO_TEMPLATE (
10485 .ptl_bus (ptl_busnew[]),
10486 );
10487 */
10488
10489 Note there is only one AUTO_TEMPLATE opening parenthesis.
6341f357
DN
10490\f
10491@ Templates:
10492
10493 It is common to instantiate a cell multiple times, so templates make it
10494 trivial to substitute part of the cell name into the connection name.
10495
1dd4b004 10496 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
6341f357
DN
10497 .sig1 (sigx[@]),
10498 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
10499 );
10500 */
10501
10502 If no regular expression is provided immediately after the AUTO_TEMPLATE
10503 keyword, then the @ character in any connection names will be replaced
10504 with the instantiation number; the first digits found in the cell's
10505 instantiation name.
10506
10507 If a regular expression is provided, the @ character will be replaced
10508 with the first \(\) grouping that matches against the cell name. Using a
10509 regexp of \"\\([0-9]+\\)\" provides identical values for @ as when no
10510 regexp is provided. If you use multiple layers of parenthesis,
10511 \"test\\([^0-9]+\\)_\\([0-9]+\\)\" would replace @ with non-number
10512 characters after test and before _, whereas
10513 \"\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)\" would replace @ with the entire
10514 match.
10515
10516 For example:
10517
1dd4b004 10518 /* InstModule AUTO_TEMPLATE (
6341f357
DN
10519 .ptl_mapvalidx (ptl_mapvalid[@]),
10520 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
10521 );
10522 */
1dd4b004 10523 InstModule ms2m (/*AUTOINST*/);
6341f357
DN
10524
10525 Typing \\[verilog-auto] will make this into:
10526
1dd4b004 10527 InstModule ms2m (/*AUTOINST*/
6341f357
DN
10528 // Outputs
10529 .ptl_mapvalidx (ptl_mapvalid[2]),
10530 .ptl_mapvalidp1x (ptl_mapvalid[3]));
10531
10532 Note the @ character was replaced with the 2 from \"ms2m\".
10533
10534 Alternatively, using a regular expression for @:
10535
1dd4b004 10536 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
6341f357
DN
10537 .ptl_mapvalidx (@_ptl_mapvalid),
10538 .ptl_mapvalidp1x (ptl_mapvalid_@),
10539 );
10540 */
1dd4b004
DN
10541 InstModule ms2_FOO (/*AUTOINST*/);
10542 InstModule ms2_BAR (/*AUTOINST*/);
6341f357
DN
10543
10544 Typing \\[verilog-auto] will make this into:
10545
1dd4b004 10546 InstModule ms2_FOO (/*AUTOINST*/
6341f357
DN
10547 // Outputs
10548 .ptl_mapvalidx (FOO_ptl_mapvalid),
10549 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
1dd4b004 10550 InstModule ms2_BAR (/*AUTOINST*/
6341f357
DN
10551 // Outputs
10552 .ptl_mapvalidx (BAR_ptl_mapvalid),
10553 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
10554
10555\f
10556Regexp Templates:
10557
10558 A template entry of the form
10559
10560 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
10561
37ea4b9b 10562 will apply an Emacs style regular expression search for any port beginning
6341f357
DN
10563 in pci_req followed by numbers and ending in _l and connecting that to
10564 the pci_req_jtag_[] net, with the bus subscript coming from what matches
10565 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
10566
10567 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
0e5c8aed 10568 does the same thing. (Note a @ in the connection/replacement text is
6341f357
DN
10569 completely different -- still use \\1 there!) Thus this is the same as
10570 the above template:
10571
10572 .pci_req@_l (pci_req_jtag_[\\1]),
10573
10574 Here's another example to remove the _l, useful when naming conventions
10575 specify _ alone to mean active low. Note the use of [] to keep the bus
10576 subscript:
10577
10578 .\\(.*\\)_l (\\1_[]),
10579\f
10580Lisp Templates:
10581
10582 First any regular expression template is expanded.
10583
10584 If the syntax @\"( ... )\" is found in a connection, the expression in
10585 quotes will be evaluated as a Lisp expression, with @ replaced by the
10586 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
10587 4 into the brackets. Quote all double-quotes inside the expression with
0e5c8aed
DN
10588 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
10589 regexp template backslash the backslash quote (\\\\\"...\\\\\").
10590
10591 There are special variables defined that are useful in these
10592 Lisp functions:
6341f357 10593
37ea4b9b
JB
10594 vl-name Name portion of the input/output port.
10595 vl-bits Bus bits portion of the input/output port ('[2:0]').
fd9ea9d3 10596 vl-mbits Multidimensional array bits for port ('[2:0][3:0]').
37ea4b9b 10597 vl-width Width of the input/output port ('3' for [2:0]).
6341f357 10598 May be a (...) expression if bits isn't a constant.
a3a8b002
DN
10599 vl-dir Direction of the pin input/output/inout/interface.
10600 vl-modport The modport, if an interface with a modport.
1dd4b004
DN
10601 vl-cell-type Module name/type of the cell ('InstModule').
10602 vl-cell-name Instance name of the cell ('instName').
6341f357
DN
10603
10604 Normal Lisp variables may be used in expressions. See
10605 `verilog-read-defines' which can set vh-{definename} variables for use
10606 here. Also, any comments of the form:
10607
10608 /*AUTO_LISP(setq foo 1)*/
10609
10610 will evaluate any Lisp expression inside the parenthesis between the
10611 beginning of the buffer and the point of the AUTOINST. This allows
10612 functions to be defined or variables to be changed between instantiations.
a3a8b002
DN
10613 (See also `verilog-auto-insert-lisp' if you want the output from your
10614 lisp function to be inserted.)
6341f357
DN
10615
10616 Note that when using lisp expressions errors may occur when @ is not a
37ea4b9b 10617 number; you may need to use the standard Emacs Lisp functions
6341f357
DN
10618 `number-to-string' and `string-to-number'.
10619
10620 After the evaluation is completed, @ substitution and [] substitution
0e5c8aed
DN
10621 occur.
10622
10623For more information see the \\[verilog-faq] and forums at URL
10624`http://www.veripool.org'."
6341f357
DN
10625 (save-excursion
10626 ;; Find beginning
10627 (let* ((pt (point))
10628 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
10629 (indent-pt (save-excursion (verilog-backward-open-paren)
10630 (1+ (current-column))))
10631 (verilog-auto-inst-column (max verilog-auto-inst-column
10632 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
10633 (modi (verilog-modi-current))
5509c6ad 10634 (moddecls (verilog-modi-get-decls modi))
6341f357 10635 (vector-skip-list (unless verilog-auto-inst-vector
5509c6ad
DN
10636 (verilog-decls-get-signals moddecls)))
10637 submod submodi submoddecls
4c5e69c6
DN
10638 inst skip-pins tpl-list tpl-num did-first par-values)
10639
6341f357
DN
10640 ;; Find module name that is instantiated
10641 (setq submod (verilog-read-inst-module)
10642 inst (verilog-read-inst-name)
10643 vl-cell-type submod
10644 vl-cell-name inst
10645 skip-pins (aref (verilog-read-inst-pins) 0))
10646
10647 ;; Parse any AUTO_LISP() before here
10648 (verilog-read-auto-lisp (point-min) pt)
10649
4c5e69c6
DN
10650 ;; Read parameters (after AUTO_LISP)
10651 (setq par-values (and verilog-auto-inst-param-value
10652 (verilog-read-inst-param-value)))
10653
6341f357
DN
10654 ;; Lookup position, etc of submodule
10655 ;; Note this may raise an error
a03c2342
WS
10656 (when (and (not (member submod verilog-gate-keywords))
10657 (setq submodi (verilog-modi-lookup submod t)))
5509c6ad 10658 (setq submoddecls (verilog-modi-get-decls submodi))
6341f357
DN
10659 ;; If there's a number in the instantiation, it may be a argument to the
10660 ;; automatic variable instantiation program.
10661 (let* ((tpl-info (verilog-read-auto-template submod))
10662 (tpl-regexp (aref tpl-info 0)))
10663 (setq tpl-num (if (string-match tpl-regexp inst)
10664 (match-string 1 inst)
10665 "")
10666 tpl-list (aref tpl-info 1)))
10667 ;; Find submodule's signals and dump
a03c2342
WS
10668 (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
10669 (verilog-signals-not-in
9489a450 10670 (verilog-decls-get-vars submoddecls)
a03c2342
WS
10671 skip-pins)))
10672 (vl-dir "interfaced"))
10673 (when sig-list
10674 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
10675 ;; Note these are searched for in verilog-read-sub-decls.
10676 (verilog-insert-indent "// Interfaced\n")
9489a450
MM
10677 (verilog-auto-inst-port-list sig-list indent-pt
10678 tpl-list tpl-num for-star par-values)))
a3a8b002
DN
10679 (let ((sig-list (verilog-signals-not-in
10680 (verilog-decls-get-interfaces submoddecls)
10681 skip-pins))
10682 (vl-dir "interface"))
10683 (when sig-list
10684 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
a3a8b002 10685 ;; Note these are searched for in verilog-read-sub-decls.
a03c2342 10686 (verilog-insert-indent "// Interfaces\n")
9489a450
MM
10687 (verilog-auto-inst-port-list sig-list indent-pt
10688 tpl-list tpl-num for-star par-values)))
6341f357 10689 (let ((sig-list (verilog-signals-not-in
5509c6ad 10690 (verilog-decls-get-outputs submoddecls)
6341f357
DN
10691 skip-pins))
10692 (vl-dir "output"))
10693 (when sig-list
10694 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
a03c2342 10695 (verilog-insert-indent "// Outputs\n")
9489a450
MM
10696 (verilog-auto-inst-port-list sig-list indent-pt
10697 tpl-list tpl-num for-star par-values)))
6341f357 10698 (let ((sig-list (verilog-signals-not-in
5509c6ad 10699 (verilog-decls-get-inouts submoddecls)
6341f357
DN
10700 skip-pins))
10701 (vl-dir "inout"))
10702 (when sig-list
10703 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
a03c2342 10704 (verilog-insert-indent "// Inouts\n")
9489a450
MM
10705 (verilog-auto-inst-port-list sig-list indent-pt
10706 tpl-list tpl-num for-star par-values)))
6341f357 10707 (let ((sig-list (verilog-signals-not-in
5509c6ad 10708 (verilog-decls-get-inputs submoddecls)
6341f357
DN
10709 skip-pins))
10710 (vl-dir "input"))
10711 (when sig-list
10712 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
a03c2342 10713 (verilog-insert-indent "// Inputs\n")
9489a450
MM
10714 (verilog-auto-inst-port-list sig-list indent-pt
10715 tpl-list tpl-num for-star par-values)))
6341f357
DN
10716 ;; Kill extra semi
10717 (save-excursion
10718 (cond (did-first
10719 (re-search-backward "," pt t)
10720 (delete-char 1)
10721 (insert ");")
10722 (search-forward "\n") ;; Added by inst-port
d355a0b7 10723 (delete-char -1)
6341f357 10724 (if (search-forward ")" nil t) ;; From user, moved up a line
d355a0b7 10725 (delete-char -1))
6341f357 10726 (if (search-forward ";" nil t) ;; Don't error if user had syntax error and forgot it
d355a0b7 10727 (delete-char -1)))))))))
6341f357
DN
10728
10729(defun verilog-auto-inst-param ()
10730 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
10731Replace the parameter connections to an instantiation with ones
10732automatically derived from the module header of the instantiated netlist.
10733
10734See \\[verilog-auto-inst] for limitations, and templates to customize the
10735output.
10736
1dd4b004 10737For example, first take the submodule InstModule.v:
6341f357 10738
a03c2342 10739 module InstModule (o,i);
6341f357
DN
10740 parameter PAR;
10741 endmodule
10742
10743This is then used in a upper level module:
10744
a03c2342 10745 module ExampInst (o,i);
6341f357 10746 parameter PAR;
1dd4b004
DN
10747 InstModule #(/*AUTOINSTPARAM*/)
10748 instName (/*AUTOINST*/);
6341f357
DN
10749 endmodule
10750
10751Typing \\[verilog-auto] will make this into:
10752
a03c2342 10753 module ExampInst (o,i);
6341f357
DN
10754 output o;
10755 input i;
1dd4b004
DN
10756 InstModule #(/*AUTOINSTPARAM*/
10757 // Parameters
10758 .PAR (PAR));
10759 instName (/*AUTOINST*/);
6341f357
DN
10760 endmodule
10761
10762Where the list of parameter connections come from the inst module.
10763\f
10764Templates:
10765
10766 You can customize the parameter connections using AUTO_TEMPLATEs,
10767 just as you would with \\[verilog-auto-inst]."
10768 (save-excursion
10769 ;; Find beginning
10770 (let* ((pt (point))
10771 (indent-pt (save-excursion (verilog-backward-open-paren)
10772 (1+ (current-column))))
10773 (verilog-auto-inst-column (max verilog-auto-inst-column
10774 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
10775 (modi (verilog-modi-current))
5509c6ad 10776 (moddecls (verilog-modi-get-decls modi))
6341f357 10777 (vector-skip-list (unless verilog-auto-inst-vector
5509c6ad
DN
10778 (verilog-decls-get-signals moddecls)))
10779 submod submodi submoddecls
10780 inst skip-pins tpl-list tpl-num did-first)
6341f357
DN
10781 ;; Find module name that is instantiated
10782 (setq submod (save-excursion
10783 ;; Get to the point where AUTOINST normally is to read the module
10784 (verilog-re-search-forward-quick "[(;]" nil nil)
10785 (verilog-read-inst-module))
10786 inst (save-excursion
10787 ;; Get to the point where AUTOINST normally is to read the module
10788 (verilog-re-search-forward-quick "[(;]" nil nil)
10789 (verilog-read-inst-name))
10790 vl-cell-type submod
10791 vl-cell-name inst
10792 skip-pins (aref (verilog-read-inst-pins) 0))
10793
10794 ;; Parse any AUTO_LISP() before here
10795 (verilog-read-auto-lisp (point-min) pt)
10796
10797 ;; Lookup position, etc of submodule
10798 ;; Note this may raise an error
10799 (when (setq submodi (verilog-modi-lookup submod t))
5509c6ad 10800 (setq submoddecls (verilog-modi-get-decls submodi))
6341f357
DN
10801 ;; If there's a number in the instantiation, it may be a argument to the
10802 ;; automatic variable instantiation program.
10803 (let* ((tpl-info (verilog-read-auto-template submod))
10804 (tpl-regexp (aref tpl-info 0)))
10805 (setq tpl-num (if (string-match tpl-regexp inst)
10806 (match-string 1 inst)
10807 "")
10808 tpl-list (aref tpl-info 1)))
10809 ;; Find submodule's signals and dump
10810 (let ((sig-list (verilog-signals-not-in
5509c6ad 10811 (verilog-decls-get-gparams submoddecls)
6341f357
DN
10812 skip-pins))
10813 (vl-dir "parameter"))
10814 (when sig-list
10815 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
7ea26faf 10816 ;; Note these are searched for in verilog-read-sub-decls.
a03c2342 10817 (verilog-insert-indent "// Parameters\n")
9489a450
MM
10818 (verilog-auto-inst-port-list sig-list indent-pt
10819 tpl-list tpl-num nil nil)))
6341f357
DN
10820 ;; Kill extra semi
10821 (save-excursion
10822 (cond (did-first
10823 (re-search-backward "," pt t)
10824 (delete-char 1)
10825 (insert ")")
10826 (search-forward "\n") ;; Added by inst-port
d355a0b7 10827 (delete-char -1)
6341f357 10828 (if (search-forward ")" nil t) ;; From user, moved up a line
d355a0b7 10829 (delete-char -1)))))))))
6341f357
DN
10830
10831(defun verilog-auto-reg ()
10832 "Expand AUTOREG statements, as part of \\[verilog-auto].
10833Make reg statements for any output that isn't already declared,
9489a450
MM
10834and isn't a wire output from a block. `verilog-auto-wire-type'
10835may be used to change the datatype of the declarations.
6341f357
DN
10836
10837Limitations:
10838 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
10839
10840 This does NOT work on memories, declare those yourself.
10841
10842An example:
10843
a03c2342 10844 module ExampReg (o,i);
6341f357
DN
10845 output o;
10846 input i;
10847 /*AUTOREG*/
10848 always o = i;
10849 endmodule
10850
10851Typing \\[verilog-auto] will make this into:
10852
a03c2342 10853 module ExampReg (o,i);
6341f357
DN
10854 output o;
10855 input i;
10856 /*AUTOREG*/
10857 // Beginning of automatic regs (for this module's undeclared outputs)
1dd4b004 10858 reg o;
6341f357
DN
10859 // End of automatics
10860 always o = i;
10861 endmodule"
10862 (save-excursion
10863 ;; Point must be at insertion point.
10864 (let* ((indent-pt (current-indentation))
10865 (modi (verilog-modi-current))
5509c6ad
DN
10866 (moddecls (verilog-modi-get-decls modi))
10867 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357 10868 (sig-list (verilog-signals-not-in
5509c6ad 10869 (verilog-decls-get-outputs moddecls)
9489a450 10870 (append (verilog-decls-get-vars moddecls)
5509c6ad
DN
10871 (verilog-decls-get-assigns moddecls)
10872 (verilog-decls-get-consts moddecls)
10873 (verilog-decls-get-gparams moddecls)
a03c2342 10874 (verilog-subdecls-get-interfaced modsubdecls)
5509c6ad
DN
10875 (verilog-subdecls-get-outputs modsubdecls)
10876 (verilog-subdecls-get-inouts modsubdecls)))))
6341f357
DN
10877 (forward-line 1)
10878 (when sig-list
10879 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
9489a450 10880 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
60618039 10881 (verilog-insert-indent "// End of automatics\n")))))
6341f357
DN
10882
10883(defun verilog-auto-reg-input ()
10884 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
10885Make reg statements instantiation inputs that aren't already declared.
10886This is useful for making a top level shell for testing the module that is
10887to be instantiated.
10888
10889Limitations:
10890 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
10891
10892 This does NOT work on memories, declare those yourself.
10893
10894An example (see `verilog-auto-inst' for what else is going on here):
10895
a03c2342 10896 module ExampRegInput (o,i);
6341f357
DN
10897 output o;
10898 input i;
10899 /*AUTOREGINPUT*/
1dd4b004
DN
10900 InstModule instName
10901 (/*AUTOINST*/);
6341f357
DN
10902 endmodule
10903
10904Typing \\[verilog-auto] will make this into:
10905
a03c2342 10906 module ExampRegInput (o,i);
6341f357
DN
10907 output o;
10908 input i;
10909 /*AUTOREGINPUT*/
10910 // Beginning of automatic reg inputs (for undeclared ...
1dd4b004 10911 reg [31:0] iv; // From inst of inst.v
6341f357 10912 // End of automatics
1dd4b004
DN
10913 InstModule instName
10914 (/*AUTOINST*/
10915 // Outputs
10916 .o (o[31:0]),
10917 // Inputs
10918 .iv (iv));
6341f357
DN
10919 endmodule"
10920 (save-excursion
10921 ;; Point must be at insertion point.
10922 (let* ((indent-pt (current-indentation))
10923 (modi (verilog-modi-current))
5509c6ad
DN
10924 (moddecls (verilog-modi-get-decls modi))
10925 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357
DN
10926 (sig-list (verilog-signals-combine-bus
10927 (verilog-signals-not-in
5509c6ad
DN
10928 (append (verilog-subdecls-get-inputs modsubdecls)
10929 (verilog-subdecls-get-inouts modsubdecls))
9489a450
MM
10930 (append (verilog-decls-get-signals moddecls)
10931 (verilog-decls-get-assigns moddecls))))))
6341f357
DN
10932 (forward-line 1)
10933 (when sig-list
10934 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
9489a450 10935 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
60618039 10936 (verilog-insert-indent "// End of automatics\n")))))
6341f357 10937
9489a450
MM
10938(defun verilog-auto-logic ()
10939 "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
10940Make wire statements using the SystemVerilog logic keyword.
99d99081 10941This is currently equivalent to:
9489a450
MM
10942
10943 /*AUTOWIRE*/
10944
10945with the below at the bottom of the file
10946
10947 // Local Variables:
10948 // verilog-auto-logic-type:\"logic\"
10949 // End:
10950
10951In the future AUTOLOGIC may declare additional identifiers,
10952while AUTOWIRE will not."
10953 (save-excursion
10954 (unless verilog-auto-wire-type
10955 (set (make-local-variable 'verilog-auto-wire-type)
10956 "logic"))
10957 (verilog-auto-wire)))
10958
6341f357
DN
10959(defun verilog-auto-wire ()
10960 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
10961Make wire statements for instantiations outputs that aren't
9489a450
MM
10962already declared. `verilog-auto-wire-type' may be used to change
10963the datatype of the declarations.
6341f357
DN
10964
10965Limitations:
10966 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
301b181a 10967 and all buses must have widths, such as those from AUTOINST, or using []
6341f357
DN
10968 in AUTO_TEMPLATEs.
10969
10970 This does NOT work on memories or SystemVerilog .name connections,
10971 declare those yourself.
10972
37ea4b9b
JB
10973 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
10974 determine how to bus together. This occurs when you have ports with
10975 non-numeric or non-sequential bus subscripts. If Verilog mode
6341f357
DN
10976 mis-guessed, you'll have to declare them yourself.
10977
10978An example (see `verilog-auto-inst' for what else is going on here):
10979
a03c2342 10980 module ExampWire (o,i);
6341f357
DN
10981 output o;
10982 input i;
10983 /*AUTOWIRE*/
1dd4b004
DN
10984 InstModule instName
10985 (/*AUTOINST*/);
6341f357
DN
10986 endmodule
10987
10988Typing \\[verilog-auto] will make this into:
10989
a03c2342 10990 module ExampWire (o,i);
6341f357
DN
10991 output o;
10992 input i;
10993 /*AUTOWIRE*/
10994 // Beginning of automatic wires
10995 wire [31:0] ov; // From inst of inst.v
10996 // End of automatics
1dd4b004
DN
10997 InstModule instName
10998 (/*AUTOINST*/
10999 // Outputs
11000 .ov (ov[31:0]),
11001 // Inputs
11002 .i (i));
6341f357
DN
11003 wire o = | ov;
11004 endmodule"
11005 (save-excursion
11006 ;; Point must be at insertion point.
11007 (let* ((indent-pt (current-indentation))
11008 (modi (verilog-modi-current))
5509c6ad
DN
11009 (moddecls (verilog-modi-get-decls modi))
11010 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357
DN
11011 (sig-list (verilog-signals-combine-bus
11012 (verilog-signals-not-in
5509c6ad
DN
11013 (append (verilog-subdecls-get-outputs modsubdecls)
11014 (verilog-subdecls-get-inouts modsubdecls))
11015 (verilog-decls-get-signals moddecls)))))
6341f357
DN
11016 (forward-line 1)
11017 (when sig-list
11018 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
9489a450 11019 (verilog-insert-definition modi sig-list "wire" indent-pt nil)
6341f357 11020 (verilog-insert-indent "// End of automatics\n")
9489a450
MM
11021 ;; We used to optionally call verilog-pretty-declarations and
11022 ;; verilog-pretty-expr here, but it's too slow on huge modules,
11023 ;; plus makes everyone's module change. Finally those call
11024 ;; syntax-ppss which is broken when change hooks are disabled.
11025 ))))
6341f357 11026
e2076c2c 11027(defun verilog-auto-output (&optional with-params)
6341f357
DN
11028 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
11029Make output statements for any output signal from an /*AUTOINST*/ that
11030isn't a input to another AUTOINST. This is useful for modules which
11031only instantiate other modules.
11032
11033Limitations:
11034 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11035
11036 If placed inside the parenthesis of a module declaration, it creates
11037 Verilog 2001 style, else uses Verilog 1995 style.
11038
11039 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11040 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
11041
11042 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11043
11044 Signals matching `verilog-auto-output-ignore-regexp' are not included.
11045
11046An example (see `verilog-auto-inst' for what else is going on here):
11047
a03c2342 11048 module ExampOutput (ov,i);
6341f357
DN
11049 input i;
11050 /*AUTOOUTPUT*/
1dd4b004
DN
11051 InstModule instName
11052 (/*AUTOINST*/);
6341f357
DN
11053 endmodule
11054
11055Typing \\[verilog-auto] will make this into:
11056
a03c2342 11057 module ExampOutput (ov,i);
6341f357
DN
11058 input i;
11059 /*AUTOOUTPUT*/
11060 // Beginning of automatic outputs (from unused autoinst outputs)
1dd4b004 11061 output [31:0] ov; // From inst of inst.v
6341f357 11062 // End of automatics
1dd4b004
DN
11063 InstModule instName
11064 (/*AUTOINST*/
11065 // Outputs
11066 .ov (ov[31:0]),
11067 // Inputs
11068 .i (i));
e2076c2c
DN
11069 endmodule
11070
11071You may also provide an optional regular expression, in which case only
11072signals matching the regular expression will be included. For example the
11073same expansion will result from only extracting outputs starting with ov:
11074
11075 /*AUTOOUTPUT(\"^ov\")*/"
6341f357
DN
11076 (save-excursion
11077 ;; Point must be at insertion point.
11078 (let* ((indent-pt (current-indentation))
e2076c2c
DN
11079 (regexp (and with-params
11080 (nth 0 (verilog-read-auto-params 1))))
9489a450 11081 (v2k (verilog-in-paren-quick))
6341f357 11082 (modi (verilog-modi-current))
5509c6ad
DN
11083 (moddecls (verilog-modi-get-decls modi))
11084 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357 11085 (sig-list (verilog-signals-not-in
5509c6ad
DN
11086 (verilog-subdecls-get-outputs modsubdecls)
11087 (append (verilog-decls-get-outputs moddecls)
11088 (verilog-decls-get-inouts moddecls)
11089 (verilog-subdecls-get-inputs modsubdecls)
11090 (verilog-subdecls-get-inouts modsubdecls)))))
e2076c2c
DN
11091 (when regexp
11092 (setq sig-list (verilog-signals-matching-regexp
11093 sig-list regexp)))
6341f357
DN
11094 (setq sig-list (verilog-signals-not-matching-regexp
11095 sig-list verilog-auto-output-ignore-regexp))
11096 (forward-line 1)
11097 (when v2k (verilog-repair-open-comma))
11098 (when sig-list
11099 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
9489a450 11100 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
6341f357 11101 (verilog-insert-indent "// End of automatics\n"))
60618039 11102 (when v2k (verilog-repair-close-comma)))))
6341f357
DN
11103
11104(defun verilog-auto-output-every ()
11105 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
11106Make output statements for any signals that aren't primary inputs or
11107outputs already. This makes every signal in the design a output. This is
11108useful to get Synopsys to preserve every signal in the design, since it
11109won't optimize away the outputs.
11110
11111An example:
11112
a03c2342 11113 module ExampOutputEvery (o,i,tempa,tempb);
6341f357
DN
11114 output o;
11115 input i;
11116 /*AUTOOUTPUTEVERY*/
11117 wire tempa = i;
11118 wire tempb = tempa;
11119 wire o = tempb;
11120 endmodule
11121
11122Typing \\[verilog-auto] will make this into:
11123
a03c2342 11124 module ExampOutputEvery (o,i,tempa,tempb);
6341f357
DN
11125 output o;
11126 input i;
11127 /*AUTOOUTPUTEVERY*/
11128 // Beginning of automatic outputs (every signal)
1dd4b004
DN
11129 output tempb;
11130 output tempa;
6341f357
DN
11131 // End of automatics
11132 wire tempa = i;
11133 wire tempb = tempa;
11134 wire o = tempb;
11135 endmodule"
11136 (save-excursion
11137 ;;Point must be at insertion point
11138 (let* ((indent-pt (current-indentation))
9489a450 11139 (v2k (verilog-in-paren-quick))
6341f357 11140 (modi (verilog-modi-current))
5509c6ad 11141 (moddecls (verilog-modi-get-decls modi))
6341f357
DN
11142 (sig-list (verilog-signals-combine-bus
11143 (verilog-signals-not-in
5509c6ad
DN
11144 (verilog-decls-get-signals moddecls)
11145 (verilog-decls-get-ports moddecls)))))
6341f357
DN
11146 (forward-line 1)
11147 (when v2k (verilog-repair-open-comma))
11148 (when sig-list
11149 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
9489a450 11150 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
6341f357 11151 (verilog-insert-indent "// End of automatics\n"))
60618039 11152 (when v2k (verilog-repair-close-comma)))))
6341f357 11153
e2076c2c 11154(defun verilog-auto-input (&optional with-params)
6341f357
DN
11155 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
11156Make input statements for any input signal into an /*AUTOINST*/ that
11157isn't declared elsewhere inside the module. This is useful for modules which
11158only instantiate other modules.
11159
11160Limitations:
11161 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11162
11163 If placed inside the parenthesis of a module declaration, it creates
11164 Verilog 2001 style, else uses Verilog 1995 style.
11165
11166 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11167 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
11168
11169 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11170
11171 Signals matching `verilog-auto-input-ignore-regexp' are not included.
11172
11173An example (see `verilog-auto-inst' for what else is going on here):
11174
a03c2342 11175 module ExampInput (ov,i);
6341f357
DN
11176 output [31:0] ov;
11177 /*AUTOINPUT*/
1dd4b004
DN
11178 InstModule instName
11179 (/*AUTOINST*/);
6341f357
DN
11180 endmodule
11181
11182Typing \\[verilog-auto] will make this into:
11183
a03c2342 11184 module ExampInput (ov,i);
6341f357
DN
11185 output [31:0] ov;
11186 /*AUTOINPUT*/
11187 // Beginning of automatic inputs (from unused autoinst inputs)
1dd4b004 11188 input i; // From inst of inst.v
6341f357 11189 // End of automatics
1dd4b004
DN
11190 InstModule instName
11191 (/*AUTOINST*/
11192 // Outputs
11193 .ov (ov[31:0]),
11194 // Inputs
11195 .i (i));
e2076c2c
DN
11196 endmodule
11197
11198You may also provide an optional regular expression, in which case only
11199signals matching the regular expression will be included. For example the
11200same expansion will result from only extracting inputs starting with i:
11201
11202 /*AUTOINPUT(\"^i\")*/"
6341f357
DN
11203 (save-excursion
11204 (let* ((indent-pt (current-indentation))
e2076c2c
DN
11205 (regexp (and with-params
11206 (nth 0 (verilog-read-auto-params 1))))
9489a450 11207 (v2k (verilog-in-paren-quick))
6341f357 11208 (modi (verilog-modi-current))
5509c6ad
DN
11209 (moddecls (verilog-modi-get-decls modi))
11210 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357 11211 (sig-list (verilog-signals-not-in
5509c6ad
DN
11212 (verilog-subdecls-get-inputs modsubdecls)
11213 (append (verilog-decls-get-inputs moddecls)
11214 (verilog-decls-get-inouts moddecls)
9489a450 11215 (verilog-decls-get-vars moddecls)
5509c6ad
DN
11216 (verilog-decls-get-consts moddecls)
11217 (verilog-decls-get-gparams moddecls)
a03c2342 11218 (verilog-subdecls-get-interfaced modsubdecls)
5509c6ad
DN
11219 (verilog-subdecls-get-outputs modsubdecls)
11220 (verilog-subdecls-get-inouts modsubdecls)))))
e2076c2c
DN
11221 (when regexp
11222 (setq sig-list (verilog-signals-matching-regexp
11223 sig-list regexp)))
6341f357
DN
11224 (setq sig-list (verilog-signals-not-matching-regexp
11225 sig-list verilog-auto-input-ignore-regexp))
11226 (forward-line 1)
11227 (when v2k (verilog-repair-open-comma))
11228 (when sig-list
11229 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
9489a450 11230 (verilog-insert-definition modi sig-list "input" indent-pt v2k)
6341f357 11231 (verilog-insert-indent "// End of automatics\n"))
60618039 11232 (when v2k (verilog-repair-close-comma)))))
6341f357 11233
e2076c2c 11234(defun verilog-auto-inout (&optional with-params)
6341f357
DN
11235 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
11236Make inout statements for any inout signal in an /*AUTOINST*/ that
11237isn't declared elsewhere inside the module.
11238
11239Limitations:
11240 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11241
11242 If placed inside the parenthesis of a module declaration, it creates
11243 Verilog 2001 style, else uses Verilog 1995 style.
11244
11245 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11246 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
11247
11248 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11249
11250 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
11251
11252An example (see `verilog-auto-inst' for what else is going on here):
11253
a03c2342 11254 module ExampInout (ov,i);
6341f357
DN
11255 input i;
11256 /*AUTOINOUT*/
1dd4b004
DN
11257 InstModule instName
11258 (/*AUTOINST*/);
6341f357
DN
11259 endmodule
11260
11261Typing \\[verilog-auto] will make this into:
11262
a03c2342 11263 module ExampInout (ov,i);
6341f357
DN
11264 input i;
11265 /*AUTOINOUT*/
11266 // Beginning of automatic inouts (from unused autoinst inouts)
1dd4b004 11267 inout [31:0] ov; // From inst of inst.v
6341f357 11268 // End of automatics
1dd4b004
DN
11269 InstModule instName
11270 (/*AUTOINST*/
11271 // Inouts
11272 .ov (ov[31:0]),
11273 // Inputs
11274 .i (i));
e2076c2c
DN
11275 endmodule
11276
11277You may also provide an optional regular expression, in which case only
11278signals matching the regular expression will be included. For example the
11279same expansion will result from only extracting inouts starting with i:
11280
11281 /*AUTOINOUT(\"^i\")*/"
6341f357
DN
11282 (save-excursion
11283 ;; Point must be at insertion point.
11284 (let* ((indent-pt (current-indentation))
e2076c2c
DN
11285 (regexp (and with-params
11286 (nth 0 (verilog-read-auto-params 1))))
9489a450 11287 (v2k (verilog-in-paren-quick))
6341f357 11288 (modi (verilog-modi-current))
5509c6ad
DN
11289 (moddecls (verilog-modi-get-decls modi))
11290 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357 11291 (sig-list (verilog-signals-not-in
5509c6ad
DN
11292 (verilog-subdecls-get-inouts modsubdecls)
11293 (append (verilog-decls-get-outputs moddecls)
11294 (verilog-decls-get-inouts moddecls)
11295 (verilog-decls-get-inputs moddecls)
11296 (verilog-subdecls-get-inputs modsubdecls)
11297 (verilog-subdecls-get-outputs modsubdecls)))))
e2076c2c
DN
11298 (when regexp
11299 (setq sig-list (verilog-signals-matching-regexp
11300 sig-list regexp)))
6341f357
DN
11301 (setq sig-list (verilog-signals-not-matching-regexp
11302 sig-list verilog-auto-inout-ignore-regexp))
11303 (forward-line 1)
11304 (when v2k (verilog-repair-open-comma))
11305 (when sig-list
11306 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
9489a450 11307 (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
6341f357 11308 (verilog-insert-indent "// End of automatics\n"))
60618039 11309 (when v2k (verilog-repair-close-comma)))))
6341f357 11310
9489a450 11311(defun verilog-auto-inout-module (&optional complement all-in)
6341f357
DN
11312 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
11313Take input/output/inout statements from the specified module and insert
11314into the current module. This is useful for making null templates and
37ea4b9b
JB
11315shell modules which need to have identical I/O with another module.
11316Any I/O which are already defined in this module will not be redefined.
9489a450
MM
11317For the complement of this function, see `verilog-auto-inout-comp',
11318and to make monitors with all inputs, see `verilog-auto-inout-in'.
6341f357
DN
11319
11320Limitations:
11321 If placed inside the parenthesis of a module declaration, it creates
11322 Verilog 2001 style, else uses Verilog 1995 style.
11323
301b181a 11324 Concatenation and outputting partial buses is not supported.
6341f357
DN
11325
11326 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11327
11328 Signals are not inserted in the same order as in the original module,
11329 though they will appear to be in the same order to a AUTOINST
11330 instantiating either module.
11331
11332An example:
11333
a03c2342 11334 module ExampShell (/*AUTOARG*/);
1dd4b004 11335 /*AUTOINOUTMODULE(\"ExampMain\")*/
6341f357
DN
11336 endmodule
11337
a03c2342 11338 module ExampMain (i,o,io);
6341f357
DN
11339 input i;
11340 output o;
11341 inout io;
11342 endmodule
11343
11344Typing \\[verilog-auto] will make this into:
11345
a03c2342 11346 module ExampShell (/*AUTOARG*/i,o,io);
1dd4b004 11347 /*AUTOINOUTMODULE(\"ExampMain\")*/
6341f357 11348 // Beginning of automatic in/out/inouts (from specific module)
6341f357
DN
11349 output o;
11350 inout io;
7cb1c4d7 11351 input i;
6341f357 11352 // End of automatics
1dd4b004
DN
11353 endmodule
11354
11355You may also provide an optional regular expression, in which case only
11356signals matching the regular expression will be included. For example the
11357same expansion will result from only extracting signals starting with i:
11358
a3a8b002
DN
11359 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
11360
14862301 11361You may also provide an optional second regular expression, in
a3a8b002
DN
11362which case only signals which have that pin direction and data
11363type will be included. This matches against everything before
11364the signal name in the declaration, for example against
11365\"input\" (single bit), \"output logic\" (direction and type) or
11366\"output [1:0]\" (direction and implicit type). You also
11367probably want to skip spaces in your regexp.
11368
11369For example, the below will result in matching the output \"o\"
11370against the previous example's module:
11371
11372 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/"
6341f357 11373 (save-excursion
a3a8b002 11374 (let* ((params (verilog-read-auto-params 1 3))
1dd4b004
DN
11375 (submod (nth 0 params))
11376 (regexp (nth 1 params))
a3a8b002 11377 (direction-re (nth 2 params))
1dd4b004 11378 submodi)
6341f357
DN
11379 ;; Lookup position, etc of co-module
11380 ;; Note this may raise an error
11381 (when (setq submodi (verilog-modi-lookup submod t))
11382 (let* ((indent-pt (current-indentation))
9489a450 11383 (v2k (verilog-in-paren-quick))
6341f357 11384 (modi (verilog-modi-current))
5509c6ad
DN
11385 (moddecls (verilog-modi-get-decls modi))
11386 (submoddecls (verilog-modi-get-decls submodi))
6341f357 11387 (sig-list-i (verilog-signals-not-in
9489a450
MM
11388 (cond (all-in
11389 (append
11390 (verilog-decls-get-inputs submoddecls)
11391 (verilog-decls-get-inouts submoddecls)
11392 (verilog-decls-get-outputs submoddecls)))
11393 (complement
11394 (verilog-decls-get-outputs submoddecls))
11395 (t (verilog-decls-get-inputs submoddecls)))
5509c6ad 11396 (append (verilog-decls-get-inputs moddecls))))
6341f357 11397 (sig-list-o (verilog-signals-not-in
9489a450
MM
11398 (cond (all-in nil)
11399 (complement
11400 (verilog-decls-get-inputs submoddecls))
11401 (t (verilog-decls-get-outputs submoddecls)))
5509c6ad 11402 (append (verilog-decls-get-outputs moddecls))))
6341f357 11403 (sig-list-io (verilog-signals-not-in
9489a450
MM
11404 (cond (all-in nil)
11405 (t (verilog-decls-get-inouts submoddecls)))
a3a8b002
DN
11406 (append (verilog-decls-get-inouts moddecls))))
11407 (sig-list-if (verilog-signals-not-in
11408 (verilog-decls-get-interfaces submoddecls)
11409 (append (verilog-decls-get-interfaces moddecls)))))
6341f357 11410 (forward-line 1)
a3a8b002
DN
11411 (setq sig-list-i (verilog-signals-matching-dir-re
11412 (verilog-signals-matching-regexp sig-list-i regexp)
11413 "input" direction-re)
11414 sig-list-o (verilog-signals-matching-dir-re
11415 (verilog-signals-matching-regexp sig-list-o regexp)
11416 "output" direction-re)
11417 sig-list-io (verilog-signals-matching-dir-re
11418 (verilog-signals-matching-regexp sig-list-io regexp)
11419 "inout" direction-re)
11420 sig-list-if (verilog-signals-matching-dir-re
11421 (verilog-signals-matching-regexp sig-list-if regexp)
11422 "interface" direction-re))
6341f357
DN
11423 (when v2k (verilog-repair-open-comma))
11424 (when (or sig-list-i sig-list-o sig-list-io)
11425 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
11426 ;; Don't sort them so a upper AUTOINST will match the main module
9489a450
MM
11427 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
11428 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
11429 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
11430 (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
6341f357 11431 (verilog-insert-indent "// End of automatics\n"))
60618039 11432 (when v2k (verilog-repair-close-comma)))))))
6341f357 11433
7cb1c4d7
DN
11434(defun verilog-auto-inout-comp ()
11435 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
11436Take input/output/inout statements from the specified module and
11437insert the inverse into the current module (inputs become outputs
11438and vice-versa.) This is useful for making test and stimulus
11439modules which need to have complementing I/O with another module.
11440Any I/O which are already defined in this module will not be
a03c2342
WS
11441redefined. For the complement of this function, see
11442`verilog-auto-inout-module'.
7cb1c4d7
DN
11443
11444Limitations:
11445 If placed inside the parenthesis of a module declaration, it creates
11446 Verilog 2001 style, else uses Verilog 1995 style.
11447
301b181a 11448 Concatenation and outputting partial buses is not supported.
7cb1c4d7
DN
11449
11450 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11451
11452 Signals are not inserted in the same order as in the original module,
11453 though they will appear to be in the same order to a AUTOINST
11454 instantiating either module.
11455
11456An example:
11457
a03c2342 11458 module ExampShell (/*AUTOARG*/);
7cb1c4d7
DN
11459 /*AUTOINOUTCOMP(\"ExampMain\")*/
11460 endmodule
11461
a03c2342 11462 module ExampMain (i,o,io);
7cb1c4d7
DN
11463 input i;
11464 output o;
11465 inout io;
11466 endmodule
11467
11468Typing \\[verilog-auto] will make this into:
11469
a03c2342 11470 module ExampShell (/*AUTOARG*/i,o,io);
7cb1c4d7
DN
11471 /*AUTOINOUTCOMP(\"ExampMain\")*/
11472 // Beginning of automatic in/out/inouts (from specific module)
11473 output i;
11474 inout io;
11475 input o;
11476 // End of automatics
11477 endmodule
11478
11479You may also provide an optional regular expression, in which case only
11480signals matching the regular expression will be included. For example the
11481same expansion will result from only extracting signals starting with i:
11482
11483 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
9489a450
MM
11484 (verilog-auto-inout-module t nil))
11485
11486(defun verilog-auto-inout-in ()
11487 "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
11488Take input/output/inout statements from the specified module and
11489insert them as all inputs into the current module. This is
11490useful for making monitor modules which need to see all signals
11491as inputs based on another module. Any I/O which are already
11492defined in this module will not be redefined. See also
11493`verilog-auto-inout-module'.
11494
11495Limitations:
11496 If placed inside the parenthesis of a module declaration, it creates
11497 Verilog 2001 style, else uses Verilog 1995 style.
11498
301b181a 11499 Concatenation and outputting partial buses is not supported.
9489a450
MM
11500
11501 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11502
11503 Signals are not inserted in the same order as in the original module,
11504 though they will appear to be in the same order to a AUTOINST
11505 instantiating either module.
11506
11507An example:
11508
11509 module ExampShell (/*AUTOARG*/);
11510 /*AUTOINOUTIN(\"ExampMain\")*/
11511 endmodule
11512
11513 module ExampMain (i,o,io);
11514 input i;
11515 output o;
11516 inout io;
11517 endmodule
11518
11519Typing \\[verilog-auto] will make this into:
11520
11521 module ExampShell (/*AUTOARG*/i,o,io);
11522 /*AUTOINOUTIN(\"ExampMain\")*/
11523 // Beginning of automatic in/out/inouts (from specific module)
11524 input i;
11525 input io;
11526 input o;
11527 // End of automatics
11528 endmodule
11529
11530You may also provide an optional regular expression, in which case only
11531signals matching the regular expression will be included. For example the
11532same expansion will result from only extracting signals starting with i:
11533
11534 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
11535 (verilog-auto-inout-module nil t))
7cb1c4d7 11536
a3a8b002
DN
11537(defun verilog-auto-insert-lisp ()
11538 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
11539The Lisp code provided is called, and the Lisp code calls
11540`insert` to insert text into the current file beginning on the
11541line after the AUTOINSERTLISP.
11542
11543See also AUTO_LISP, which takes a Lisp expression and evaluates
11544it during `verilog-auto-inst' but does not insert any text.
11545
11546An example:
11547
11548 module ExampInsertLisp;
11549 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
11550 endmodule
11551
11552 // For this example we declare the function in the
11553 // module's file itself. Often you'd define it instead
11554 // in a site-start.el or .emacs file.
11555 /*
11556 Local Variables:
11557 eval:
11558 (defun my-verilog-insert-hello (who)
11559 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
11560 End:
11561 */
11562
11563Typing \\[verilog-auto] will call my-verilog-insert-hello and
11564expand the above into:
11565
11566 // Beginning of automatic insert lisp
11567 initial $write(\"hello world\");
11568 // End of automatics
11569
11570You can also call an external program and insert the returned
11571text:
11572
11573 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
11574 // Beginning of automatic insert lisp
11575 //hello
11576 // End of automatics"
11577 (save-excursion
11578 ;; Point is at end of /*AUTO...*/
11579 (let* ((indent-pt (current-indentation))
11580 (cmd-end-pt (save-excursion (search-backward ")")
11581 (forward-char)
11582 (point))) ;; Closing paren
11583 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
9489a450 11584 (backward-sexp 1) ;; Inside comment
a3a8b002
DN
11585 (point))) ;; Beginning paren
11586 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
11587 (forward-line 1)
0e5c8aed
DN
11588 ;; Some commands don't move point (like insert-file) so we always
11589 ;; add the begin/end comments, then delete it if not needed
11590 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
11591 (verilog-insert-indent "// End of automatics\n")
11592 (forward-line -1)
11593 (eval (read cmd))
11594 (forward-line -1)
a03c2342 11595 (setq verilog-scan-cache-tick nil) ;; Clear cache; inserted unknown text
0e5c8aed 11596 (verilog-delete-empty-auto-pair))))
a3a8b002 11597
5509c6ad 11598(defun verilog-auto-sense-sigs (moddecls presense-sigs)
6341f357
DN
11599 "Return list of signals for current AUTOSENSE block."
11600 (let* ((sigss (verilog-read-always-signals))
11601 (sig-list (verilog-signals-not-params
11602 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
11603 (append (and (not verilog-auto-sense-include-inputs)
9489a450
MM
11604 (verilog-alw-get-outputs-delayed sigss))
11605 (and (not verilog-auto-sense-include-inputs)
11606 (verilog-alw-get-outputs-immediate sigss))
a03c2342 11607 (verilog-alw-get-temps sigss)
5509c6ad
DN
11608 (verilog-decls-get-consts moddecls)
11609 (verilog-decls-get-gparams moddecls)
6341f357
DN
11610 presense-sigs)))))
11611 sig-list))
11612
11613(defun verilog-auto-sense ()
11614 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
11615Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
11616with one automatically derived from all inputs declared in the always
11617statement. Signals that are generated within the same always block are NOT
11618placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
11619Long lines are split based on the `fill-column', see \\[set-fill-column].
11620
11621Limitations:
11622 Verilog does not allow memories (multidimensional arrays) in sensitivity
11623 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
11624
11625Constant signals:
11626 AUTOSENSE cannot always determine if a `define is a constant or a signal
11627 (it could be in a include file for example). If a `define or other signal
11628 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
11629 declaration anywhere in the module (parenthesis are required):
11630
11631 /* AUTO_CONSTANT ( `this_is_really_constant_dont_autosense_it ) */
11632
11633 Better yet, use a parameter, which will be understood to be constant
11634 automatically.
11635
11636OOps!
11637 If AUTOSENSE makes a mistake, please report it. (First try putting
11638 a begin/end after your always!) As a workaround, if a signal that
11639 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
11640 If a signal should be in the sensitivity list wasn't, placing it before
11641 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
11642 autos are updated (or added if it occurs there already).
11643
11644An example:
11645
1dd4b004 11646 always @ (/*AS*/) begin
6341f357
DN
11647 /* AUTO_CONSTANT (`constant) */
11648 outin = ina | inb | `constant;
11649 out = outin;
11650 end
11651
11652Typing \\[verilog-auto] will make this into:
11653
1dd4b004 11654 always @ (/*AS*/ina or inb) begin
6341f357
DN
11655 /* AUTO_CONSTANT (`constant) */
11656 outin = ina | inb | `constant;
11657 out = outin;
1dd4b004
DN
11658 end
11659
11660Note in Verilog 2001, you can often get the same result from the new @*
11661operator. (This was added to the language in part due to AUTOSENSE!)
11662
11663 always @* begin
11664 outin = ina | inb | `constant;
11665 out = outin;
6341f357
DN
11666 end"
11667 (save-excursion
11668 ;; Find beginning
11669 (let* ((start-pt (save-excursion
9489a450 11670 (verilog-re-search-backward-quick "(" nil t)
6341f357
DN
11671 (point)))
11672 (indent-pt (save-excursion
11673 (or (and (goto-char start-pt) (1+ (current-column)))
11674 (current-indentation))))
11675 (modi (verilog-modi-current))
5509c6ad 11676 (moddecls (verilog-modi-get-decls modi))
6341f357 11677 (sig-memories (verilog-signals-memory
9489a450 11678 (verilog-decls-get-vars moddecls)))
6341f357
DN
11679 sig-list not-first presense-sigs)
11680 ;; Read signals in always, eliminate outputs from sense list
11681 (setq presense-sigs (verilog-signals-from-signame
11682 (save-excursion
11683 (verilog-read-signals start-pt (point)))))
5509c6ad 11684 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
6341f357
DN
11685 (when sig-memories
11686 (let ((tlen (length sig-list)))
11687 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
a03c2342 11688 (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
6341f357
DN
11689 (if (and presense-sigs ;; Add a "or" if not "(.... or /*AUTOSENSE*/"
11690 (save-excursion (goto-char (point))
9489a450
MM
11691 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
11692 (verilog-re-search-backward-quick "\\s-" start-pt t)
6341f357 11693 (while (looking-at "\\s-`endif")
9489a450
MM
11694 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
11695 (verilog-re-search-backward-quick "\\s-" start-pt t))
6341f357
DN
11696 (not (looking-at "\\s-or\\b"))))
11697 (setq not-first t))
11698 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
11699 (while sig-list
11700 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
11701 (insert "\n")
11702 (indent-to indent-pt)
11703 (if not-first (insert "or ")))
11704 (not-first (insert " or ")))
11705 (insert (verilog-sig-name (car sig-list)))
11706 (setq sig-list (cdr sig-list)
60618039 11707 not-first t)))))
6341f357
DN
11708
11709(defun verilog-auto-reset ()
11710 "Expand AUTORESET statements, as part of \\[verilog-auto].
11711Replace the /*AUTORESET*/ comment with code to initialize all
11712registers set elsewhere in the always block.
11713
11714Limitations:
11715 AUTORESET will not clear memories.
11716
9489a450 11717 AUTORESET uses <= if the signal has a <= assignment in the block,
a3a8b002 11718 else it uses =.
6341f357 11719
9489a450
MM
11720 If <= is used, all = assigned variables are ignored if
11721 `verilog-auto-reset-blocking-in-non' is nil; they are presumed
11722 to be temporaries.
11723
6341f357
DN
11724/*AUTORESET*/ presumes that any signals mentioned between the previous
11725begin/case/if statement and the AUTORESET comment are being reset manually
11726and should not be automatically reset. This includes omitting any signals
11727used on the right hand side of assignments.
11728
11729By default, AUTORESET will include the width of the signal in the autos,
11730this is a recent change. To control this behavior, see
11731`verilog-auto-reset-widths'.
11732
11733AUTORESET ties signals to deasserted, which is presumed to be zero.
22bcf204 11734Signals that match `verilog-active-low-regexp' will be deasserted by tying
6341f357
DN
11735them to a one.
11736
11737An example:
11738
11739 always @(posedge clk or negedge reset_l) begin
11740 if (!reset_l) begin
11741 c <= 1;
11742 /*AUTORESET*/
11743 end
11744 else begin
11745 a <= in_a;
11746 b <= in_b;
11747 c <= in_c;
11748 end
11749 end
11750
11751Typing \\[verilog-auto] will make this into:
11752
11753 always @(posedge core_clk or negedge reset_l) begin
11754 if (!reset_l) begin
11755 c <= 1;
11756 /*AUTORESET*/
11757 // Beginning of autoreset for uninitialized flops
11758 a <= 0;
9489a450 11759 b = 0; // if `verilog-auto-reset-blocking-in-non' true
6341f357
DN
11760 // End of automatics
11761 end
11762 else begin
11763 a <= in_a;
9489a450 11764 b = in_b;
6341f357
DN
11765 c <= in_c;
11766 end
11767 end"
11768
11769 (interactive)
11770 (save-excursion
11771 ;; Find beginning
11772 (let* ((indent-pt (current-indentation))
11773 (modi (verilog-modi-current))
5509c6ad
DN
11774 (moddecls (verilog-modi-get-decls modi))
11775 (all-list (verilog-decls-get-signals moddecls))
9489a450 11776 sigss sig-list dly-list prereset-sigs)
6341f357
DN
11777 ;; Read signals in always, eliminate outputs from reset list
11778 (setq prereset-sigs (verilog-signals-from-signame
11779 (save-excursion
11780 (verilog-read-signals
11781 (save-excursion
9489a450 11782 (verilog-re-search-backward-quick "\\(@\\|\\<begin\\>\\|\\<if\\>\\|\\<case\\>\\)" nil t)
6341f357
DN
11783 (point))
11784 (point)))))
11785 (save-excursion
9489a450 11786 (verilog-re-search-backward-quick "@" nil t)
6341f357 11787 (setq sigss (verilog-read-always-signals)))
9489a450
MM
11788 (setq dly-list (verilog-alw-get-outputs-delayed sigss))
11789 (setq sig-list (verilog-signals-not-in (append
11790 (verilog-alw-get-outputs-delayed sigss)
11791 (when (or (not (verilog-alw-get-uses-delayed sigss))
11792 verilog-auto-reset-blocking-in-non)
11793 (verilog-alw-get-outputs-immediate sigss)))
a03c2342
WS
11794 (append
11795 (verilog-alw-get-temps sigss)
11796 prereset-sigs)))
6341f357
DN
11797 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
11798 (when sig-list
11799 (insert "\n");
a03c2342 11800 (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
6341f357
DN
11801 (while sig-list
11802 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ;; As sig-list has no widths
11803 (car sig-list))))
9489a450 11804 (indent-to indent-pt)
6341f357 11805 (insert (verilog-sig-name sig)
9489a450
MM
11806 (if (assoc (verilog-sig-name sig) dly-list)
11807 (concat " <= " verilog-assignment-delay)
11808 " = ")
6341f357
DN
11809 (verilog-sig-tieoff sig (not verilog-auto-reset-widths))
11810 ";\n")
6341f357 11811 (setq sig-list (cdr sig-list))))
9489a450 11812 (verilog-insert-indent "// End of automatics")))))
6341f357
DN
11813
11814(defun verilog-auto-tieoff ()
11815 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
11816Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
11817signals to deasserted.
11818
11819/*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
11820input/output list as another module, but no internals. Specifically, it
11821finds all outputs in the module, and if that input is not otherwise declared
11822as a register or wire, creates a tieoff.
11823
11824AUTORESET ties signals to deasserted, which is presumed to be zero.
22bcf204 11825Signals that match `verilog-active-low-regexp' will be deasserted by tying
6341f357
DN
11826them to a one.
11827
a03c2342
WS
11828You can add signals you do not want included in AUTOTIEOFF with
11829`verilog-auto-tieoff-ignore-regexp'.
11830
9489a450
MM
11831`verilog-auto-wire-type' may be used to change the datatype of
11832the declarations.
11833
6341f357
DN
11834An example of making a stub for another module:
11835
1dd4b004 11836 module ExampStub (/*AUTOINST*/);
6341f357
DN
11837 /*AUTOINOUTMODULE(\"Foo\")*/
11838 /*AUTOTIEOFF*/
11839 // verilator lint_off UNUSED
11840 wire _unused_ok = &{1'b0,
11841 /*AUTOUNUSED*/
11842 1'b0};
11843 // verilator lint_on UNUSED
11844 endmodule
11845
11846Typing \\[verilog-auto] will make this into:
11847
1dd4b004 11848 module ExampStub (/*AUTOINST*/...);
6341f357
DN
11849 /*AUTOINOUTMODULE(\"Foo\")*/
11850 // Beginning of autotieoff
11851 output [2:0] foo;
11852 // End of automatics
11853
11854 /*AUTOTIEOFF*/
11855 // Beginning of autotieoff
11856 wire [2:0] foo = 3'b0;
11857 // End of automatics
11858 ...
11859 endmodule"
11860 (interactive)
11861 (save-excursion
11862 ;; Find beginning
11863 (let* ((indent-pt (current-indentation))
11864 (modi (verilog-modi-current))
5509c6ad
DN
11865 (moddecls (verilog-modi-get-decls modi))
11866 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357 11867 (sig-list (verilog-signals-not-in
5509c6ad 11868 (verilog-decls-get-outputs moddecls)
9489a450 11869 (append (verilog-decls-get-vars moddecls)
5509c6ad
DN
11870 (verilog-decls-get-assigns moddecls)
11871 (verilog-decls-get-consts moddecls)
11872 (verilog-decls-get-gparams moddecls)
a03c2342 11873 (verilog-subdecls-get-interfaced modsubdecls)
5509c6ad
DN
11874 (verilog-subdecls-get-outputs modsubdecls)
11875 (verilog-subdecls-get-inouts modsubdecls)))))
a03c2342
WS
11876 (setq sig-list (verilog-signals-not-matching-regexp
11877 sig-list verilog-auto-tieoff-ignore-regexp))
6341f357
DN
11878 (when sig-list
11879 (forward-line 1)
11880 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
11881 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
9489a450 11882 (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
6341f357
DN
11883 (while sig-list
11884 (let ((sig (car sig-list)))
9489a450
MM
11885 (cond ((equal verilog-auto-tieoff-declaration "assign")
11886 (indent-to indent-pt)
11887 (insert "assign " (verilog-sig-name sig)))
11888 (t
11889 (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
6341f357
DN
11890 (indent-to (max 48 (+ indent-pt 40)))
11891 (insert "= " (verilog-sig-tieoff sig)
11892 ";\n")
11893 (setq sig-list (cdr sig-list))))
60618039 11894 (verilog-insert-indent "// End of automatics\n")))))
6341f357
DN
11895
11896(defun verilog-auto-unused ()
11897 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
11898Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
11899input and inout signals.
11900
11901/*AUTOUNUSED*/ is used to make stub modules; modules that have the same
11902input/output list as another module, but no internals. Specifically, it
11903finds all inputs and inouts in the module, and if that input is not otherwise
11904used, adds it to a comma separated list.
11905
11906The comma separated list is intended to be used to create a _unused_ok
11907signal. Using the exact name \"_unused_ok\" for name of the temporary
11908signal is recommended as it will insure maximum forward compatibility, it
11909also makes lint warnings easy to understand; ignore any unused warnings
11910with \"unused\" in the signal name.
11911
11912To reduce simulation time, the _unused_ok signal should be forced to a
11913constant to prevent wiggling. The easiest thing to do is use a
11914reduction-and with 1'b0 as shown.
11915
11916This way all unused signals are in one place, making it convenient to add
11917your tool's specific pragmas around the assignment to disable any unused
11918warnings.
11919
11920You can add signals you do not want included in AUTOUNUSED with
11921`verilog-auto-unused-ignore-regexp'.
11922
11923An example of making a stub for another module:
11924
1dd4b004
DN
11925 module ExampStub (/*AUTOINST*/);
11926 /*AUTOINOUTMODULE(\"Examp\")*/
6341f357
DN
11927 /*AUTOTIEOFF*/
11928 // verilator lint_off UNUSED
11929 wire _unused_ok = &{1'b0,
11930 /*AUTOUNUSED*/
11931 1'b0};
11932 // verilator lint_on UNUSED
11933 endmodule
11934
11935Typing \\[verilog-auto] will make this into:
11936
11937 ...
11938 // verilator lint_off UNUSED
11939 wire _unused_ok = &{1'b0,
11940 /*AUTOUNUSED*/
11941 // Beginning of automatics
11942 unused_input_a,
11943 unused_input_b,
11944 unused_input_c,
11945 // End of automatics
11946 1'b0};
11947 // verilator lint_on UNUSED
11948 endmodule"
11949 (interactive)
11950 (save-excursion
11951 ;; Find beginning
11952 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
11953 (modi (verilog-modi-current))
5509c6ad
DN
11954 (moddecls (verilog-modi-get-decls modi))
11955 (modsubdecls (verilog-modi-get-sub-decls modi))
6341f357 11956 (sig-list (verilog-signals-not-in
5509c6ad
DN
11957 (append (verilog-decls-get-inputs moddecls)
11958 (verilog-decls-get-inouts moddecls))
11959 (append (verilog-subdecls-get-inputs modsubdecls)
11960 (verilog-subdecls-get-inouts modsubdecls)))))
6341f357
DN
11961 (setq sig-list (verilog-signals-not-matching-regexp
11962 sig-list verilog-auto-unused-ignore-regexp))
11963 (when sig-list
11964 (forward-line 1)
11965 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
11966 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
11967 (while sig-list
11968 (let ((sig (car sig-list)))
11969 (indent-to indent-pt)
11970 (insert (verilog-sig-name sig) ",\n")
11971 (setq sig-list (cdr sig-list))))
60618039 11972 (verilog-insert-indent "// End of automatics\n")))))
6341f357
DN
11973
11974(defun verilog-enum-ascii (signm elim-regexp)
37ea4b9b 11975 "Convert an enum name SIGNM to an ascii string for insertion.
6341f357
DN
11976Remove user provided prefix ELIM-REGEXP."
11977 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
11978 (let ((case-fold-search t))
11979 ;; All upper becomes all lower for readability
11980 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
11981
11982(defun verilog-auto-ascii-enum ()
11983 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
11984Create a register to contain the ASCII decode of a enumerated signal type.
11985This will allow trace viewers to show the ASCII name of states.
11986
11987First, parameters are built into a enumeration using the synopsys enum
11988comment. The comment must be between the keyword and the symbol.
6edb5716 11989\(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
6341f357
DN
11990
11991Next, registers which that enum applies to are also tagged with the same
9489a450 11992enum.
6341f357
DN
11993
11994Finally, a AUTOASCIIENUM command is used.
11995
11996 The first parameter is the name of the signal to be decoded.
11997
11998 The second parameter is the name to store the ASCII code into. For the
11999 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
12000 a signal that is just for simulation, and the magic characters _ascii
12001 tell viewers like Dinotrace to display in ASCII format.
12002
9489a450
MM
12003 The third optional parameter is a string which will be removed
12004 from the state names. It defaults to "" which removes nothing.
12005
12006 The fourth optional parameter is \"onehot\" to force one-hot
12007 decoding. If unspecified, if and only if the first parameter
12008 width is 2^(number of states in enum) and does NOT match the
12009 width of the enum, the signal is assumed to be a one hot
12010 decode. Otherwise, it's a normal encoded state vector.
12011
12012 `verilog-auto-wire-type' may be used to change the datatype of
12013 the declarations.
6341f357
DN
12014
12015An example:
12016
12017 //== State enumeration
12018 parameter [2:0] // synopsys enum state_info
12019 SM_IDLE = 3'b000,
12020 SM_SEND = 3'b001,
12021 SM_WAIT1 = 3'b010;
12022 //== State variables
a3a8b002
DN
12023 reg [2:0] /* synopsys enum state_info */
12024 state_r; /* synopsys state_vector state_r */
12025 reg [2:0] /* synopsys enum state_info */
12026 state_e1;
6341f357
DN
12027
12028 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12029
12030Typing \\[verilog-auto] will make this into:
12031
12032 ... same front matter ...
12033
12034 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12035 // Beginning of automatic ASCII enum decoding
12036 reg [39:0] state_ascii_r; // Decode of state_r
12037 always @(state_r) begin
12038 case ({state_r})
12039 SM_IDLE: state_ascii_r = \"idle \";
12040 SM_SEND: state_ascii_r = \"send \";
12041 SM_WAIT1: state_ascii_r = \"wait1\";
12042 default: state_ascii_r = \"%Erro\";
12043 endcase
12044 end
12045 // End of automatics"
12046 (save-excursion
9489a450 12047 (let* ((params (verilog-read-auto-params 2 4))
6341f357
DN
12048 (undecode-name (nth 0 params))
12049 (ascii-name (nth 1 params))
9489a450
MM
12050 (elim-regexp (and (nth 2 params)
12051 (not (equal (nth 2 params) ""))
12052 (nth 2 params)))
12053 (one-hot-flag (nth 3 params))
6341f357
DN
12054 ;;
12055 (indent-pt (current-indentation))
12056 (modi (verilog-modi-current))
5509c6ad 12057 (moddecls (verilog-modi-get-decls modi))
6341f357 12058 ;;
5509c6ad
DN
12059 (sig-list-consts (append (verilog-decls-get-consts moddecls)
12060 (verilog-decls-get-gparams moddecls)))
9489a450 12061 (sig-list-all (append (verilog-decls-get-vars moddecls)
5509c6ad
DN
12062 (verilog-decls-get-outputs moddecls)
12063 (verilog-decls-get-inouts moddecls)
9489a450 12064 (verilog-decls-get-inputs moddecls)))
6341f357
DN
12065 ;;
12066 (undecode-sig (or (assoc undecode-name sig-list-all)
12067 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
12068 (undecode-enum (or (verilog-sig-enum undecode-sig)
12069 (error "%s: Signal %s does not have a enum tag" (verilog-point-text) undecode-name)))
12070 ;;
a3a8b002
DN
12071 (enum-sigs (verilog-signals-not-in
12072 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
12073 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum))
12074 nil))
6341f357 12075 ;;
9489a450
MM
12076 (one-hot (or
12077 (string-match "onehot" (or one-hot-flag ""))
12078 (and ;; width(enum) != width(sig)
12079 (or (not (verilog-sig-bits (car enum-sigs)))
12080 (not (equal (verilog-sig-width (car enum-sigs))
12081 (verilog-sig-width undecode-sig))))
12082 ;; count(enums) == width(sig)
12083 (equal (number-to-string (length enum-sigs))
12084 (verilog-sig-width undecode-sig)))))
a3a8b002 12085 (enum-chars 0)
6341f357
DN
12086 (ascii-chars 0))
12087 ;;
12088 ;; Find number of ascii chars needed
12089 (let ((tmp-sigs enum-sigs))
12090 (while tmp-sigs
12091 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
12092 ascii-chars (max ascii-chars (length (verilog-enum-ascii
12093 (verilog-sig-name (car tmp-sigs))
12094 elim-regexp)))
12095 tmp-sigs (cdr tmp-sigs))))
12096 ;;
12097 (forward-line 1)
12098 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
12099 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
12100 (concat "Decode of " undecode-name) nil nil))))
9489a450 12101 (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
6341f357
DN
12102 ;;
12103 (verilog-insert-indent "always @(" undecode-name ") begin\n")
12104 (setq indent-pt (+ indent-pt verilog-indent-level))
9489a450 12105 (verilog-insert-indent "case ({" undecode-name "})\n")
6341f357
DN
12106 (setq indent-pt (+ indent-pt verilog-case-indent))
12107 ;;
12108 (let ((tmp-sigs enum-sigs)
a3a8b002
DN
12109 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
12110 (+ (if one-hot 9 1) (max 8 enum-chars))
6341f357
DN
12111 ascii-name ascii-chars))
12112 (errname (substring "%Error" 0 (min 6 ascii-chars))))
12113 (while tmp-sigs
12114 (verilog-insert-indent
a3a8b002
DN
12115 (concat
12116 (format chrfmt
12117 (concat (if one-hot "(")
9489a450
MM
12118 ;; Use enum-sigs length as that's numeric
12119 ;; verilog-sig-width undecode-sig might not be.
12120 (if one-hot (number-to-string (length enum-sigs)))
a3a8b002
DN
12121 ;; We use a shift instead of var[index]
12122 ;; so that a non-one hot value will show as error.
12123 (if one-hot "'b1<<")
12124 (verilog-sig-name (car tmp-sigs))
12125 (if one-hot ")") ":")
12126 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
12127 elim-regexp))))
6341f357
DN
12128 (setq tmp-sigs (cdr tmp-sigs)))
12129 (verilog-insert-indent (format chrfmt "default:" errname)))
12130 ;;
12131 (setq indent-pt (- indent-pt verilog-case-indent))
12132 (verilog-insert-indent "endcase\n")
12133 (setq indent-pt (- indent-pt verilog-indent-level))
12134 (verilog-insert-indent "end\n"
60618039 12135 "// End of automatics\n"))))
6341f357
DN
12136
12137(defun verilog-auto-templated-rel ()
12138 "Replace Templated relative line numbers with absolute line numbers.
12139Internal use only. This hacks around the line numbers in AUTOINST Templates
12140being different from the final output's line numbering."
a03c2342 12141 (let ((templateno 0) (template-line (list 0)) (buf-line 1))
6341f357 12142 ;; Find line number each template is on
a03c2342 12143 ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
6341f357 12144 (goto-char (point-min))
a03c2342
WS
12145 (while (not (eobp))
12146 (when (looking-at ".*AUTO_TEMPLATE")
12147 (setq templateno (1+ templateno))
12148 (setq template-line (cons buf-line template-line)))
12149 (setq buf-line (1+ buf-line))
12150 (forward-line 1))
6341f357
DN
12151 (setq template-line (nreverse template-line))
12152 ;; Replace T# L# with absolute line number
12153 (goto-char (point-min))
12154 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
7ea26faf
DN
12155 (replace-match
12156 (concat " Templated "
12157 (int-to-string (+ (nth (string-to-number (match-string 1))
12158 template-line)
12159 (string-to-number (match-string 2)))))
12160 t t))))
6341f357
DN
12161
12162\f
12163;;
12164;; Auto top level
12165;;
12166
12167(defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing a arg
12168 "Expand AUTO statements.
12169Look for any /*AUTO...*/ commands in the code, as used in
12170instantiations or argument headers. Update the list of signals
12171following the /*AUTO...*/ command.
12172
12173Use \\[verilog-delete-auto] to remove the AUTOs.
12174
9489a450
MM
12175Use \\[verilog-diff-auto] to see differences in AUTO expansion.
12176
6341f357
DN
12177Use \\[verilog-inject-auto] to insert AUTOs for the first time.
12178
12179Use \\[verilog-faq] for a pointer to frequently asked questions.
12180
12181The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
12182called before and after this function, respectively.
12183
12184For example:
a03c2342 12185 module ModuleName (/*AUTOARG*/);
6341f357
DN
12186 /*AUTOINPUT*/
12187 /*AUTOOUTPUT*/
12188 /*AUTOWIRE*/
12189 /*AUTOREG*/
1dd4b004 12190 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
6341f357
DN
12191
12192You can also update the AUTOs from the shell using:
12193 emacs --batch <filenames.v> -f verilog-batch-auto
12194Or fix indentation with:
12195 emacs --batch <filenames.v> -f verilog-batch-indent
12196Likewise, you can delete or inject AUTOs with:
12197 emacs --batch <filenames.v> -f verilog-batch-delete-auto
12198 emacs --batch <filenames.v> -f verilog-batch-inject-auto
9489a450
MM
12199Or check if AUTOs have the same expansion
12200 emacs --batch <filenames.v> -f verilog-batch-diff-auto
6341f357
DN
12201
12202Using \\[describe-function], see also:
12203 `verilog-auto-arg' for AUTOARG module instantiations
12204 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
9489a450
MM
12205 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
12206 `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
6341f357
DN
12207 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
12208 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
12209 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
a3a8b002 12210 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
6341f357
DN
12211 `verilog-auto-inst' for AUTOINST instantiation pins
12212 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
12213 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
9489a450 12214 `verilog-auto-logic' for AUTOLOGIC declaring logic signals
6341f357
DN
12215 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
12216 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
12217 `verilog-auto-reg' for AUTOREG registers
12218 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
12219 `verilog-auto-reset' for AUTORESET flop resets
12220 `verilog-auto-sense' for AUTOSENSE always sensitivity lists
12221 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
12222 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
12223 `verilog-auto-wire' for AUTOWIRE instantiation wires
12224
12225 `verilog-read-defines' for reading `define values
12226 `verilog-read-includes' for reading `includes
12227
a3a8b002 12228If you have bugs with these autos, please file an issue at
855b42a2 12229URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
a3a8b002 12230Wilson Snyder (wsnyder@wsnyder.org)."
6341f357
DN
12231 (interactive)
12232 (unless noninteractive (message "Updating AUTOs..."))
7ea26faf 12233 (if (fboundp 'dinotrace-unannotate-all)
6341f357
DN
12234 (dinotrace-unannotate-all))
12235 (let ((oldbuf (if (not (buffer-modified-p))
12236 (buffer-string)))
12237 ;; Before version 20, match-string with font-lock returns a
12238 ;; vector that is not equal to the string. IE if on "input"
12239 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
12240 (fontlocked (when (and (boundp 'font-lock-mode)
12241 font-lock-mode)
7cb1c4d7 12242 (font-lock-mode 0)
5509c6ad
DN
12243 t))
12244 ;; Cache directories; we don't write new files, so can't change
a03c2342
WS
12245 (verilog-dir-cache-preserving t)
12246 ;; Cache current module
12247 (verilog-modi-cache-current-enable t)
12248 (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
12249 verilog-modi-cache-current)
12250 (unwind-protect
12251 ;; Disable change hooks for speed
12252 ;; This let can't be part of above let; must restore
12253 ;; after-change-functions before font-lock resumes
12254 (verilog-save-no-change-functions
12255 (verilog-save-scan-cache
12256 (save-excursion
9489a450
MM
12257 ;; Wipe cache; otherwise if we AUTOed a block above this one,
12258 ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
12259 (setq verilog-modi-cache-list nil)
a03c2342
WS
12260 ;; If we're not in verilog-mode, change syntax table so parsing works right
12261 (unless (eq major-mode `verilog-mode) (verilog-mode))
12262 ;; Allow user to customize
9489a450 12263 (verilog-run-hooks 'verilog-before-auto-hook)
a03c2342
WS
12264 ;; Try to save the user from needing to revert-file to reread file local-variables
12265 (verilog-auto-reeval-locals)
12266 (verilog-read-auto-lisp-present)
12267 (verilog-read-auto-lisp (point-min) (point-max))
12268 (verilog-getopt-flags)
12269 ;; From here on out, we can cache anything we read from disk
12270 (verilog-preserve-dir-cache
12271 ;; These two may seem obvious to do always, but on large includes it can be way too slow
12272 (when verilog-auto-read-includes
12273 (verilog-read-includes)
12274 (verilog-read-defines nil nil t))
12275 ;; This particular ordering is important
12276 ;; INST: Lower modules correct, no internal dependencies, FIRST
12277 (verilog-preserve-modi-cache
12278 ;; Clear existing autos else we'll be screwed by existing ones
12279 (verilog-delete-auto)
12280 ;; Injection if appropriate
12281 (when inject
12282 (verilog-inject-inst)
12283 (verilog-inject-sense)
12284 (verilog-inject-arg))
12285 ;;
12286 ;; Do user inserts first, so their code can insert AUTOs
12287 ;; We may provide a AUTOINSERTLISPLAST if another cleanup pass is needed
12288 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
12289 'verilog-auto-insert-lisp)
12290 ;; Expand instances before need the signals the instances input/output
12291 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
12292 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
12293 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
12294 ;; Doesn't matter when done, but combine it with a common changer
12295 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
12296 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
12297 ;; Must be done before autoin/out as creates a reg
12298 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM([^)]*)\\*/" 'verilog-auto-ascii-enum)
12299 ;;
12300 ;; first in/outs from other files
12301 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE([^)]*)\\*/" 'verilog-auto-inout-module)
12302 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP([^)]*)\\*/" 'verilog-auto-inout-comp)
9489a450 12303 (verilog-auto-re-search-do "/\\*AUTOINOUTIN([^)]*)\\*/" 'verilog-auto-inout-in)
a03c2342
WS
12304 ;; next in/outs which need previous sucked inputs first
12305 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((\"[^\"]*\")\\)\\*/"
4f91a816 12306 (lambda () (verilog-auto-output t)))
a03c2342
WS
12307 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\*/" 'verilog-auto-output)
12308 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((\"[^\"]*\")\\)\\*/"
4f91a816 12309 (lambda () (verilog-auto-input t)))
a03c2342
WS
12310 (verilog-auto-re-search-do "/\\*AUTOINPUT\\*/" 'verilog-auto-input)
12311 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((\"[^\"]*\")\\)\\*/"
4f91a816 12312 (lambda () (verilog-auto-inout t)))
a03c2342
WS
12313 (verilog-auto-re-search-do "/\\*AUTOINOUT\\*/" 'verilog-auto-inout)
12314 ;; Then tie off those in/outs
12315 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
12316 ;; Wires/regs must be after inputs/outputs
9489a450 12317 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
a03c2342
WS
12318 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
12319 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
12320 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
12321 ;; outputevery needs AUTOOUTPUTs done first
12322 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\*/" 'verilog-auto-output-every)
12323 ;; After we've created all new variables
12324 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
12325 ;; Must be after all inputs outputs are generated
12326 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
12327 ;; Fix line numbers (comments only)
12328 (when verilog-auto-inst-template-numbers
12329 (verilog-auto-templated-rel))))
12330 ;;
9489a450
MM
12331 (verilog-run-hooks 'verilog-auto-hook)
12332 ;;
12333 (when verilog-auto-delete-trailing-whitespace
12334 (verilog-delete-trailing-whitespace))
a03c2342
WS
12335 ;;
12336 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
12337 ;;
12338 ;; If end result is same as when started, clear modified flag
12339 (cond ((and oldbuf (equal oldbuf (buffer-string)))
12340 (set-buffer-modified-p nil)
12341 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
12342 (t (unless noninteractive (message "Updating AUTOs...done"))))
12343 ;; End of after-change protection
12344 )))
12345 ;; Unwind forms
12346 (progn
12347 ;; Restore font-lock
12348 (when fontlocked (font-lock-mode t))))))
6341f357
DN
12349\f
12350
12351;;
12352;; Skeleton based code insertion
12353;;
60618039 12354(defvar verilog-template-map
6edb5716
DN
12355 (let ((map (make-sparse-keymap)))
12356 (define-key map "a" 'verilog-sk-always)
12357 (define-key map "b" 'verilog-sk-begin)
12358 (define-key map "c" 'verilog-sk-case)
12359 (define-key map "f" 'verilog-sk-for)
12360 (define-key map "g" 'verilog-sk-generate)
12361 (define-key map "h" 'verilog-sk-header)
12362 (define-key map "i" 'verilog-sk-initial)
12363 (define-key map "j" 'verilog-sk-fork)
12364 (define-key map "m" 'verilog-sk-module)
9489a450 12365 (define-key map "o" 'verilog-sk-ovm-class)
6edb5716
DN
12366 (define-key map "p" 'verilog-sk-primitive)
12367 (define-key map "r" 'verilog-sk-repeat)
12368 (define-key map "s" 'verilog-sk-specify)
12369 (define-key map "t" 'verilog-sk-task)
9489a450 12370 (define-key map "u" 'verilog-sk-uvm-class)
6edb5716
DN
12371 (define-key map "w" 'verilog-sk-while)
12372 (define-key map "x" 'verilog-sk-casex)
12373 (define-key map "z" 'verilog-sk-casez)
12374 (define-key map "?" 'verilog-sk-if)
12375 (define-key map ":" 'verilog-sk-else-if)
12376 (define-key map "/" 'verilog-sk-comment)
12377 (define-key map "A" 'verilog-sk-assign)
12378 (define-key map "F" 'verilog-sk-function)
12379 (define-key map "I" 'verilog-sk-input)
12380 (define-key map "O" 'verilog-sk-output)
12381 (define-key map "S" 'verilog-sk-state-machine)
12382 (define-key map "=" 'verilog-sk-inout)
12383 (define-key map "W" 'verilog-sk-wire)
12384 (define-key map "R" 'verilog-sk-reg)
30d48f20
DN
12385 (define-key map "D" 'verilog-sk-define-signal)
12386 map)
6341f357
DN
12387 "Keymap used in Verilog mode for smart template operations.")
12388
6341f357
DN
12389
12390;;
12391;; Place the templates into Verilog Mode. They may be inserted under any key.
12392;; C-c C-t will be the default. If you use templates a lot, you
12393;; may want to consider moving the binding to another key in your .emacs
12394;; file.
12395;;
9489a450 12396;; Note \C-c and letter are reserved for users
6341f357
DN
12397(define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
12398
12399;;; ---- statement skeletons ------------------------------------------
12400
12401(define-skeleton verilog-sk-prompt-condition
12402 "Prompt for the loop condition."
12403 "[condition]: " str )
12404
12405(define-skeleton verilog-sk-prompt-init
12406 "Prompt for the loop init statement."
12407 "[initial statement]: " str )
12408
12409(define-skeleton verilog-sk-prompt-inc
12410 "Prompt for the loop increment statement."
12411 "[increment statement]: " str )
12412
12413(define-skeleton verilog-sk-prompt-name
12414 "Prompt for the name of something."
12415 "[name]: " str)
12416
12417(define-skeleton verilog-sk-prompt-clock
12418 "Prompt for the name of something."
12419 "name and edge of clock(s): " str)
12420
12421(defvar verilog-sk-reset nil)
12422(defun verilog-sk-prompt-reset ()
12423 "Prompt for the name of a state machine reset."
7ea26faf 12424 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
6341f357
DN
12425
12426
12427(define-skeleton verilog-sk-prompt-state-selector
12428 "Prompt for the name of a state machine selector."
12429 "name of selector (eg {a,b,c,d}): " str )
12430
12431(define-skeleton verilog-sk-prompt-output
12432 "Prompt for the name of something."
12433 "output: " str)
12434
12435(define-skeleton verilog-sk-prompt-msb
9489a450 12436 "Prompt for most significant bit specification."
f07fe184 12437 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
6341f357
DN
12438
12439(define-skeleton verilog-sk-prompt-lsb
12440 "Prompt for least significant bit specification."
12441 "lsb:" str )
12442
12443(defvar verilog-sk-p nil)
12444(define-skeleton verilog-sk-prompt-width
12445 "Prompt for a width specification."
12446 ()
12447 (progn
12448 (setq verilog-sk-p (point))
12449 (verilog-sk-prompt-msb)
12450 (if (> (point) verilog-sk-p) "] " " ")))
12451
12452(defun verilog-sk-header ()
a3a8b002
DN
12453 "Insert a descriptive header at the top of the file.
12454See also `verilog-header' for an alternative format."
6341f357
DN
12455 (interactive "*")
12456 (save-excursion
12457 (goto-char (point-min))
12458 (verilog-sk-header-tmpl)))
12459
12460(define-skeleton verilog-sk-header-tmpl
12461 "Insert a comment block containing the module title, author, etc."
12462 "[Description]: "
12463 "// -*- Mode: Verilog -*-"
12464 "\n// Filename : " (buffer-name)
12465 "\n// Description : " str
12466 "\n// Author : " (user-full-name)
12467 "\n// Created On : " (current-time-string)
a3a8b002
DN
12468 "\n// Last Modified By: " (user-full-name)
12469 "\n// Last Modified On: " (current-time-string)
6341f357
DN
12470 "\n// Update Count : 0"
12471 "\n// Status : Unknown, Use with caution!"
12472 "\n")
12473
12474(define-skeleton verilog-sk-module
12475 "Insert a module definition."
12476 ()
f07fe184 12477 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
6341f357
DN
12478 > _ \n
12479 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
12480
9489a450
MM
12481;;; ------------------------------------------------------------------------
12482;;; Define a default OVM class, with macros and new()
12483;;; ------------------------------------------------------------------------
12484
12485(define-skeleton verilog-sk-ovm-class
12486 "Insert a class definition"
12487 ()
12488 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
12489 > _ \n
12490 > "`ovm_object_utils_begin(" name ")" \n
12491 > (- verilog-indent-level) " `ovm_object_utils_end" \n
12492 > _ \n
12493 > "function new(name=\"" name "\");" \n
12494 > "super.new(name);" \n
12495 > (- verilog-indent-level) "endfunction" \n
12496 > _ \n
12497 > "endclass" (progn (electric-verilog-terminate-line) nil))
12498
12499(define-skeleton verilog-sk-uvm-class
12500 "Insert a class definition"
12501 ()
12502 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
12503 > _ \n
12504 > "`uvm_object_utils_begin(" name ")" \n
12505 > (- verilog-indent-level) " `uvm_object_utils_end" \n
12506 > _ \n
12507 > "function new(name=\"" name "\");" \n
12508 > "super.new(name);" \n
12509 > (- verilog-indent-level) "endfunction" \n
12510 > _ \n
12511 > "endclass" (progn (electric-verilog-terminate-line) nil))
12512
6341f357
DN
12513(define-skeleton verilog-sk-primitive
12514 "Insert a task definition."
12515 ()
f07fe184 12516 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
6341f357
DN
12517 > _ \n
12518 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
12519
12520(define-skeleton verilog-sk-task
12521 "Insert a task definition."
12522 ()
f07fe184 12523 > "task " '(verilog-sk-prompt-name) & ?; \n
6341f357
DN
12524 > _ \n
12525 > "begin" \n
12526 > \n
12527 > (- verilog-indent-level-behavioral) "end" \n
12528 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
12529
12530(define-skeleton verilog-sk-function
12531 "Insert a function definition."
12532 ()
f07fe184 12533 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
6341f357
DN
12534 > _ \n
12535 > "begin" \n
12536 > \n
12537 > (- verilog-indent-level-behavioral) "end" \n
12538 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
12539
12540(define-skeleton verilog-sk-always
12541 "Insert always block. Uses the minibuffer to prompt
12542for sensitivity list."
12543 ()
12544 > "always @ ( /*AUTOSENSE*/ ) begin\n"
12545 > _ \n
12546 > (- verilog-indent-level-behavioral) "end" \n >
12547 )
12548
12549(define-skeleton verilog-sk-initial
12550 "Insert an initial block."
12551 ()
12552 > "initial begin\n"
12553 > _ \n
12554 > (- verilog-indent-level-behavioral) "end" \n > )
12555
12556(define-skeleton verilog-sk-specify
12557 "Insert specify block. "
12558 ()
12559 > "specify\n"
12560 > _ \n
12561 > (- verilog-indent-level-behavioral) "endspecify" \n > )
12562
12563(define-skeleton verilog-sk-generate
12564 "Insert generate block. "
12565 ()
12566 > "generate\n"
12567 > _ \n
12568 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
12569
12570(define-skeleton verilog-sk-begin
37ea4b9b 12571 "Insert begin end block. Uses the minibuffer to prompt for name."
6341f357 12572 ()
f07fe184 12573 > "begin" '(verilog-sk-prompt-name) \n
6341f357
DN
12574 > _ \n
12575 > (- verilog-indent-level-behavioral) "end"
12576)
12577
12578(define-skeleton verilog-sk-fork
37ea4b9b 12579 "Insert a fork join block."
6341f357
DN
12580 ()
12581 > "fork\n"
12582 > "begin" \n
12583 > _ \n
12584 > (- verilog-indent-level-behavioral) "end" \n
12585 > "begin" \n
12586 > \n
12587 > (- verilog-indent-level-behavioral) "end" \n
12588 > (- verilog-indent-level-behavioral) "join" \n
12589 > )
12590
12591
12592(define-skeleton verilog-sk-case
12593 "Build skeleton case statement, prompting for the selector expression,
12594and the case items."
12595 "[selector expression]: "
12596 > "case (" str ") " \n
a3a8b002 12597 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
6341f357
DN
12598 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
12599
12600(define-skeleton verilog-sk-casex
12601 "Build skeleton casex statement, prompting for the selector expression,
12602and the case items."
12603 "[selector expression]: "
12604 > "casex (" str ") " \n
a3a8b002 12605 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
6341f357
DN
12606 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
12607
12608(define-skeleton verilog-sk-casez
12609 "Build skeleton casez statement, prompting for the selector expression,
12610and the case items."
12611 "[selector expression]: "
12612 > "casez (" str ") " \n
a3a8b002 12613 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
6341f357
DN
12614 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
12615
12616(define-skeleton verilog-sk-if
12617 "Insert a skeleton if statement."
f07fe184 12618 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
6341f357
DN
12619 > _ \n
12620 > (- verilog-indent-level-behavioral) "end " \n )
12621
12622(define-skeleton verilog-sk-else-if
12623 "Insert a skeleton else if statement."
12624 > (verilog-indent-line) "else if ("
f07fe184 12625 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
6341f357
DN
12626 > _ \n
12627 > "end" (progn (electric-verilog-terminate-line) nil))
12628
12629(define-skeleton verilog-sk-datadef
37ea4b9b 12630 "Common routine to get data definition."
6341f357 12631 ()
f07fe184 12632 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
6341f357
DN
12633
12634(define-skeleton verilog-sk-input
12635 "Insert an input definition."
12636 ()
f07fe184 12637 > "input [" '(verilog-sk-datadef))
6341f357
DN
12638
12639(define-skeleton verilog-sk-output
12640 "Insert an output definition."
12641 ()
f07fe184 12642 > "output [" '(verilog-sk-datadef))
6341f357
DN
12643
12644(define-skeleton verilog-sk-inout
12645 "Insert an inout definition."
12646 ()
f07fe184 12647 > "inout [" '(verilog-sk-datadef))
6341f357
DN
12648
12649(defvar verilog-sk-signal nil)
12650(define-skeleton verilog-sk-def-reg
12651 "Insert a reg definition."
12652 ()
f07fe184 12653 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations) )
6341f357
DN
12654
12655(defun verilog-sk-define-signal ()
12656 "Insert a definition of signal under point at top of module."
12657 (interactive "*")
60618039 12658 (let* ((sig-re "[a-zA-Z0-9_]*")
6341f357
DN
12659 (v1 (buffer-substring
12660 (save-excursion
12661 (skip-chars-backward sig-re)
12662 (point))
12663 (save-excursion
12664 (skip-chars-forward sig-re)
60618039 12665 (point)))))
6341f357
DN
12666 (if (not (member v1 verilog-keywords))
12667 (save-excursion
12668 (setq verilog-sk-signal v1)
12669 (verilog-beg-of-defun)
12670 (verilog-end-of-statement)
12671 (verilog-forward-syntactic-ws)
12672 (verilog-sk-def-reg)
12673 (message "signal at point is %s" v1))
60618039 12674 (message "object at point (%s) is a keyword" v1))))
6341f357
DN
12675
12676(define-skeleton verilog-sk-wire
12677 "Insert a wire definition."
12678 ()
f07fe184 12679 > "wire [" '(verilog-sk-datadef))
6341f357
DN
12680
12681(define-skeleton verilog-sk-reg
12682 "Insert a reg definition."
12683 ()
f07fe184 12684 > "reg [" '(verilog-sk-datadef))
6341f357
DN
12685
12686(define-skeleton verilog-sk-assign
12687 "Insert a skeleton assign statement."
12688 ()
f07fe184 12689 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
6341f357
DN
12690
12691(define-skeleton verilog-sk-while
12692 "Insert a skeleton while loop statement."
12693 ()
f07fe184 12694 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
6341f357
DN
12695 > _ \n
12696 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
12697
12698(define-skeleton verilog-sk-repeat
12699 "Insert a skeleton repeat loop statement."
12700 ()
f07fe184 12701 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
6341f357
DN
12702 > _ \n
12703 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
12704
12705(define-skeleton verilog-sk-for
12706 "Insert a skeleton while loop statement."
12707 ()
12708 > "for ("
f07fe184
DN
12709 '(verilog-sk-prompt-init) "; "
12710 '(verilog-sk-prompt-condition) "; "
12711 '(verilog-sk-prompt-inc)
6341f357
DN
12712 ") begin" \n
12713 > _ \n
12714 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
12715
12716(define-skeleton verilog-sk-comment
12717 "Inserts three comment lines, making a display comment."
12718 ()
12719 > "/*\n"
12720 > "* " _ \n
12721 > "*/")
12722
12723(define-skeleton verilog-sk-state-machine
12724 "Insert a state machine definition."
12725 "Name of state variable: "
12726 '(setq input "state")
12727 > "// State registers for " str | -23 \n
12728 '(setq verilog-sk-state str)
f07fe184 12729 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
6341f357
DN
12730 '(setq input nil)
12731 > \n
12732 > "// State FF for " verilog-sk-state \n
12733 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
12734 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
12735 > verilog-sk-state " = next_" verilog-sk-state ?; \n
12736 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
12737 > \n
12738 > "// Next State Logic for " verilog-sk-state \n
12739 > "always @ ( /*AUTOSENSE*/ ) begin\n"
f07fe184 12740 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
6341f357
DN
12741 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
12742 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
12743 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
6341f357
DN
12744\f
12745
12746;;
12747;; Include file loading with mouse/return event
12748;;
12749;; idea & first impl.: M. Rouat (eldo-mode.el)
12750;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
12751
12752(if (featurep 'xemacs)
af62aa88 12753 (require 'overlay))
6341f357
DN
12754
12755(defconst verilog-include-file-regexp
12756 "^`include\\s-+\"\\([^\n\"]*\\)\""
12757 "Regexp that matches the include file.")
12758
60618039 12759(defvar verilog-mode-mouse-map
6341f357
DN
12760 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
12761 (set-keymap-parent map verilog-mode-map)
12762 ;; mouse button bindings
12763 (define-key map "\r" 'verilog-load-file-at-point)
12764 (if (featurep 'xemacs)
12765 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
12766 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
12767 (if (featurep 'xemacs)
12768 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
30d48f20
DN
12769 (define-key map [S-mouse-2] 'mouse-yank-at-click))
12770 map)
6edb5716
DN
12771 "Map containing mouse bindings for `verilog-mode'.")
12772
6341f357 12773
a03c2342
WS
12774(defun verilog-highlight-region (beg end old-len)
12775 "Colorize included files and modules in the (changed?) region.
6341f357 12776Clicking on the middle-mouse button loads them in a buffer (as in dired)."
a03c2342
WS
12777 (when (or verilog-highlight-includes
12778 verilog-highlight-modules)
12779 (save-excursion
12780 (save-match-data ;; A query-replace may call this function - do not disturb
12781 (verilog-save-buffer-state
12782 (verilog-save-scan-cache
12783 (let (end-point)
12784 (goto-char end)
3ba6b2ee 12785 (setq end-point (point-at-eol))
a03c2342
WS
12786 (goto-char beg)
12787 (beginning-of-line) ; scan entire line
12788 ;; delete overlays existing on this line
12789 (let ((overlays (overlays-in (point) end-point)))
12790 (while overlays
12791 (if (and
12792 (overlay-get (car overlays) 'detachable)
12793 (or (overlay-get (car overlays) 'verilog-include-file)
12794 (overlay-get (car overlays) 'verilog-inst-module)))
12795 (delete-overlay (car overlays)))
12796 (setq overlays (cdr overlays))))
12797 ;;
12798 ;; make new include overlays
12799 (when verilog-highlight-includes
12800 (while (search-forward-regexp verilog-include-file-regexp end-point t)
12801 (goto-char (match-beginning 1))
12802 (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
12803 (overlay-put ov 'start-closed 't)
12804 (overlay-put ov 'end-closed 't)
12805 (overlay-put ov 'evaporate 't)
12806 (overlay-put ov 'verilog-include-file 't)
12807 (overlay-put ov 'mouse-face 'highlight)
12808 (overlay-put ov 'local-map verilog-mode-mouse-map))))
12809 ;;
12810 ;; make new module overlays
12811 (goto-char beg)
12812 ;; This scanner is syntax-fragile, so don't get bent
12813 (when verilog-highlight-modules
12814 (condition-case nil
9489a450 12815 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
a03c2342
WS
12816 (save-excursion
12817 (goto-char (match-beginning 0))
9489a450 12818 (unless (verilog-inside-comment-or-string-p)
a03c2342
WS
12819 (verilog-read-inst-module-matcher) ;; sets match 0
12820 (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
12821 (overlay-put ov 'start-closed 't)
12822 (overlay-put ov 'end-closed 't)
12823 (overlay-put ov 'evaporate 't)
12824 (overlay-put ov 'verilog-inst-module 't)
12825 (overlay-put ov 'mouse-face 'highlight)
12826 (overlay-put ov 'local-map verilog-mode-mouse-map)))))
12827 (error nil)))
12828 ;;
12829 ;; Future highlights:
12830 ;; variables - make an Occur buffer of where referenced
12831 ;; pins - make an Occur buffer of the sig in the declaration module
12832 )))))))
12833
12834(defun verilog-highlight-buffer ()
12835 "Colorize included files and modules across the whole buffer."
12836 ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
6341f357 12837 (interactive)
a03c2342
WS
12838 ;; delete and remake overlays
12839 (verilog-highlight-region (point-min) (point-max) nil))
12840
12841;; Deprecated, but was interactive, so we'll keep it around
12842(defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
6341f357 12843
37ea4b9b 12844;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
6341f357
DN
12845;; so define this function to do more or less the same as ffap-at-mouse
12846;; but first resolve filename...
12847(defun verilog-load-file-at-mouse (event)
12848 "Load file under button 2 click's EVENT.
a03c2342 12849Files are checked based on `verilog-library-flags'."
6341f357 12850 (interactive "@e")
37ea4b9b 12851 (save-excursion ;; implement a Verilog specific ffap-at-mouse
6341f357 12852 (mouse-set-point event)
a03c2342 12853 (verilog-load-file-at-point t)))
6341f357 12854
8350f087 12855;; ffap isn't usable for Verilog mode. It uses library paths.
6341f357
DN
12856;; so define this function to do more or less the same as ffap
12857;; but first resolve filename...
a03c2342 12858(defun verilog-load-file-at-point (&optional warn)
6341f357 12859 "Load file under point.
a03c2342
WS
12860If WARN, throw warning if not found.
12861Files are checked based on `verilog-library-flags'."
6341f357 12862 (interactive)
37ea4b9b 12863 (save-excursion ;; implement a Verilog specific ffap
a03c2342
WS
12864 (let ((overlays (overlays-in (point) (point)))
12865 hit)
12866 (while (and overlays (not hit))
12867 (when (overlay-get (car overlays) 'verilog-inst-module)
12868 (verilog-goto-defun-file (buffer-substring
12869 (overlay-start (car overlays))
12870 (overlay-end (car overlays))))
12871 (setq hit t))
12872 (setq overlays (cdr overlays)))
12873 ;; Include?
12874 (beginning-of-line)
12875 (when (and (not hit)
12876 (looking-at verilog-include-file-regexp))
12877 (if (and (car (verilog-library-filenames
12878 (match-string 1) (buffer-file-name)))
12879 (file-readable-p (car (verilog-library-filenames
12880 (match-string 1) (buffer-file-name)))))
6341f357 12881 (find-file (car (verilog-library-filenames
a03c2342
WS
12882 (match-string 1) (buffer-file-name))))
12883 (when warn
12884 (message
12885 "File '%s' isn't readable, use shift-mouse2 to paste in this field"
12886 (match-string 1))))))))
6341f357
DN
12887
12888;;
12889;; Bug reporting
12890;;
12891
12892(defun verilog-faq ()
12893 "Tell the user their current version, and where to get the FAQ etc."
12894 (interactive)
12895 (with-output-to-temp-buffer "*verilog-mode help*"
12896 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
12897 (princ "\n")
12898 (princ "For new releases, see http://www.verilog.com\n")
12899 (princ "\n")
7cb1c4d7 12900 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
6341f357
DN
12901 (princ "\n")
12902 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
12903 (princ "\n")))
12904
7ea26faf 12905(autoload 'reporter-submit-bug-report "reporter")
60618039 12906(defvar reporter-prompt-for-summary-p)
7ea26faf 12907
6341f357
DN
12908(defun verilog-submit-bug-report ()
12909 "Submit via mail a bug report on verilog-mode.el."
12910 (interactive)
12911 (let ((reporter-prompt-for-summary-p t))
12912 (reporter-submit-bug-report
86a4c7ac 12913 "mac@verilog.com, wsnyder@wsnyder.org"
6341f357
DN
12914 (concat "verilog-mode v" verilog-mode-version)
12915 '(
a03c2342 12916 verilog-active-low-regexp
6341f357 12917 verilog-align-ifelse
a03c2342
WS
12918 verilog-assignment-delay
12919 verilog-auto-arg-sort
6341f357
DN
12920 verilog-auto-endcomments
12921 verilog-auto-hook
a03c2342 12922 verilog-auto-ignore-concat
6341f357 12923 verilog-auto-indent-on-newline
a03c2342
WS
12924 verilog-auto-inout-ignore-regexp
12925 verilog-auto-input-ignore-regexp
12926 verilog-auto-inst-column
12927 verilog-auto-inst-dot-name
12928 verilog-auto-inst-param-value
6341f357 12929 verilog-auto-inst-template-numbers
a03c2342 12930 verilog-auto-inst-vector
6341f357
DN
12931 verilog-auto-lineup
12932 verilog-auto-newline
a03c2342
WS
12933 verilog-auto-output-ignore-regexp
12934 verilog-auto-read-includes
12935 verilog-auto-reset-widths
6341f357
DN
12936 verilog-auto-save-policy
12937 verilog-auto-sense-defines-constant
12938 verilog-auto-sense-include-inputs
a03c2342
WS
12939 verilog-auto-star-expand
12940 verilog-auto-star-save
12941 verilog-auto-unused-ignore-regexp
6341f357 12942 verilog-before-auto-hook
a03c2342
WS
12943 verilog-before-delete-auto-hook
12944 verilog-before-getopt-flags-hook
6341f357
DN
12945 verilog-case-indent
12946 verilog-cexp-indent
12947 verilog-compiler
12948 verilog-coverage
a03c2342
WS
12949 verilog-delete-auto-hook
12950 verilog-getopt-flags-hook
12951 verilog-highlight-grouping-keywords
12952 verilog-highlight-p1800-keywords
6341f357
DN
12953 verilog-highlight-translate-off
12954 verilog-indent-begin-after-if
12955 verilog-indent-declaration-macros
12956 verilog-indent-level
12957 verilog-indent-level-behavioral
12958 verilog-indent-level-declaration
12959 verilog-indent-level-directive
12960 verilog-indent-level-module
12961 verilog-indent-lists
6341f357
DN
12962 verilog-library-directories
12963 verilog-library-extensions
12964 verilog-library-files
a03c2342 12965 verilog-library-flags
6341f357
DN
12966 verilog-linter
12967 verilog-minimum-comment-distance
12968 verilog-mode-hook
a03c2342 12969 verilog-preprocessor
6341f357
DN
12970 verilog-simulator
12971 verilog-tab-always-indent
12972 verilog-tab-to-comment
a03c2342 12973 verilog-typedef-regexp
6341f357
DN
12974 )
12975 nil nil
12976 (concat "Hi Mac,
12977
86a4c7ac 12978I want to report a bug.
6341f357
DN
12979
12980Before I go further, I want to say that Verilog mode has changed my life.
12981I save so much time, my files are colored nicely, my co workers respect
12982my coding ability... until now. I'd really appreciate anything you
12983could do to help me out with this minor deficiency in the product.
12984
86a4c7ac 12985I've taken a look at the Verilog-Mode FAQ at
7cb1c4d7 12986http://www.veripool.org/verilog-mode-faq.html.
6341f357 12987
86a4c7ac
DN
12988And, I've considered filing the bug on the issue tracker at
12989http://www.veripool.org/verilog-mode-bugs
12990since I realize that public bugs are easier for you to track,
12991and for others to search, but would prefer to email.
12992
12993So, to reproduce the bug, start a fresh Emacs via " invocation-name "
37ea4b9b 12994-no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
6341f357
DN
12995the code included below.
12996
12997Given those lines, I expected [[Fill in here]] to happen;
12998but instead, [[Fill in here]] happens!.
12999
13000== The code: =="))))
13001
6edb5716
DN
13002(provide 'verilog-mode)
13003
6341f357
DN
13004;; Local Variables:
13005;; checkdoc-permit-comma-termination-flag:t
13006;; checkdoc-force-docstrings-flag:nil
13007;; End:
13008
13009;;; verilog-mode.el ends here