Typo fixes for Ada mode.
[emacs.git] / lisp / progmodes / verilog-mode.el
blob8ae43f0a59e29ce88615c5b34ba3ba0194c19ecb
1 ;; verilog-mode.el --- major mode for editing verilog source in Emacs
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Michael McNamara (mac@verilog.com)
7 ;; http://www.verilog.com
8 ;;
9 ;; AUTO features, signal, modsig; by: Wilson Snyder
10 ;; (wsnyder@wsnyder.org)
11 ;; http://www.veripool.com
12 ;; Keywords: languages
14 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
15 ;; file on 19/3/2008, and the maintainer agreed that when a bug is
16 ;; filed in the Emacs bug reporting system against this file, a copy
17 ;; of the bug report be sent to the maintainer's email address.
19 ;; This code supports Emacs 21.1 and later
20 ;; And XEmacs 21.1 and later
21 ;; Please do not make changes that break Emacs 21. Thanks!
25 ;; This file is part of GNU Emacs.
27 ;; GNU Emacs is free software: you can redistribute it and/or modify
28 ;; it under the terms of the GNU General Public License as published by
29 ;; the Free Software Foundation, either version 3 of the License, or
30 ;; (at your option) any later version.
32 ;; GNU Emacs is distributed in the hope that it will be useful,
33 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
34 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 ;; GNU General Public License for more details.
37 ;; You should have received a copy of the GNU General Public License
38 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
40 ;;; Commentary:
42 ;; This mode borrows heavily from the Pascal-mode and the cc-mode of Emacs
44 ;; USAGE
45 ;; =====
47 ;; A major mode for editing Verilog HDL source code. When you have
48 ;; entered Verilog mode, you may get more info by pressing C-h m. You
49 ;; may also get online help describing various functions by: C-h f
50 ;; <Name of function you want described>
52 ;; KNOWN BUGS / BUG REPORTS
53 ;; =======================
55 ;; Verilog is a rapidly evolving language, and hence this mode is
56 ;; under continuous development. Hence this is beta code, and likely
57 ;; has bugs. Please report any and all bugs to me at mac@verilog.com.
58 ;; Please use verilog-submit-bug-report to submit a report; type C-c
59 ;; C-b to invoke this and as a result I will have a much easier time
60 ;; of reproducing the bug you find, and hence fixing it.
62 ;; INSTALLING THE MODE
63 ;; ===================
65 ;; An older version of this mode may be already installed as a part of
66 ;; your environment, and one method of updating would be to update
67 ;; your Emacs environment. Sometimes this is difficult for local
68 ;; political/control reasons, and hence you can always install a
69 ;; private copy (or even a shared copy) which overrides the system
70 ;; default.
72 ;; You can get step by step help in installing this file by going to
73 ;; <http://www.verilog.com/emacs_install.html>
75 ;; The short list of installation instructions are: To set up
76 ;; automatic Verilog mode, put this file in your load path, and put
77 ;; the following in code (please un comment it first!) in your
78 ;; .emacs, or in your site's site-load.el
80 ; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
81 ; (setq auto-mode-alist (cons '("\\.v\\'" . verilog-mode) auto-mode-alist))
82 ; (setq auto-mode-alist (cons '("\\.dv\\'" . verilog-mode) auto-mode-alist))
84 ;; If you want to customize Verilog mode to fit your needs better,
85 ;; you may add these lines (the values of the variables presented
86 ;; here are the defaults). Note also that if you use an Emacs that
87 ;; supports custom, it's probably better to use the custom menu to
88 ;; edit these.
90 ;; Be sure to examine at the help for verilog-auto, and the other
91 ;; verilog-auto-* functions for some major coding time savers.
93 ; ;; User customization for Verilog mode
94 ; (setq verilog-indent-level 3
95 ; verilog-indent-level-module 3
96 ; verilog-indent-level-declaration 3
97 ; verilog-indent-level-behavioral 3
98 ; verilog-indent-level-directive 1
99 ; verilog-case-indent 2
100 ; verilog-auto-newline t
101 ; verilog-auto-indent-on-newline t
102 ; verilog-tab-always-indent t
103 ; verilog-auto-endcomments t
104 ; verilog-minimum-comment-distance 40
105 ; verilog-indent-begin-after-if t
106 ; verilog-auto-lineup '(all)
107 ; verilog-highlight-p1800-keywords nil
108 ; verilog-linter "my_lint_shell_command"
111 ;; \f
113 ;;; History:
115 ;; See commit history at http://www.veripool.com/verilog-mode.html
116 ;; (This section is required to appease checkdoc.)
118 ;;; Code:
120 ;; This variable will always hold the version number of the mode
121 (defconst verilog-mode-version "423"
122 "Version of this Verilog mode.")
123 (defconst verilog-mode-release-date "2008-05-07-GNU"
124 "Release date of this Verilog mode.")
125 (defconst verilog-mode-release-emacs t
126 "If non-nil, this version of Verilog mode was released with Emacs itself.")
128 (defun verilog-version ()
129 "Inform caller of the version of this file."
130 (interactive)
131 (message "Using verilog-mode version %s" verilog-mode-version))
133 ;; Insure we have certain packages, and deal with it if we don't
134 ;; Be sure to note which Emacs flavor and version added each feature.
135 (eval-when-compile
136 ;; The below were disabled when GNU Emacs 22 was released;
137 ;; perhaps some still need to be there to support Emacs 21.
138 (when (featurep 'xemacs)
139 (condition-case nil
140 (require 'easymenu)
141 (error nil))
142 (condition-case nil
143 (require 'regexp-opt)
144 (error nil))
145 ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
146 (condition-case nil
147 (load "skeleton")
148 (error nil))
149 (condition-case nil
150 (if (fboundp 'when)
151 nil ;; fab
152 (defmacro when (cond &rest body)
153 (list 'if cond (cons 'progn body))))
154 (error nil))
155 (condition-case nil
156 (if (fboundp 'unless)
157 nil ;; fab
158 (defmacro unless (cond &rest body)
159 (cons 'if (cons cond (cons nil body)))))
160 (error nil))
161 (condition-case nil
162 (if (fboundp 'store-match-data)
163 nil ;; fab
164 (defmacro store-match-data (&rest args) nil))
165 (error nil))
166 (condition-case nil
167 (if (fboundp 'char-before)
168 nil ;; great
169 (defmacro char-before (&rest body)
170 (char-after (1- (point)))))
171 (error nil))
172 (condition-case nil
173 (require 'custom)
174 (error nil))
175 (condition-case nil
176 (if (fboundp 'match-string-no-properties)
177 nil ;; great
178 (defsubst match-string-no-properties (num &optional string)
179 "Return string of text matched by last search, without text properties.
180 NUM specifies which parenthesized expression in the last regexp.
181 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
182 Zero means the entire text matched by the whole regexp or whole string.
183 STRING should be given if the last search was by `string-match' on STRING."
184 (if (match-beginning num)
185 (if string
186 (let ((result
187 (substring string
188 (match-beginning num) (match-end num))))
189 (set-text-properties 0 (length result) nil result)
190 result)
191 (buffer-substring-no-properties (match-beginning num)
192 (match-end num)
193 (current-buffer)))))
195 (error nil))
196 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
197 nil ;; We've got what we needed
198 ;; We have the old custom-library, hack around it!
199 (defmacro defgroup (&rest args) nil)
200 (defmacro customize (&rest args)
201 (message
202 "Sorry, Customize is not available with this version of Emacs"))
203 (defmacro defcustom (var value doc &rest args)
204 `(defvar ,var ,value ,doc))
206 (if (fboundp 'defface)
207 nil ; great!
208 (defmacro defface (var values doc &rest args)
209 `(make-face ,var))
212 (if (and (featurep 'custom) (fboundp 'customize-group))
213 nil ;; We've got what we needed
214 ;; We have an intermediate custom-library, hack around it!
215 (defmacro customize-group (var &rest args)
216 `(customize ,var))
219 ;; Provide a regular expression optimization routine, using regexp-opt
220 ;; if provided by the user's elisp libraries
221 (eval-and-compile
222 ;; The below were disabled when GNU Emacs 22 was released;
223 ;; perhaps some still need to be there to support Emacs 21.
224 (if (featurep 'xemacs)
225 (if (fboundp 'regexp-opt)
226 ;; regexp-opt is defined, does it take 3 or 2 arguments?
227 (if (fboundp 'function-max-args)
228 (let ((args (function-max-args `regexp-opt)))
229 (cond
230 ((eq args 3) ;; It takes 3
231 (condition-case nil ; Hide this defun from emacses
232 ;with just a two input regexp
233 (defun verilog-regexp-opt (a b)
234 "Deal with differing number of required arguments for `regexp-opt'.
235 Call 'regexp-opt' on A and B."
236 (regexp-opt a b 't))
237 (error nil))
239 ((eq args 2) ;; It takes 2
240 (defun verilog-regexp-opt (a b)
241 "Call 'regexp-opt' on A and B."
242 (regexp-opt a b))
244 (t nil)))
245 ;; We can't tell; assume it takes 2
246 (defun verilog-regexp-opt (a b)
247 "Call 'regexp-opt' on A and B."
248 (regexp-opt a b))
250 ;; There is no regexp-opt, provide our own
251 (defun verilog-regexp-opt (strings &optional paren shy)
252 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
253 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
255 ;; Emacs.
256 (defalias 'verilog-regexp-opt 'regexp-opt)))
258 (eval-when-compile
259 (defun verilog-regexp-words (a)
260 "Call 'regexp-opt' with word delimiters for the words A."
261 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
263 (defun verilog-easy-menu-filter (menu)
264 "Filter a easy-menu-define to support new features."
265 (cond ((not (featurep 'xemacs))
266 menu) ;; GNU Emacs - passthru
267 ;; Xemacs doesn't support :help. Strip it.
268 ;; Recursively filter the a submenu
269 ((listp menu)
270 (mapcar 'verilog-easy-menu-filter menu))
271 ;; Look for [:help "blah"] and remove
272 ((vectorp menu)
273 (let ((i 0) (out []))
274 (while (< i (length menu))
275 (if (equal `:help (aref menu i))
276 (setq i (+ 2 i))
277 (setq out (vconcat out (vector (aref menu i)))
278 i (1+ i))))
279 out))
280 (t menu))) ;; Default - ok
281 ;;(verilog-easy-menu-filter
282 ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
283 ;; "----" ["MB" nil :help "Help MB"]))
285 (defun verilog-customize ()
286 "Customize variables and other settings used by Verilog-Mode."
287 (interactive)
288 (customize-group 'verilog-mode))
290 (defun verilog-font-customize ()
291 "Customize fonts used by Verilog-Mode."
292 (interactive)
293 (if (fboundp 'customize-apropos)
294 (customize-apropos "font-lock-*" 'faces)))
296 (defun verilog-booleanp (value)
297 "Return t if VALUE is boolean.
298 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
299 This function may be removed when Emacs 21 is no longer supported."
300 (or (equal value t) (equal value nil)))
302 (defalias 'verilog-syntax-ppss
303 (if (fboundp 'syntax-ppss) 'syntax-ppss
304 (lambda (&optional pos) (parse-partial-sexp (point-min) (or pos (point))))))
306 (defgroup verilog-mode nil
307 "Facilitates easy editing of Verilog source text."
308 :version "22.2"
309 :group 'languages)
311 ; (defgroup verilog-mode-fonts nil
312 ; "Facilitates easy customization fonts used in Verilog source text"
313 ; :link '(customize-apropos "font-lock-*" 'faces)
314 ; :group 'verilog-mode)
316 (defgroup verilog-mode-indent nil
317 "Customize indentation and highlighting of Verilog source text."
318 :group 'verilog-mode)
320 (defgroup verilog-mode-actions nil
321 "Customize actions on Verilog source text."
322 :group 'verilog-mode)
324 (defgroup verilog-mode-auto nil
325 "Customize AUTO actions when expanding Verilog source text."
326 :group 'verilog-mode)
328 (defcustom verilog-linter
329 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
330 "*Unix program and arguments to call to run a lint checker on Verilog source.
331 Depending on the `verilog-set-compile-command', this may be invoked when
332 you type \\[compile]. When the compile completes, \\[next-error] will take
333 you to the next lint error."
334 :type 'string
335 :group 'verilog-mode-actions)
336 ;; We don't mark it safe, as it's used as a shell command
338 (defcustom verilog-coverage
339 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
340 "*Program and arguments to use to annotate for coverage Verilog source.
341 Depending on the `verilog-set-compile-command', this may be invoked when
342 you type \\[compile]. When the compile completes, \\[next-error] will take
343 you to the next lint error."
344 :type 'string
345 :group 'verilog-mode-actions)
346 ;; We don't mark it safe, as it's used as a shell command
348 (defcustom verilog-simulator
349 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
350 "*Program and arguments to use to interpret Verilog source.
351 Depending on the `verilog-set-compile-command', this may be invoked when
352 you type \\[compile]. When the compile completes, \\[next-error] will take
353 you to the next lint error."
354 :type 'string
355 :group 'verilog-mode-actions)
356 ;; We don't mark it safe, as it's used as a shell command
358 (defcustom verilog-compiler
359 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
360 "*Program and arguments to use to compile Verilog source.
361 Depending on the `verilog-set-compile-command', this may be invoked when
362 you type \\[compile]. When the compile completes, \\[next-error] will take
363 you to the next lint error."
364 :type 'string
365 :group 'verilog-mode-actions)
366 ;; We don't mark it safe, as it's used as a shell command
368 (defvar verilog-tool 'verilog-linter
369 "Which tool to use for building compiler-command.
370 Either nil, `verilog-linter, `verilog-coverage, `verilog-simulator, or
371 `verilog-compiler. Alternatively use the \"Choose Compilation Action\"
372 menu. See `verilog-set-compile-command' for more information.")
374 (defcustom verilog-highlight-translate-off nil
375 "*Non-nil means background-highlight code excluded from translation.
376 That is, all code between \"// synopsys translate_off\" and
377 \"// synopsys translate_on\" is highlighted using a different background color
378 \(face `verilog-font-lock-translate-off-face').
380 Note: This will slow down on-the-fly fontification (and thus editing).
382 Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
383 entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
384 :type 'boolean
385 :group 'verilog-mode-indent)
386 ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
387 (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
389 (defcustom verilog-indent-level 3
390 "*Indentation of Verilog statements with respect to containing block."
391 :group 'verilog-mode-indent
392 :type 'integer)
393 (put 'verilog-indent-level 'safe-local-variable 'integerp)
395 (defcustom verilog-indent-level-module 3
396 "*Indentation of Module level Verilog statements (eg always, initial).
397 Set to 0 to get initial and always statements lined up on the left side of
398 your screen."
399 :group 'verilog-mode-indent
400 :type 'integer)
401 (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
403 (defcustom verilog-indent-level-declaration 3
404 "*Indentation of declarations with respect to containing block.
405 Set to 0 to get them list right under containing block."
406 :group 'verilog-mode-indent
407 :type 'integer)
408 (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
410 (defcustom verilog-indent-declaration-macros nil
411 "*How to treat macro expansions in a declaration.
412 If nil, indent as:
413 input [31:0] a;
414 input `CP;
415 output c;
416 If non nil, treat as:
417 input [31:0] a;
418 input `CP ;
419 output c;"
420 :group 'verilog-mode-indent
421 :type 'boolean)
422 (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
424 (defcustom verilog-indent-lists t
425 "*How to treat indenting items in a list.
426 If t (the default), indent as:
427 always @( posedge a or
428 reset ) begin
430 If nil, treat as:
431 always @( posedge a or
432 reset ) begin"
433 :group 'verilog-mode-indent
434 :type 'boolean)
435 (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
437 (defcustom verilog-indent-level-behavioral 3
438 "*Absolute indentation of first begin in a task or function block.
439 Set to 0 to get such code to start at the left side of the screen."
440 :group 'verilog-mode-indent
441 :type 'integer)
442 (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
444 (defcustom verilog-indent-level-directive 1
445 "*Indentation to add to each level of `ifdef declarations.
446 Set to 0 to have all directives start at the left side of the screen."
447 :group 'verilog-mode-indent
448 :type 'integer)
449 (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
451 (defcustom verilog-cexp-indent 2
452 "*Indentation of Verilog statements split across lines."
453 :group 'verilog-mode-indent
454 :type 'integer)
455 (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
457 (defcustom verilog-case-indent 2
458 "*Indentation for case statements."
459 :group 'verilog-mode-indent
460 :type 'integer)
461 (put 'verilog-case-indent 'safe-local-variable 'integerp)
463 (defcustom verilog-auto-newline t
464 "*True means automatically newline after semicolons."
465 :group 'verilog-mode-indent
466 :type 'boolean)
467 (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
469 (defcustom verilog-auto-indent-on-newline t
470 "*True means automatically indent line after newline."
471 :group 'verilog-mode-indent
472 :type 'boolean)
473 (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
475 (defcustom verilog-tab-always-indent t
476 "*True means TAB should always re-indent the current line.
477 A nil value means TAB will only reindent when at the beginning of the line."
478 :group 'verilog-mode-indent
479 :type 'boolean)
480 (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
482 (defcustom verilog-tab-to-comment nil
483 "*True means TAB moves to the right hand column in preparation for a comment."
484 :group 'verilog-mode-actions
485 :type 'boolean)
486 (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
488 (defcustom verilog-indent-begin-after-if t
489 "*If true, indent begin statements following if, else, while, for and repeat.
490 Otherwise, line them up."
491 :group 'verilog-mode-indent
492 :type 'boolean)
493 (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
496 (defcustom verilog-align-ifelse nil
497 "*If true, align `else' under matching `if'.
498 Otherwise else is lined up with first character on line holding matching if."
499 :group 'verilog-mode-indent
500 :type 'boolean)
501 (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
503 (defcustom verilog-minimum-comment-distance 10
504 "*Minimum distance (in lines) between begin and end required before a comment.
505 Setting this variable to zero results in every end acquiring a comment; the
506 default avoids too many redundant comments in tight quarters."
507 :group 'verilog-mode-indent
508 :type 'integer)
509 (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
511 (defcustom verilog-auto-lineup '(declaration)
512 "*Algorithm for lining up statements on multiple lines.
514 If this list contains the symbol 'all', then all line ups described below
515 are done.
517 If this list contains the symbol 'declaration', then declarations are lined up
518 with any preceding declarations, taking into account widths and the like, so
519 for example the code:
520 reg [31:0] a;
521 reg b;
522 would become
523 reg [31:0] a;
524 reg b;
526 If this list contains the symbol 'assignment', then assignments are lined up
527 with any preceding assignments, so for example the code
528 a_long_variable = b + c;
529 d = e + f;
530 would become
531 a_long_variable = b + c;
532 d = e + f;"
534 ;; The following is not implemented:
535 ;If this list contains the symbol 'case', then case items are lined up
536 ;with any preceding case items, so for example the code
537 ; case (a) begin
538 ; a_long_state : a = 3;
539 ; b: a = 4;
540 ; endcase
541 ;would become
542 ; case (a) begin
543 ; a_long_state : a = 3;
544 ; b : a = 4;
545 ; endcase
548 :group 'verilog-mode-indent
549 :type 'list)
550 (put 'verilog-auto-lineup 'safe-local-variable 'listp)
552 (defcustom verilog-highlight-p1800-keywords nil
553 "*True means highlight words newly reserved by IEEE-1800.
554 These will appear in `verilog-font-lock-p1800-face' in order to gently
555 suggest changing where these words are used as variables to something else.
556 A nil value means highlight these words as appropriate for the SystemVerilog
557 IEEE-1800 standard. Note that changing this will require restarting Emacs
558 to see the effect as font color choices are cached by Emacs."
559 :group 'verilog-mode-indent
560 :type 'boolean)
561 (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
563 (defcustom verilog-highlight-grouping-keywords nil
564 "*True means highlight grouping keywords 'begin' and 'end' more dramatically.
565 If false, these words are in the font-lock-type-face; if True then they are in
566 `verilog-font-lock-ams-face'. Some find that special highlighting on these
567 grouping constructs allow the structure of the code to be understood at a glance."
568 :group 'verilog-mode-indent
569 :type 'boolean)
570 (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
572 (defcustom verilog-auto-endcomments t
573 "*True means insert a comment /* ... */ after 'end's.
574 The name of the function or case will be set between the braces."
575 :group 'verilog-mode-actions
576 :type 'boolean)
577 (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
579 (defcustom verilog-auto-read-includes nil
580 "*True means to automatically read includes before AUTOs.
581 This will do a `verilog-read-defines' and `verilog-read-includes' before
582 each AUTO expansion. This makes it easier to embed defines and includes,
583 but can result in very slow reading times if there are many or large
584 include files."
585 :group 'verilog-mode-actions
586 :type 'boolean)
587 (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
589 (defcustom verilog-auto-save-policy nil
590 "*Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
591 A value of `force' will always do a \\[verilog-auto] automatically if
592 needed on every save. A value of `detect' will do \\[verilog-auto]
593 automatically when it thinks necessary. A value of `ask' will query the
594 user when it thinks updating is needed.
596 You should not rely on the 'ask or 'detect policies, they are safeguards
597 only. They do not detect when AUTOINSTs need to be updated because a
598 sub-module's port list has changed."
599 :group 'verilog-mode-actions
600 :type '(choice (const nil) (const ask) (const detect) (const force)))
602 (defcustom verilog-auto-star-expand t
603 "*Non-nil indicates to expand a SystemVerilog .* instance ports.
604 They will be expanded in the same way as if there was a AUTOINST in the
605 instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
606 :group 'verilog-mode-actions
607 :type 'boolean)
608 (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
610 (defcustom verilog-auto-star-save nil
611 "*Non-nil indicates to save to disk SystemVerilog .* instance expansions.
612 A nil value indicates direct connections will be removed before saving.
613 Only meaningful to those created due to `verilog-auto-star-expand' being set.
615 Instead of setting this, you may want to use /*AUTOINST*/, which will
616 always be saved."
617 :group 'verilog-mode-actions
618 :type 'boolean)
619 (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
621 (defvar verilog-auto-update-tick nil
622 "Modification tick at which autos were last performed.")
624 (defvar verilog-auto-last-file-locals nil
625 "Text from file-local-variables during last evaluation.")
627 (defvar verilog-error-regexp-add-didit nil)
628 (defvar verilog-error-regexp nil)
629 (setq verilog-error-regexp-add-didit nil
630 verilog-error-regexp
632 ; SureLint
633 ;; ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
634 ; Most SureFire tools
635 ("\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\), \\(line \\|\\)\\([0-9]+\\):" 2 4 )
637 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
638 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
639 ; xsim
640 ; Error! in file /homes/mac/Axis/Xsim/test.v at line 13 [OBJ_NOT_DECLARED]
641 ("\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
642 ; vcs
643 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
644 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
645 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
646 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
647 ; Verilator
648 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
649 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
650 ; vxl
651 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
652 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\([0-9]+\\):.*$" 1 2) ; vxl
653 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+line[ \t]+\\([0-9]+\\):.*$" 1 2)
654 ; nc-verilog
655 (".*\\*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)|" 1 2)
656 ; Leda
657 ("In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\[\\(Warning\\|Error\\|Failure\\)\\][^\n]*" 1 2)
659 ; "*List of regexps for Verilog compilers, like verilint. See compilation-error-regexp-alist for the formatting."
662 (defvar verilog-error-font-lock-keywords
664 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
665 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
667 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
668 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
671 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
672 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
674 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
675 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
677 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
678 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
680 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
681 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
683 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
684 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
686 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
687 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
689 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
690 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
691 ; vxl
692 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
693 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
695 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\([0-9]+\\):.*$" 1 bold t)
696 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\([0-9]+\\):.*$" 2 bold t)
698 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+line[ \t]+\\([0-9]+\\):.*$" 1 bold t)
699 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+line[ \t]+\\([0-9]+\\):.*$" 2 bold t)
700 ; nc-verilog
701 (".*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)|" 1 bold t)
702 (".*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
703 ; Leda
704 ("In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\[\\(Warning\\|Error\\|Failure\\)\\][^\n]*" 1 bold t)
705 ("In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\[\\(Warning\\|Error\\|Failure\\)\\][^\n]*" 2 bold t)
707 "*Keywords to also highlight in Verilog *compilation* buffers.")
709 (defcustom verilog-library-flags '("")
710 "*List of standard Verilog arguments to use for /*AUTOINST*/.
711 These arguments are used to find files for `verilog-auto', and match
712 the flags accepted by a standard Verilog-XL simulator.
714 -f filename Reads more `verilog-library-flags' from the filename.
715 +incdir+dir Adds the directory to `verilog-library-directories'.
716 -Idir Adds the directory to `verilog-library-directories'.
717 -y dir Adds the directory to `verilog-library-directories'.
718 +libext+.v Adds the extensions to `verilog-library-extensions'.
719 -v filename Adds the filename to `verilog-library-files'.
721 filename Adds the filename to `verilog-library-files'.
722 This is not recommended, -v is a better choice.
724 You might want these defined in each file; put at the *END* of your file
725 something like:
727 // Local Variables:
728 // verilog-library-flags:(\"-y dir -y otherdir\")
729 // End:
731 Verilog-mode attempts to detect changes to this local variable, but they
732 are only insured to be correct when the file is first visited. Thus if you
733 have problems, use \\[find-alternate-file] RET to have these take effect.
735 See also the variables mentioned above."
736 :group 'verilog-mode-auto
737 :type '(repeat string))
738 (put 'verilog-library-flags 'safe-local-variable 'listp)
740 (defcustom verilog-library-directories '(".")
741 "*List of directories when looking for files for /*AUTOINST*/.
742 The directory may be relative to the current file, or absolute.
743 Environment variables are also expanded in the directory names.
744 Having at least the current directory is a good idea.
746 You might want these defined in each file; put at the *END* of your file
747 something like:
749 // Local Variables:
750 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
751 // End:
753 Verilog-mode attempts to detect changes to this local variable, but they
754 are only insured to be correct when the file is first visited. Thus if you
755 have problems, use \\[find-alternate-file] RET to have these take effect.
757 See also `verilog-library-flags', `verilog-library-files'
758 and `verilog-library-extensions'."
759 :group 'verilog-mode-auto
760 :type '(repeat file))
761 (put 'verilog-library-directories 'safe-local-variable 'listp)
763 (defcustom verilog-library-files '()
764 "*List of files to search for modules.
765 AUTOINST will use this when it needs to resolve a module name.
766 This is a complete path, usually to a technology file with many standard
767 cells defined in it.
769 You might want these defined in each file; put at the *END* of your file
770 something like:
772 // Local Variables:
773 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
774 // End:
776 Verilog-mode attempts to detect changes to this local variable, but they
777 are only insured to be correct when the file is first visited. Thus if you
778 have problems, use \\[find-alternate-file] RET to have these take effect.
780 See also `verilog-library-flags', `verilog-library-directories'."
781 :group 'verilog-mode-auto
782 :type '(repeat directory))
783 (put 'verilog-library-files 'safe-local-variable 'listp)
785 (defcustom verilog-library-extensions '(".v")
786 "*List of extensions to use when looking for files for /*AUTOINST*/.
787 See also `verilog-library-flags', `verilog-library-directories'."
788 :type '(repeat string)
789 :group 'verilog-mode-auto)
790 (put 'verilog-library-extensions 'safe-local-variable 'listp)
792 (defcustom verilog-active-low-regexp nil
793 "*If set, treat signals matching this regexp as active low.
794 This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
795 you will probably also need `verilog-auto-reset-widths' set."
796 :group 'verilog-mode-auto
797 :type 'string)
798 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
800 (defcustom verilog-auto-sense-include-inputs nil
801 "*If true, AUTOSENSE should include all inputs.
802 If nil, only inputs that are NOT output signals in the same block are
803 included."
804 :group 'verilog-mode-auto
805 :type 'boolean)
806 (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
808 (defcustom verilog-auto-sense-defines-constant nil
809 "*If true, AUTOSENSE should assume all defines represent constants.
810 When true, the defines will not be included in sensitivity lists. To
811 maintain compatibility with other sites, this should be set at the bottom
812 of each Verilog file that requires it, rather than being set globally."
813 :group 'verilog-mode-auto
814 :type 'boolean)
815 (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
817 (defcustom verilog-auto-reset-widths t
818 "*If true, AUTORESET should determine the width of signals.
819 This is then used to set the width of the zero (32'h0 for example). This
820 is required by some lint tools that aren't smart enough to ignore widths of
821 the constant zero. This may result in ugly code when parameters determine
822 the MSB or LSB of a signal inside an AUTORESET."
823 :type 'boolean
824 :group 'verilog-mode-auto)
825 (put 'verilog-auto-reset-widths 'safe-local-variable 'verilog-booleanp)
827 (defcustom verilog-assignment-delay ""
828 "*Text used for delays in delayed assignments. Add a trailing space if set."
829 :group 'verilog-mode-auto
830 :type 'string)
831 (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
833 (defcustom verilog-auto-inst-vector t
834 "*If true, when creating default ports with AUTOINST, use bus subscripts.
835 If nil, skip the subscript when it matches the entire bus as declared in
836 the module (AUTOWIRE signals always are subscripted, you must manually
837 declare the wire to have the subscripts removed.) Setting this to nil may
838 speed up some simulators, but is less general and harder to read, so avoid."
839 :group 'verilog-mode-auto
840 :type 'boolean)
841 (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
843 (defcustom verilog-auto-inst-template-numbers nil
844 "*If true, when creating templated ports with AUTOINST, add a comment.
845 The comment will add the line number of the template that was used for that
846 port declaration. Setting this aids in debugging, but nil is suggested for
847 regular use to prevent large numbers of merge conflicts."
848 :group 'verilog-mode-auto
849 :type 'boolean)
850 (put 'verilog-auto-inst-template-numbers 'safe-local-variable 'verilog-booleanp)
852 (defvar verilog-auto-inst-column 40
853 "Column number for first part of auto-inst.")
855 (defcustom verilog-auto-input-ignore-regexp nil
856 "*If set, when creating AUTOINPUT list, ignore signals matching this regexp.
857 See the \\[verilog-faq] for examples on using this."
858 :group 'verilog-mode-auto
859 :type 'string)
860 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
862 (defcustom verilog-auto-inout-ignore-regexp nil
863 "*If set, when creating AUTOINOUT list, ignore signals matching this regexp.
864 See the \\[verilog-faq] for examples on using this."
865 :group 'verilog-mode-auto
866 :type 'string)
867 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
869 (defcustom verilog-auto-output-ignore-regexp nil
870 "*If set, when creating AUTOOUTPUT list, ignore signals matching this regexp.
871 See the \\[verilog-faq] for examples on using this."
872 :group 'verilog-mode-auto
873 :type 'string)
874 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
876 (defcustom verilog-auto-unused-ignore-regexp nil
877 "*If set, when creating AUTOUNUSED list, ignore signals matching this regexp.
878 See the \\[verilog-faq] for examples on using this."
879 :group 'verilog-mode-auto
880 :type 'string)
881 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
883 (defcustom verilog-typedef-regexp nil
884 "*If non-nil, regular expression that matches Verilog-2001 typedef names.
885 For example, \"_t$\" matches typedefs named with _t, as in the C language."
886 :group 'verilog-mode-auto
887 :type 'string)
888 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
890 (defcustom verilog-mode-hook 'verilog-set-compile-command
891 "*Hook run after Verilog mode is loaded."
892 :type 'hook
893 :group 'verilog-mode)
895 (defcustom verilog-auto-hook nil
896 "*Hook run after `verilog-mode' updates AUTOs."
897 :group 'verilog-mode-auto
898 :type 'hook)
900 (defcustom verilog-before-auto-hook nil
901 "*Hook run before `verilog-mode' updates AUTOs."
902 :group 'verilog-mode-auto
903 :type 'hook)
905 (defcustom verilog-delete-auto-hook nil
906 "*Hook run after `verilog-mode' deletes AUTOs."
907 :group 'verilog-mode-auto
908 :type 'hook)
910 (defcustom verilog-before-delete-auto-hook nil
911 "*Hook run before `verilog-mode' deletes AUTOs."
912 :group 'verilog-mode-auto
913 :type 'hook)
915 (defcustom verilog-getopt-flags-hook nil
916 "*Hook run after `verilog-getopt-flags' determines the Verilog option lists."
917 :group 'verilog-mode-auto
918 :type 'hook)
920 (defcustom verilog-before-getopt-flags-hook nil
921 "*Hook run before `verilog-getopt-flags' determines the Verilog option lists."
922 :group 'verilog-mode-auto
923 :type 'hook)
925 (defvar verilog-imenu-generic-expression
926 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
927 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
928 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
931 ;; provide a verilog-header function.
932 ;; Customization variables:
934 (defvar verilog-date-scientific-format nil
935 "*If non-nil, dates are written in scientific format (e.g. 1997/09/17).
936 If nil, in European format (e.g. 17.09.1997). The brain-dead American
937 format (e.g. 09/17/1997) is not supported.")
939 (defvar verilog-company nil
940 "*Default name of Company for Verilog header.
941 If set will become buffer local.")
942 (make-variable-buffer-local 'verilog-company)
944 (defvar verilog-project nil
945 "*Default name of Project for Verilog header.
946 If set will become buffer local.")
947 (make-variable-buffer-local 'verilog-project)
949 (defvar verilog-mode-map
950 (let ((map (make-sparse-keymap)))
951 (define-key map ";" 'electric-verilog-semi)
952 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
953 (define-key map ":" 'electric-verilog-colon)
954 ;;(define-key map "=" 'electric-verilog-equal)
955 (define-key map "\`" 'electric-verilog-tick)
956 (define-key map "\t" 'electric-verilog-tab)
957 (define-key map "\r" 'electric-verilog-terminate-line)
958 ;; backspace/delete key bindings
959 (define-key map [backspace] 'backward-delete-char-untabify)
960 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
961 (define-key map [delete] 'delete-char)
962 (define-key map [(meta delete)] 'kill-word))
963 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
964 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
965 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
966 (define-key map "\M-\t" 'verilog-complete-word)
967 (define-key map "\M-?" 'verilog-show-completions)
968 (define-key map "\C-c\`" 'verilog-lint-off)
969 (define-key map "\C-c\*" 'verilog-delete-auto-star-implicit)
970 (define-key map "\C-c\C-r" 'verilog-label-be)
971 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
972 (define-key map "\C-c=" 'verilog-pretty-expr)
973 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
974 (define-key map "\M-*" 'verilog-star-comment)
975 (define-key map "\C-c\C-c" 'verilog-comment-region)
976 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
977 (when (featurep 'xemacs)
978 (define-key map [(meta control h)] 'verilog-mark-defun)
979 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
980 (define-key map "\M-\C-e" 'verilog-end-of-defun))
981 (define-key map "\C-c\C-d" 'verilog-goto-defun)
982 (define-key map "\C-c\C-k" 'verilog-delete-auto)
983 (define-key map "\C-c\C-a" 'verilog-auto)
984 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
985 (define-key map "\C-c\C-z" 'verilog-inject-auto)
986 (define-key map "\C-c\C-e" 'verilog-expand-vector)
987 (define-key map "\C-c\C-h" 'verilog-header)
988 map)
989 "Keymap used in Verilog mode.")
991 ;; menus
992 (easy-menu-define
993 verilog-menu verilog-mode-map "Menu for Verilog mode"
994 (verilog-easy-menu-filter
995 '("Verilog"
996 ("Choose Compilation Action"
997 ["None"
998 (progn
999 (setq verilog-tool nil)
1000 (verilog-set-compile-command))
1001 :style radio
1002 :selected (equal verilog-tool nil)
1003 :help "When invoking compilation, use compile-command"]
1004 ["Lint"
1005 (progn
1006 (setq verilog-tool 'verilog-linter)
1007 (verilog-set-compile-command))
1008 :style radio
1009 :selected (equal verilog-tool `verilog-linter)
1010 :help "When invoking compilation, use lint checker"]
1011 ["Coverage"
1012 (progn
1013 (setq verilog-tool 'verilog-coverage)
1014 (verilog-set-compile-command))
1015 :style radio
1016 :selected (equal verilog-tool `verilog-coverage)
1017 :help "When invoking compilation, annotate for coverage"]
1018 ["Simulator"
1019 (progn
1020 (setq verilog-tool 'verilog-simulator)
1021 (verilog-set-compile-command))
1022 :style radio
1023 :selected (equal verilog-tool `verilog-simulator)
1024 :help "When invoking compilation, interpret Verilog source"]
1025 ["Compiler"
1026 (progn
1027 (setq verilog-tool 'verilog-compiler)
1028 (verilog-set-compile-command))
1029 :style radio
1030 :selected (equal verilog-tool `verilog-compiler)
1031 :help "When invoking compilation, compile Verilog source"]
1033 ("Move"
1034 ["Beginning of function" verilog-beg-of-defun
1035 :keys "C-M-a"
1036 :help "Move backward to the beginning of the current function or procedure"]
1037 ["End of function" verilog-end-of-defun
1038 :keys "C-M-e"
1039 :help "Move forward to the end of the current function or procedure"]
1040 ["Mark function" verilog-mark-defun
1041 :keys "C-M-h"
1042 :help "Mark the current Verilog function or procedure"]
1043 ["Goto function/module" verilog-goto-defun
1044 :help "Move to specified Verilog module/task/function"]
1045 ["Move to beginning of block" electric-verilog-backward-sexp
1046 :help "Move backward over one balanced expression"]
1047 ["Move to end of block" electric-verilog-forward-sexp
1048 :help "Move forward over one balanced expression"]
1050 ("Comments"
1051 ["Comment Region" verilog-comment-region
1052 :help "Put marked area into a comment"]
1053 ["UnComment Region" verilog-uncomment-region
1054 :help "Uncomment an area commented with Comment Region"]
1055 ["Multi-line comment insert" verilog-star-comment
1056 :help "Insert Verilog /* */ comment at point"]
1057 ["Lint error to comment" verilog-lint-off
1058 :help "Convert a Verilog linter warning line into a disable statement"]
1060 "----"
1061 ["Compile" compile
1062 :help "Perform compilation-action (above) on the current buffer"]
1063 ["AUTO, Save, Compile" verilog-auto-save-compile
1064 :help "Recompute AUTOs, save buffer, and compile"]
1065 ["Next Compile Error" next-error
1066 :help "Visit next compilation error message and corresponding source code"]
1067 ["Ignore Lint Warning at point" verilog-lint-off
1068 :help "Convert a Verilog linter warning line into a disable statement"]
1069 "----"
1070 ["Line up declarations around point" verilog-pretty-declarations
1071 :help "Line up declarations around point"]
1072 ["Line up equations around point" verilog-pretty-expr
1073 :help "Line up expressions around point"]
1074 ["Redo/insert comments on every end" verilog-label-be
1075 :help "Label matching begin ... end statements"]
1076 ["Expand [x:y] vector line" verilog-expand-vector
1077 :help "Take a signal vector on the current line and expand it to multiple lines"]
1078 ["Insert begin-end block" verilog-insert-block
1079 :help "Insert begin ... end"]
1080 ["Complete word" verilog-complete-word
1081 :help "Complete word at point"]
1082 "----"
1083 ["Recompute AUTOs" verilog-auto
1084 :help "Expand AUTO meta-comment statements"]
1085 ["Kill AUTOs" verilog-delete-auto
1086 :help "Remove AUTO expansions"]
1087 ["Inject AUTOs" verilog-inject-auto
1088 :help "Inject AUTOs into legacy non-AUTO buffer"]
1089 ("AUTO Help..."
1090 ["AUTO General" (describe-function 'verilog-auto)
1091 :help "Help introduction on AUTOs"]
1092 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1093 :help "Help on verilog-library-flags"]
1094 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1095 :help "Help on verilog-library-directories"]
1096 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1097 :help "Help on verilog-library-files"]
1098 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1099 :help "Help on verilog-library-extensions"]
1100 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1101 :help "Help on reading `defines"]
1102 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1103 :help "Help on parsing `includes"]
1104 ["AUTOARG" (describe-function 'verilog-auto-arg)
1105 :help "Help on AUTOARG - declaring module port list"]
1106 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1107 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1108 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1109 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1110 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1111 :help "Help on AUTOINOUT - adding inouts from cells"]
1112 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1113 :help "Help on AUTOINPUT - adding inputs from cells"]
1114 ["AUTOINST" (describe-function 'verilog-auto-inst)
1115 :help "Help on AUTOINST - adding pins for cells"]
1116 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1117 :help "Help on expanding Verilog-2001 .* pins"]
1118 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1119 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1120 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1121 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1122 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1123 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1124 ["AUTOREG" (describe-function 'verilog-auto-reg)
1125 :help "Help on AUTOREG - declaring registers for non-wires"]
1126 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1127 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1128 ["AUTORESET" (describe-function 'verilog-auto-reset)
1129 :help "Help on AUTORESET - resetting always blocks"]
1130 ["AUTOSENSE" (describe-function 'verilog-auto-sense)
1131 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1132 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1133 :help "Help on AUTOTIEOFF - tieing off unused outputs"]
1134 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1135 :help "Help on AUTOUNUSED - terminating unused inputs"]
1136 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1137 :help "Help on AUTOWIRE - declaring wires for cells"]
1139 "----"
1140 ["Submit bug report" verilog-submit-bug-report
1141 :help "Submit via mail a bug report on verilog-mode.el"]
1142 ["Version and FAQ" verilog-faq
1143 :help "Show the current version, and where to get the FAQ etc"]
1144 ["Customize Verilog Mode..." verilog-customize
1145 :help "Customize variables and other settings used by Verilog-Mode"]
1146 ["Customize Verilog Fonts & Colors" verilog-font-customize
1147 :help "Customize fonts used by Verilog-Mode."])))
1149 (easy-menu-define
1150 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1151 (verilog-easy-menu-filter
1152 '("Statements"
1153 ["Header" verilog-sk-header
1154 :help "Insert a header block at the top of file"]
1155 ["Comment" verilog-sk-comment
1156 :help "Insert a comment block"]
1157 "----"
1158 ["Module" verilog-sk-module
1159 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1160 ["Primitive" verilog-sk-primitive
1161 :help "Insert a primitive .. (.. );.. endprimitive block"]
1162 "----"
1163 ["Input" verilog-sk-input
1164 :help "Insert an input declaration"]
1165 ["Output" verilog-sk-output
1166 :help "Insert an output declaration"]
1167 ["Inout" verilog-sk-inout
1168 :help "Insert an inout declaration"]
1169 ["Wire" verilog-sk-wire
1170 :help "Insert a wire declaration"]
1171 ["Reg" verilog-sk-reg
1172 :help "Insert a register declaration"]
1173 ["Define thing under point as a register" verilog-sk-define-signal
1174 :help "Define signal under point as a register at the top of the module"]
1175 "----"
1176 ["Initial" verilog-sk-initial
1177 :help "Insert an initial begin .. end block"]
1178 ["Always" verilog-sk-always
1179 :help "Insert an always @(AS) begin .. end block"]
1180 ["Function" verilog-sk-function
1181 :help "Insert a function .. begin .. end endfunction block"]
1182 ["Task" verilog-sk-task
1183 :help "Insert a task .. begin .. end endtask block"]
1184 ["Specify" verilog-sk-specify
1185 :help "Insert a specify .. endspecify block"]
1186 ["Generate" verilog-sk-generate
1187 :help "Insert a generate .. endgenerate block"]
1188 "----"
1189 ["Begin" verilog-sk-begin
1190 :help "Insert a begin .. end block"]
1191 ["If" verilog-sk-if
1192 :help "Insert an if (..) begin .. end block"]
1193 ["(if) else" verilog-sk-else-if
1194 :help "Insert an else if (..) begin .. end block"]
1195 ["For" verilog-sk-for
1196 :help "Insert a for (...) begin .. end block"]
1197 ["While" verilog-sk-while
1198 :help "Insert a while (...) begin .. end block"]
1199 ["Fork" verilog-sk-fork
1200 :help "Insert a fork begin .. end .. join block"]
1201 ["Repeat" verilog-sk-repeat
1202 :help "Insert a repeat (..) begin .. end block"]
1203 ["Case" verilog-sk-case
1204 :help "Insert a case block, prompting for details"]
1205 ["Casex" verilog-sk-casex
1206 :help "Insert a casex (...) item: begin.. end endcase block"]
1207 ["Casez" verilog-sk-casez
1208 :help "Insert a casez (...) item: begin.. end endcase block"])))
1210 (defvar verilog-mode-abbrev-table nil
1211 "Abbrev table in use in Verilog-mode buffers.")
1213 (define-abbrev-table 'verilog-mode-abbrev-table ())
1216 ;; Macros
1219 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1220 "Replace occurrences of FROM-STRING with TO-STRING.
1221 FIXEDCASE and LITERAL as in `replace-match`. STRING is what to replace.
1222 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1223 will break, as the o's continuously replace. xa -> x works ok though."
1224 ;; Hopefully soon to a emacs built-in
1225 (let ((start 0))
1226 (while (string-match from-string string start)
1227 (setq string (replace-match to-string fixedcase literal string)
1228 start (min (length string) (match-end 0))))
1229 string))
1231 (defsubst verilog-string-remove-spaces (string)
1232 "Remove spaces surrounding STRING."
1233 (save-match-data
1234 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1235 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1236 string))
1238 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1239 ; checkdoc-params: (REGEXP BOUND NOERROR)
1240 "Like `re-search-forward', but skips over match in comments or strings."
1241 (store-match-data '(nil nil)) ;; So match-end will return nil if no matches found
1242 (while (and
1243 (re-search-forward REGEXP BOUND NOERROR)
1244 (and (verilog-skip-forward-comment-or-string)
1245 (progn
1246 (store-match-data '(nil nil))
1247 (if BOUND
1248 (< (point) BOUND)
1249 t)))))
1250 (match-end 0))
1252 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1253 ; checkdoc-params: (REGEXP BOUND NOERROR)
1254 "Like `re-search-backward', but skips over match in comments or strings."
1255 (store-match-data '(nil nil)) ;; So match-end will return nil if no matches found
1256 (while (and
1257 (re-search-backward REGEXP BOUND NOERROR)
1258 (and (verilog-skip-backward-comment-or-string)
1259 (progn
1260 (store-match-data '(nil nil))
1261 (if BOUND
1262 (> (point) BOUND)
1263 t)))))
1264 (match-end 0))
1266 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1267 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1268 but trashes match data and is faster for REGEXP that doesn't match often.
1269 This may at some point use text properties to ignore comments,
1270 so there may be a large up front penalty for the first search."
1271 (let (pt)
1272 (while (and (not pt)
1273 (re-search-forward regexp bound noerror))
1274 (if (not (verilog-inside-comment-p))
1275 (setq pt (match-end 0))))
1276 pt))
1278 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1279 ; checkdoc-params: (REGEXP BOUND NOERROR)
1280 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1281 but trashes match data and is faster for REGEXP that doesn't match often.
1282 This may at some point use text properties to ignore comments,
1283 so there may be a large up front penalty for the first search."
1284 (let (pt)
1285 (while (and (not pt)
1286 (re-search-backward regexp bound noerror))
1287 (if (not (verilog-inside-comment-p))
1288 (setq pt (match-end 0))))
1289 pt))
1291 (defsubst verilog-get-beg-of-line (&optional arg)
1292 (save-excursion
1293 (beginning-of-line arg)
1294 (point)))
1296 (defsubst verilog-get-end-of-line (&optional arg)
1297 (save-excursion
1298 (end-of-line arg)
1299 (point)))
1301 (defsubst verilog-within-string ()
1302 (save-excursion
1303 (nth 3 (parse-partial-sexp (verilog-get-beg-of-line) (point)))))
1305 (defvar compile-command)
1307 ;; compilation program
1308 (defun verilog-set-compile-command ()
1309 "Function to compute shell command to compile Verilog.
1311 This reads `verilog-tool' and sets `compile-command'. This specifies the
1312 program that executes when you type \\[compile] or
1313 \\[verilog-auto-save-compile].
1315 By default `verilog-tool' uses a Makefile if one exists in the current
1316 directory. If not, it is set to the `verilog-linter', `verilog-coverage',
1317 `verilog-simulator', or `verilog-compiler' variables, as selected with the
1318 Verilog -> \"Choose Compilation Action\" menu.
1320 You should set `verilog-tool' or the other variables to the path and
1321 arguments for your Verilog simulator. For example:
1322 \"vcs -p123 -O\"
1323 or a string like:
1324 \"(cd /tmp; surecov %s)\".
1326 In the former case, the path to the current buffer is concat'ed to the
1327 value of `verilog-tool'; in the later, the path to the current buffer is
1328 substituted for the %s.
1330 Where __FILE__ appears in the string, the `buffer-file-name' of the
1331 current buffer, without the directory portion, will be substituted."
1332 (interactive)
1333 (cond
1334 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1335 (file-exists-p "Makefile"))
1336 (make-local-variable 'compile-command)
1337 (setq compile-command "make "))
1339 (make-local-variable 'compile-command)
1340 (setq compile-command
1341 (if verilog-tool
1342 (if (string-match "%s" (eval verilog-tool))
1343 (format (eval verilog-tool) (or buffer-file-name ""))
1344 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1345 ""))))
1346 (verilog-modify-compile-command))
1348 (defun verilog-modify-compile-command ()
1349 "Replace meta-information in `compile-command'.
1350 Where __FILE__ appears in the string, the current buffer's file-name,
1351 without the directory portion, will be substituted."
1352 (when (and
1353 (stringp compile-command)
1354 (string-match "\\b__FILE__\\b" compile-command))
1355 (make-local-variable 'compile-command)
1356 (setq compile-command
1357 (verilog-string-replace-matches
1358 "\\b__FILE__\\b" (file-name-nondirectory (buffer-file-name))
1359 t t compile-command))))
1361 ;; Following code only gets called from compilation-mode-hook.
1362 (defvar compilation-error-regexp-alist)
1364 (defun verilog-error-regexp-add ()
1365 "Add the messages to the `compilation-error-regexp-alist'.
1366 Called by `compilation-mode-hook'. This allows \\[next-error] to
1367 find the errors."
1368 (if (not verilog-error-regexp-add-didit)
1369 (progn
1370 (setq verilog-error-regexp-add-didit t)
1371 (setq-default compilation-error-regexp-alist
1372 (append verilog-error-regexp
1373 (default-value 'compilation-error-regexp-alist)))
1374 ;; Could be buffer local at this point; maybe also in let; change all three
1375 (setq compilation-error-regexp-alist
1376 (default-value 'compilation-error-regexp-alist))
1377 (set (make-local-variable 'compilation-error-regexp-alist)
1378 (default-value 'compilation-error-regexp-alist)))))
1380 (add-hook 'compilation-mode-hook 'verilog-error-regexp-add)
1382 (defconst verilog-directive-re
1383 ;; "`case" "`default" "`define" "`define" "`else" "`endfor" "`endif"
1384 ;; "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1385 ;; "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1386 ;; "`time_scale" "`undef" "`while"
1387 "\\<`\\(case\\|def\\(ault\\|ine\\(\\)?\\)\\|e\\(lse\\|nd\\(for\\|if\\|protect\\|switch\\|while\\)\\)\\|for\\(mat\\)?\\|i\\(f\\(def\\|ndef\\)?\\|nclude\\)\\|let\\|protect\\|switch\\|time\\(_scale\\|scale\\)\\|undef\\|while\\)\\>")
1389 (defconst verilog-directive-begin
1390 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1392 (defconst verilog-directive-middle
1393 "\\<`\\(else\\|default\\|case\\)\\>")
1395 (defconst verilog-directive-end
1396 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1398 (defconst verilog-directive-re-1
1399 (concat "[ \t]*" verilog-directive-re))
1402 ;; Regular expressions used to calculate indent, etc.
1404 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
1405 (defconst verilog-case-re "\\(\\<case[xz]?\\>\\|\\<randcase\\>\\)")
1406 ;; Want to match
1407 ;; aa :
1408 ;; aa,bb :
1409 ;; a[34:32] :
1410 ;; a,
1411 ;; b :
1413 (defconst verilog-no-indent-begin-re
1414 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\|always_comb\\|always_ff\\|always_latch\\)\\>")
1416 (defconst verilog-ends-re
1417 ;; Parenthesis indicate type of keyword found
1418 (concat
1419 "\\(\\<else\\>\\)\\|" ; 1
1420 "\\(\\<if\\>\\)\\|" ; 2
1421 "\\(\\<end\\>\\)\\|" ; 3
1422 "\\(\\<endcase\\>\\)\\|" ; 4
1423 "\\(\\<endfunction\\>\\)\\|" ; 5
1424 "\\(\\<endtask\\>\\)\\|" ; 6
1425 "\\(\\<endspecify\\>\\)\\|" ; 7
1426 "\\(\\<endtable\\>\\)\\|" ; 8
1427 "\\(\\<endgenerate\\>\\)\\|" ; 9
1428 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
1429 "\\(\\<endclass\\>\\)\\|" ; 11
1430 "\\(\\<endgroup\\>\\)" ; 12
1433 (defconst verilog-auto-end-comment-lines-re
1434 ;; Matches to names in this list cause auto-end-commentation
1435 (concat "\\("
1436 verilog-directive-re "\\)\\|\\("
1437 (eval-when-compile
1438 (verilog-regexp-words
1439 `( "begin"
1440 "else"
1441 "end"
1442 "endcase"
1443 "endclass"
1444 "endclocking"
1445 "endgroup"
1446 "endfunction"
1447 "endmodule"
1448 "endprogram"
1449 "endprimitive"
1450 "endinterface"
1451 "endpackage"
1452 "endsequence"
1453 "endspecify"
1454 "endtable"
1455 "endtask"
1456 "join"
1457 "join_any"
1458 "join_none"
1459 "module"
1460 "macromodule"
1461 "primitive"
1462 "interface"
1463 "package")))
1464 "\\)"))
1466 ;;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
1467 ;;; verilog-end-block-ordered-re matches exactly the same strings.
1468 (defconst verilog-end-block-ordered-re
1469 ;; Parenthesis indicate type of keyword found
1470 (concat "\\(\\<endcase\\>\\)\\|" ; 1
1471 "\\(\\<end\\>\\)\\|" ; 2
1472 "\\(\\<end" ; 3, but not used
1473 "\\(" ; 4, but not used
1474 "\\(function\\)\\|" ; 5
1475 "\\(task\\)\\|" ; 6
1476 "\\(module\\)\\|" ; 7
1477 "\\(primitive\\)\\|" ; 8
1478 "\\(interface\\)\\|" ; 9
1479 "\\(package\\)\\|" ; 10
1480 "\\(class\\)\\|" ; 11
1481 "\\(group\\)\\|" ; 12
1482 "\\(program\\)\\|" ; 13
1483 "\\(sequence\\)\\|" ; 14
1484 "\\(clocking\\)\\|" ; 15
1485 "\\)\\>\\)"))
1486 (defconst verilog-end-block-re
1487 (eval-when-compile
1488 (verilog-regexp-words
1490 `("end" ;; closes begin
1491 "endcase" ;; closes any of case, casex casez or randcase
1492 "join" "join_any" "join_none" ;; closes fork
1493 "endclass"
1494 "endtable"
1495 "endspecify"
1496 "endfunction"
1497 "endgenerate"
1498 "endtask"
1499 "endgroup"
1500 "endproperty"
1501 "endinterface"
1502 "endpackage"
1503 "endprogram"
1504 "endsequence"
1505 "endclocking"
1506 ))))
1509 (defconst verilog-endcomment-reason-re
1510 ;; Parenthesis indicate type of keyword found
1511 (concat
1512 "\\(\\<fork\\>\\)\\|"
1513 "\\(\\<begin\\>\\)\\|"
1514 "\\(\\<if\\>\\)\\|"
1515 "\\(\\<clocking\\>\\)\\|"
1516 "\\(\\<else\\>\\)\\|"
1517 "\\(\\<end\\>.*\\<else\\>\\)\\|"
1518 "\\(\\<task\\>\\)\\|"
1519 "\\(\\<function\\>\\)\\|"
1520 "\\(\\<initial\\>\\)\\|"
1521 "\\(\\<interface\\>\\)\\|"
1522 "\\(\\<package\\>\\)\\|"
1523 "\\(\\<final\\>\\)\\|"
1524 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
1525 "\\(\\<always_comb\\>\\(\[ \t\]*@\\)?\\)\\|"
1526 "\\(\\<always_ff\\>\\(\[ \t\]*@\\)?\\)\\|"
1527 "\\(\\<always_latch\\>\\(\[ \t\]*@\\)?\\)\\|"
1528 "\\(@\\)\\|"
1529 "\\(\\<while\\>\\)\\|"
1530 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
1531 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
1532 "#"))
1534 (defconst verilog-named-block-re "begin[ \t]*:")
1536 ;; These words begin a block which can occur inside a module which should be indented,
1537 ;; and closed with the respective word from the end-block list
1539 (defconst verilog-beg-block-re
1540 (eval-when-compile
1541 (verilog-regexp-words
1542 `("begin"
1543 "case" "casex" "casez" "randcase"
1544 "clocking"
1545 "generate"
1546 "fork"
1547 "function"
1548 "property"
1549 "specify"
1550 "table"
1551 "task"
1552 ))))
1553 ;; These are the same words, in a specific order in the regular
1554 ;; expression so that matching will work nicely for
1555 ;; verilog-forward-sexp and verilog-calc-indent
1557 (defconst verilog-beg-block-re-ordered
1558 ( concat "\\<"
1559 "\\(begin\\)" ;1
1560 "\\|\\(randcase\\|\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)" ; 2,3
1561 "\\|\\(\\(disable\\s-+\\)?fork\\)" ;4
1562 "\\|\\(class\\)" ;5
1563 "\\|\\(table\\)" ;6
1564 "\\|\\(specify\\)" ;7
1565 "\\|\\(function\\)" ;8
1566 "\\|\\(task\\)" ;9
1567 "\\|\\(generate\\)" ;10
1568 "\\|\\(covergroup\\)" ;11
1569 "\\|\\(property\\)" ;12
1570 "\\|\\(\\(rand\\)?sequence\\)" ;13
1571 "\\|\\(clocking\\)" ;14
1572 "\\>"))
1574 (defconst verilog-end-block-ordered-rry
1575 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
1576 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
1577 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
1578 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
1579 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
1580 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
1581 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
1582 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
1583 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
1584 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
1585 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
1586 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
1587 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
1590 (defconst verilog-nameable-item-re
1591 (eval-when-compile
1592 (verilog-regexp-words
1593 `("begin"
1594 "fork"
1595 "join" "join_any" "join_none"
1596 "end"
1597 "endcase"
1598 "endconfig"
1599 "endclass"
1600 "endclocking"
1601 "endfunction"
1602 "endgenerate"
1603 "endmodule"
1604 "endprimative"
1605 "endinterface"
1606 "endpackage"
1607 "endspecify"
1608 "endtable"
1609 "endtask" )
1612 (defconst verilog-declaration-opener
1613 (eval-when-compile
1614 (verilog-regexp-words
1615 `("module" "begin" "task" "function"))))
1617 (defconst verilog-declaration-prefix-re
1618 (eval-when-compile
1619 (verilog-regexp-words
1621 ;; port direction
1622 "inout" "input" "output" "ref"
1623 ;; changeableness
1624 "const" "static" "protected" "local"
1625 ;; parameters
1626 "localparam" "parameter" "var"
1627 ;; type creation
1628 "typedef"
1629 ))))
1630 (defconst verilog-declaration-core-re
1631 (eval-when-compile
1632 (verilog-regexp-words
1634 ;; port direction (by themselves)
1635 "inout" "input" "output"
1636 ;; integer_atom_type
1637 "byte" "shortint" "int" "longint" "integer" "time"
1638 ;; integer_vector_type
1639 "bit" "logic" "reg"
1640 ;; non_integer_type
1641 "shortreal" "real" "realtime"
1642 ;; net_type
1643 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
1644 ;; misc
1645 "string" "event" "chandle" "virtual" "enum" "genvar"
1646 "struct" "union"
1647 ;; builtin classes
1648 "mailbox" "semaphore"
1649 ))))
1650 (defconst verilog-declaration-re
1651 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
1652 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
1653 (defconst verilog-optional-signed-re "\\s-*\\(signed\\)?")
1654 (defconst verilog-optional-signed-range-re
1655 (concat
1656 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
1657 (defconst verilog-macroexp-re "`\\sw+")
1659 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
1660 (defconst verilog-declaration-re-2-no-macro
1661 (concat "\\s-*" verilog-declaration-re
1662 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
1663 "\\)?"))
1664 (defconst verilog-declaration-re-2-macro
1665 (concat "\\s-*" verilog-declaration-re
1666 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
1667 "\\|\\(" verilog-macroexp-re "\\)"
1668 "\\)?"))
1669 (defconst verilog-declaration-re-1-macro
1670 (concat "^" verilog-declaration-re-2-macro))
1672 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
1674 (defconst verilog-defun-re
1675 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
1676 (defconst verilog-end-defun-re
1677 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
1678 (defconst verilog-zero-indent-re
1679 (concat verilog-defun-re "\\|" verilog-end-defun-re))
1681 (defconst verilog-behavioral-block-beg-re
1682 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
1683 "function" "task"))))
1685 (defconst verilog-indent-re
1686 (eval-when-compile
1687 (verilog-regexp-words
1690 "always" "always_latch" "always_ff" "always_comb"
1691 "begin" "end"
1692 ; "unique" "priority"
1693 "case" "casex" "casez" "randcase" "endcase"
1694 "class" "endclass"
1695 "clocking" "endclocking"
1696 "config" "endconfig"
1697 "covergroup" "endgroup"
1698 "fork" "join" "join_any" "join_none"
1699 "function" "endfunction"
1700 "final"
1701 "generate" "endgenerate"
1702 "initial"
1703 "interface" "endinterface"
1704 "module" "macromodule" "endmodule"
1705 "package" "endpackage"
1706 "primitive" "endprimative"
1707 "program" "endprogram"
1708 "property" "endproperty"
1709 "sequence" "randsequence" "endsequence"
1710 "specify" "endspecify"
1711 "table" "endtable"
1712 "task" "endtask"
1713 "`case"
1714 "`default"
1715 "`define" "`undef"
1716 "`if" "`ifdef" "`ifndef" "`else" "`endif"
1717 "`while" "`endwhile"
1718 "`for" "`endfor"
1719 "`format"
1720 "`include"
1721 "`let"
1722 "`protect" "`endprotect"
1723 "`switch" "`endswitch"
1724 "`timescale"
1725 "`time_scale"
1726 ))))
1728 (defconst verilog-defun-level-re
1729 (eval-when-compile
1730 (verilog-regexp-words
1732 "module" "macromodule" "primitive" "class" "program" "initial" "final" "always" "always_comb"
1733 "always_ff" "always_latch" "endtask" "endfunction" "interface" "package"
1734 "config"))))
1736 (defconst verilog-defun-level-not-generate-re
1737 (eval-when-compile
1738 (verilog-regexp-words
1740 "module" "macromodule" "primitive" "class" "program" "interface" "package" "config"))))
1742 (defconst verilog-cpp-level-re
1743 (eval-when-compile
1744 (verilog-regexp-words
1746 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
1747 ))))
1748 (defconst verilog-disable-fork-re "disable\\s-+fork")
1749 (defconst verilog-extended-case-re "\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?")
1750 (defconst verilog-extended-complete-re
1751 (concat "\\(\\<extern\\s-+\\|\\<virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)"
1752 "\\|\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)"
1753 "\\|" verilog-extended-case-re ))
1754 (defconst verilog-basic-complete-re
1755 (eval-when-compile
1756 (verilog-regexp-words
1758 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
1759 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
1760 "if" "for" "forever" "foreach" "else" "parameter" "do"
1761 ))))
1762 (defconst verilog-complete-reg
1763 (concat
1764 verilog-extended-complete-re
1765 "\\|"
1766 verilog-basic-complete-re))
1768 (defconst verilog-end-statement-re
1769 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
1770 verilog-end-block-re "\\)"))
1772 (defconst verilog-endcase-re
1773 (concat verilog-case-re "\\|"
1774 "\\(endcase\\)\\|"
1775 verilog-defun-re
1778 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
1779 "String used to mark beginning of excluded text.")
1780 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
1781 "String used to mark end of excluded text.")
1782 (defconst verilog-preprocessor-re
1783 (eval-when-compile
1784 (verilog-regexp-words
1786 "`define" "`include" "`ifdef" "`ifndef" "`if" "`endif" "`else"
1787 ))))
1789 (defconst verilog-keywords
1790 '( "`case" "`default" "`define" "`else" "`endfor" "`endif"
1791 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1792 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1793 "`time_scale" "`undef" "`while"
1795 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
1796 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
1797 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
1798 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
1799 "config" "const" "constraint" "context" "continue" "cover"
1800 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
1801 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
1802 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
1803 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
1804 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
1805 "endtask" "enum" "event" "expect" "export" "extends" "extern"
1806 "final" "first_match" "for" "force" "foreach" "forever" "fork"
1807 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
1808 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
1809 "include" "initial" "inout" "input" "inside" "instance" "int"
1810 "integer" "interface" "intersect" "join" "join_any" "join_none"
1811 "large" "liblist" "library" "local" "localparam" "logic"
1812 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
1813 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
1814 "notif0" "notif1" "null" "or" "output" "package" "packed"
1815 "parameter" "pmos" "posedge" "primitive" "priority" "program"
1816 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
1817 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
1818 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
1819 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
1820 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
1821 "showcancelled" "signed" "small" "solve" "specify" "specparam"
1822 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
1823 "supply1" "table" "tagged" "task" "this" "throughout" "time"
1824 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
1825 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
1826 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
1827 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
1828 "wire" "with" "within" "wor" "xnor" "xor"
1830 "List of Verilog keywords.")
1832 (defconst verilog-comment-start-regexp "//\\|/\\*"
1833 "Dual comment value for `comment-start-regexp'.")
1835 (defvar verilog-mode-syntax-table
1836 (let ((table (make-syntax-table)))
1837 ;; Populate the syntax TABLE.
1838 (modify-syntax-entry ?\\ "\\" table)
1839 (modify-syntax-entry ?+ "." table)
1840 (modify-syntax-entry ?- "." table)
1841 (modify-syntax-entry ?= "." table)
1842 (modify-syntax-entry ?% "." table)
1843 (modify-syntax-entry ?< "." table)
1844 (modify-syntax-entry ?> "." table)
1845 (modify-syntax-entry ?& "." table)
1846 (modify-syntax-entry ?| "." table)
1847 (modify-syntax-entry ?` "w" table)
1848 (modify-syntax-entry ?_ "w" table)
1849 (modify-syntax-entry ?\' "." table)
1851 ;; Set up TABLE to handle block and line style comments.
1852 (if (featurep 'xemacs)
1853 (progn
1854 ;; XEmacs (formerly Lucid) has the best implementation
1855 (modify-syntax-entry ?/ ". 1456" table)
1856 (modify-syntax-entry ?* ". 23" table)
1857 (modify-syntax-entry ?\n "> b" table))
1858 ;; Emacs does things differently, but we can work with it
1859 (modify-syntax-entry ?/ ". 124b" table)
1860 (modify-syntax-entry ?* ". 23" table)
1861 (modify-syntax-entry ?\n "> b" table))
1862 table)
1863 "Syntax table used in Verilog mode buffers.")
1865 (defvar verilog-font-lock-keywords nil
1866 "Default highlighting for Verilog mode.")
1868 (defvar verilog-font-lock-keywords-1 nil
1869 "Subdued level highlighting for Verilog mode.")
1871 (defvar verilog-font-lock-keywords-2 nil
1872 "Medium level highlighting for Verilog mode.
1873 See also `verilog-font-lock-extra-types'.")
1875 (defvar verilog-font-lock-keywords-3 nil
1876 "Gaudy level highlighting for Verilog mode.
1877 See also `verilog-font-lock-extra-types'.")
1878 (defvar verilog-font-lock-translate-off-face
1879 'verilog-font-lock-translate-off-face
1880 "Font to use for translated off regions.")
1881 (defface verilog-font-lock-translate-off-face
1882 '((((class color)
1883 (background light))
1884 (:background "gray90" :italic t ))
1885 (((class color)
1886 (background dark))
1887 (:background "gray10" :italic t ))
1888 (((class grayscale) (background light))
1889 (:foreground "DimGray" :italic t))
1890 (((class grayscale) (background dark))
1891 (:foreground "LightGray" :italic t))
1892 (t (:italis t)))
1893 "Font lock mode face used to background highlight translate-off regions."
1894 :group 'font-lock-highlighting-faces)
1896 (defvar verilog-font-lock-p1800-face
1897 'verilog-font-lock-p1800-face
1898 "Font to use for p1800 keywords.")
1899 (defface verilog-font-lock-p1800-face
1900 '((((class color)
1901 (background light))
1902 (:foreground "DarkOrange3" :bold t ))
1903 (((class color)
1904 (background dark))
1905 (:foreground "orange1" :bold t ))
1906 (t (:italic t)))
1907 "Font lock mode face used to highlight P1800 keywords."
1908 :group 'font-lock-highlighting-faces)
1910 (defvar verilog-font-lock-ams-face
1911 'verilog-font-lock-ams-face
1912 "Font to use for Analog/Mixed Signal keywords.")
1913 (defface verilog-font-lock-ams-face
1914 '((((class color)
1915 (background light))
1916 (:foreground "Purple" :bold t ))
1917 (((class color)
1918 (background dark))
1919 (:foreground "orange1" :bold t ))
1920 (t (:italic t)))
1921 "Font lock mode face used to highlight AMS keywords."
1922 :group 'font-lock-highlighting-faces)
1924 (defvar verilog-font-grouping-keywords-face
1925 'verilog-font-lock-grouping-keywords-face
1926 "Font to use for Verilog Grouping Keywords (such as begin..end).")
1927 (defface verilog-font-lock-grouping-keywords-face
1928 '((((class color)
1929 (background light))
1930 (:foreground "red4" :bold t ))
1931 (((class color)
1932 (background dark))
1933 (:foreground "red4" :bold t ))
1934 (t (:italic t)))
1935 "Font lock mode face used to highlight verilog grouping keywords."
1936 :group 'font-lock-highlighting-faces)
1938 (let* ((verilog-type-font-keywords
1939 (eval-when-compile
1940 (verilog-regexp-opt
1942 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
1943 "event" "genvar" "inout" "input" "integer" "localparam"
1944 "logic" "mailbox" "nand" "nmos" "not" "notif0" "notif1" "or"
1945 "output" "parameter" "pmos" "pull0" "pull1" "pullup"
1946 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
1947 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
1948 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
1949 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
1950 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
1951 ) nil )))
1953 (verilog-pragma-keywords
1954 (eval-when-compile
1955 (verilog-regexp-opt
1956 '("surefire" "synopsys" "rtl_synthesis" "verilint" "leda" "0in") nil
1959 (verilog-p1800-keywords
1960 (eval-when-compile
1961 (verilog-regexp-opt
1962 '("alias" "assert" "assume" "automatic" "before" "bind"
1963 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
1964 "clocking" "config" "const" "constraint" "context" "continue"
1965 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
1966 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
1967 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
1968 "expect" "export" "extends" "extern" "first_match" "foreach"
1969 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
1970 "illegal_bins" "import" "incdir" "include" "inside" "instance"
1971 "int" "intersect" "large" "liblist" "library" "local" "longint"
1972 "matches" "medium" "modport" "new" "noshowcancelled" "null"
1973 "packed" "program" "property" "protected" "pull0" "pull1"
1974 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
1975 "randcase" "randsequence" "ref" "release" "return" "scalared"
1976 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
1977 "specparam" "static" "string" "strong0" "strong1" "struct"
1978 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
1979 "type" "union" "unsigned" "use" "var" "virtual" "void"
1980 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
1981 ) nil )))
1983 (verilog-ams-keywords
1984 (eval-when-compile
1985 (verilog-regexp-opt
1986 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
1987 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
1988 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
1989 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
1990 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
1991 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
1992 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
1993 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
1994 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
1995 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
1996 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
1998 (verilog-font-keywords
1999 (eval-when-compile
2000 (verilog-regexp-opt
2002 "assign" "case" "casex" "casez" "randcase" "deassign"
2003 "default" "disable" "else" "endcase" "endfunction"
2004 "endgenerate" "endinterface" "endmodule" "endprimitive"
2005 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
2006 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
2007 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
2008 "package" "endpackage" "always" "always_comb" "always_ff"
2009 "always_latch" "posedge" "primitive" "priority" "release"
2010 "repeat" "specify" "table" "task" "unique" "wait" "while"
2011 "class" "program" "endclass" "endprogram"
2012 ) nil )))
2014 (verilog-font-grouping-keywords
2015 (eval-when-compile
2016 (verilog-regexp-opt
2017 '( "begin" "end" ) nil ))))
2019 (setq verilog-font-lock-keywords
2020 (list
2021 ;; Fontify all builtin keywords
2022 (concat "\\<\\(" verilog-font-keywords "\\|"
2023 ;; And user/system tasks and functions
2024 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
2025 "\\)\\>")
2026 ;; Fontify all types
2027 (if verilog-highlight-grouping-keywords
2028 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2029 'verilog-font-lock-ams-face)
2030 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2031 'font-lock-type-face))
2032 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
2033 'font-lock-type-face)
2034 ;; Fontify IEEE-P1800 keywords appropriately
2035 (if verilog-highlight-p1800-keywords
2036 (cons (concat "\\<\\(" verilog-p1800-keywords "\\)\\>")
2037 'verilog-font-lock-p1800-face)
2038 (cons (concat "\\<\\(" verilog-p1800-keywords "\\)\\>")
2039 'font-lock-type-face))
2040 ;; Fontify Verilog-AMS keywords
2041 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
2042 'verilog-font-lock-ams-face)))
2044 (setq verilog-font-lock-keywords-1
2045 (append verilog-font-lock-keywords
2046 (list
2047 ;; Fontify module definitions
2048 (list
2049 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
2050 '(1 font-lock-keyword-face)
2051 '(3 font-lock-function-name-face 'prepend))
2052 ;; Fontify function definitions
2053 (list
2054 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
2055 '(1 font-lock-keyword-face)
2056 '(3 font-lock-reference-face prepend))
2057 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
2058 (1 font-lock-keyword-face)
2059 (2 font-lock-reference-face append))
2060 '("\\<function\\>\\s-+\\(\\sw+\\)"
2061 1 'font-lock-reference-face append))))
2063 (setq verilog-font-lock-keywords-2
2064 (append verilog-font-lock-keywords-1
2065 (list
2066 ;; Fontify pragmas
2067 (concat "\\(//\\s-*" verilog-pragma-keywords "\\s-.*\\)")
2068 ;; Fontify escaped names
2069 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
2070 ;; Fontify macro definitions/ uses
2071 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
2072 'font-lock-preprocessor-face
2073 'font-lock-type-face))
2074 ;; Fontify delays/numbers
2075 '("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9_.\]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
2076 0 font-lock-type-face append)
2077 ;; Fontify instantiation names
2078 '("\\([A-Za-z][A-Za-z0-9_]+\\)\\s-*(" 1 font-lock-function-name-face)
2081 (setq verilog-font-lock-keywords-3
2082 (append verilog-font-lock-keywords-2
2083 (when verilog-highlight-translate-off
2084 (list
2085 ;; Fontify things in translate off regions
2086 '(verilog-match-translate-off
2087 (0 'verilog-font-lock-translate-off-face prepend))
2088 )))))
2091 (defun verilog-inside-comment-p ()
2092 "Check if point inside a nested comment."
2093 (save-excursion
2094 (let ((st-point (point)) hitbeg)
2095 (or (search-backward "//" (verilog-get-beg-of-line) t)
2096 (if (progn
2097 ;; This is for tricky case //*, we keep searching if /*
2098 ;; is proceeded by // on same line.
2099 (while
2100 (and (setq hitbeg (search-backward "/*" nil t))
2101 (progn
2102 (forward-char 1)
2103 (search-backward "//" (verilog-get-beg-of-line) t))))
2104 hitbeg)
2105 (not (search-forward "*/" st-point t)))))))
2107 (defun verilog-declaration-end ()
2108 (search-forward ";"))
2110 (defun verilog-point-text (&optional pointnum)
2111 "Return text describing where POINTNUM or current point is (for errors).
2112 Use filename, if current buffer being edited shorten to just buffer name."
2113 (concat (or (and (equal (window-buffer (selected-window)) (current-buffer))
2114 (buffer-name))
2115 buffer-file-name
2116 (buffer-name))
2117 ":" (int-to-string (count-lines (point-min) (or pointnum (point))))))
2119 (defun electric-verilog-backward-sexp ()
2120 "Move backward over one balanced expression."
2121 (interactive)
2122 ;; before that see if we are in a comment
2123 (verilog-backward-sexp))
2125 (defun electric-verilog-forward-sexp ()
2126 "Move forward over one balanced expression."
2127 (interactive)
2128 ;; before that see if we are in a comment
2129 (verilog-forward-sexp))
2131 ;;;used by hs-minor-mode
2132 (defun verilog-forward-sexp-function (arg)
2133 (if (< arg 0)
2134 (verilog-backward-sexp)
2135 (verilog-forward-sexp)))
2138 (defun verilog-backward-sexp ()
2139 (let ((reg)
2140 (elsec 1)
2141 (found nil)
2142 (st (point)))
2143 (if (not (looking-at "\\<"))
2144 (forward-word -1))
2145 (cond
2146 ((verilog-skip-backward-comment-or-string))
2147 ((looking-at "\\<else\\>")
2148 (setq reg (concat
2149 verilog-end-block-re
2150 "\\|\\(\\<else\\>\\)"
2151 "\\|\\(\\<if\\>\\)"))
2152 (while (and (not found)
2153 (verilog-re-search-backward reg nil 'move))
2154 (cond
2155 ((match-end 1) ; matched verilog-end-block-re
2156 ; try to leap back to matching outward block by striding across
2157 ; indent level changing tokens then immediately
2158 ; previous line governs indentation.
2159 (verilog-leap-to-head))
2160 ((match-end 2) ; else, we're in deep
2161 (setq elsec (1+ elsec)))
2162 ((match-end 3) ; found it
2163 (setq elsec (1- elsec))
2164 (if (= 0 elsec)
2165 ;; Now previous line describes syntax
2166 (setq found 't))))))
2167 ((looking-at verilog-end-block-re)
2168 (verilog-leap-to-head))
2169 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
2170 (cond
2171 ((match-end 1)
2172 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
2173 ((match-end 2)
2174 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
2175 ((match-end 3)
2176 (verilog-re-search-backward "\\<class\\>" nil 'move))
2177 ((match-end 4)
2178 (verilog-re-search-backward "\\<program\\>" nil 'move))
2179 ((match-end 5)
2180 (verilog-re-search-backward "\\<interface\\>" nil 'move))
2181 ((match-end 6)
2182 (verilog-re-search-backward "\\<package\\>" nil 'move))
2184 (goto-char st)
2185 (backward-sexp 1))))
2187 (goto-char st)
2188 (backward-sexp)))))
2190 (defun verilog-forward-sexp ()
2191 (let ((reg)
2192 (md 2)
2193 (st (point)))
2194 (if (not (looking-at "\\<"))
2195 (forward-word -1))
2196 (cond
2197 ((verilog-skip-forward-comment-or-string)
2198 (verilog-forward-syntactic-ws))
2199 ((looking-at verilog-beg-block-re-ordered) ;; begin|(case)|xx|(fork)|class|table|specify|function|task|generate|covergroup|property|sequence|clocking
2200 (cond
2201 ((match-end 1) ; end
2202 ;; Search forward for matching begin
2203 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
2204 ((match-end 2) ; endcase
2205 ;; Search forward for matching case
2206 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
2207 (setq md 3) ;; ender is third item in regexp
2209 ((match-end 4) ; join
2210 ;; might be "disable fork"
2211 (if (or
2212 (looking-at verilog-disable-fork-re)
2213 (and (looking-at "fork")
2214 (progn
2215 (forward-word -1)
2216 (looking-at verilog-disable-fork-re))))
2217 (progn
2218 (goto-char (match-end 0))
2219 (forward-word)
2220 (setq reg nil))
2221 (progn
2222 ;; Search forward for matching fork
2223 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))))
2224 ((match-end 5) ; endclass
2225 ;; Search forward for matching class
2226 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
2227 ((match-end 6) ; endtable
2228 ;; Search forward for matching table
2229 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
2230 ((match-end 7) ; endspecify
2231 ;; Search forward for matching specify
2232 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
2233 ((match-end 8) ; endfunction
2234 ;; Search forward for matching function
2235 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
2236 ((match-end 9) ; endtask
2237 ;; Search forward for matching task
2238 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
2239 ((match-end 10) ; endgenerate
2240 ;; Search forward for matching generate
2241 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
2242 ((match-end 11) ; endgroup
2243 ;; Search forward for matching covergroup
2244 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
2245 ((match-end 12) ; endproperty
2246 ;; Search forward for matching property
2247 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
2248 ((match-end 13) ; endsequence
2249 ;; Search forward for matching sequence
2250 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
2251 (setq md 3)) ; 3 to get to endsequence in the reg above
2252 ((match-end 14) ; endclocking
2253 ;; Search forward for matching clocking
2254 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
2255 (if (and reg
2256 (forward-word 1))
2257 (catch 'skip
2258 (let ((nest 1))
2259 (while (verilog-re-search-forward reg nil 'move)
2260 (cond
2261 ((match-end md) ; the closer in reg, so we are climbing out
2262 (setq nest (1- nest))
2263 (if (= 0 nest) ; we are out!
2264 (throw 'skip 1)))
2265 ((match-end 1) ; the opener in reg, so we are deeper now
2266 (setq nest (1+ nest)))))))))
2267 ((looking-at (concat
2268 "\\(\\<\\(macro\\)?module\\>\\)\\|"
2269 "\\(\\<primitive\\>\\)\\|"
2270 "\\(\\<class\\>\\)\\|"
2271 "\\(\\<program\\>\\)\\|"
2272 "\\(\\<interface\\>\\)\\|"
2273 "\\(\\<package\\>\\)"))
2274 (cond
2275 ((match-end 1)
2276 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
2277 ((match-end 2)
2278 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
2279 ((match-end 3)
2280 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
2281 ((match-end 4)
2282 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
2283 ((match-end 5)
2284 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
2285 ((match-end 6)
2286 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
2288 (goto-char st)
2289 (if (= (following-char) ?\) )
2290 (forward-char 1)
2291 (forward-sexp 1)))))
2293 (goto-char st)
2294 (if (= (following-char) ?\) )
2295 (forward-char 1)
2296 (forward-sexp 1))))))
2298 (defun verilog-declaration-beg ()
2299 (verilog-re-search-backward verilog-declaration-re (bobp) t))
2301 (defun verilog-font-lock-init ()
2302 "Initialize fontification."
2303 ;; highlight keywords and standardized types, attributes, enumeration
2304 ;; values, and subprograms
2305 (setq verilog-font-lock-keywords-3
2306 (append verilog-font-lock-keywords-2
2307 (when verilog-highlight-translate-off
2308 (list
2309 ;; Fontify things in translate off regions
2310 '(verilog-match-translate-off
2311 (0 'verilog-font-lock-translate-off-face prepend))))))
2312 (put 'verilog-mode 'font-lock-defaults
2313 '((verilog-font-lock-keywords
2314 verilog-font-lock-keywords-1
2315 verilog-font-lock-keywords-2
2316 verilog-font-lock-keywords-3)
2317 nil ; nil means highlight strings & comments as well as keywords
2318 nil ; nil means keywords must match case
2319 nil ; syntax table handled elsewhere
2320 ;; Function to move to beginning of reasonable region to highlight
2321 verilog-beg-of-defun)))
2323 ;; initialize fontification for Verilog Mode
2324 (verilog-font-lock-init)
2328 ;; Mode
2330 (defvar verilog-which-tool 1)
2331 ;;;###autoload
2332 (defun verilog-mode ()
2333 "Major mode for editing Verilog code.
2334 \\<verilog-mode-map>
2335 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
2336 AUTOs can improve coding efficiency.
2338 Use \\[verilog-faq] for a pointer to frequently asked questions.
2340 NEWLINE, TAB indents for Verilog code.
2341 Delete converts tabs to spaces as it moves back.
2343 Supports highlighting.
2345 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
2346 with no args, if that value is non-nil.
2348 Variables controlling indentation/edit style:
2350 variable `verilog-indent-level' (default 3)
2351 Indentation of Verilog statements with respect to containing block.
2352 `verilog-indent-level-module' (default 3)
2353 Absolute indentation of Module level Verilog statements.
2354 Set to 0 to get initial and always statements lined up
2355 on the left side of your screen.
2356 `verilog-indent-level-declaration' (default 3)
2357 Indentation of declarations with respect to containing block.
2358 Set to 0 to get them list right under containing block.
2359 `verilog-indent-level-behavioral' (default 3)
2360 Indentation of first begin in a task or function block
2361 Set to 0 to get such code to lined up underneath the task or
2362 function keyword.
2363 `verilog-indent-level-directive' (default 1)
2364 Indentation of `ifdef/`endif blocks.
2365 `verilog-cexp-indent' (default 1)
2366 Indentation of Verilog statements broken across lines i.e.:
2367 if (a)
2368 begin
2369 `verilog-case-indent' (default 2)
2370 Indentation for case statements.
2371 `verilog-auto-newline' (default nil)
2372 Non-nil means automatically newline after semicolons and the punctuation
2373 mark after an end.
2374 `verilog-auto-indent-on-newline' (default t)
2375 Non-nil means automatically indent line after newline.
2376 `verilog-tab-always-indent' (default t)
2377 Non-nil means TAB in Verilog mode should always reindent the current line,
2378 regardless of where in the line point is when the TAB command is used.
2379 `verilog-indent-begin-after-if' (default t)
2380 Non-nil means to indent begin statements following a preceding
2381 if, else, while, for and repeat statements, if any. Otherwise,
2382 the begin is lined up with the preceding token. If t, you get:
2383 if (a)
2384 begin // amount of indent based on `verilog-cexp-indent'
2385 otherwise you get:
2386 if (a)
2387 begin
2388 `verilog-auto-endcomments' (default t)
2389 Non-nil means a comment /* ... */ is set after the ends which ends
2390 cases, tasks, functions and modules.
2391 The type and name of the object will be set between the braces.
2392 `verilog-minimum-comment-distance' (default 10)
2393 Minimum distance (in lines) between begin and end required before a comment
2394 will be inserted. Setting this variable to zero results in every
2395 end acquiring a comment; the default avoids too many redundant
2396 comments in tight quarters.
2397 `verilog-auto-lineup' (default `(all))
2398 List of contexts where auto lineup of code should be done.
2400 Variables controlling other actions:
2402 `verilog-linter' (default surelint)
2403 Unix program to call to run the lint checker. This is the default
2404 command for \\[compile-command] and \\[verilog-auto-save-compile].
2406 See \\[customize] for the complete list of variables.
2408 AUTO expansion functions are, in part:
2410 \\[verilog-auto] Expand AUTO statements.
2411 \\[verilog-delete-auto] Remove the AUTOs.
2412 \\[verilog-inject-auto] Insert AUTOs for the first time.
2414 Some other functions are:
2416 \\[verilog-complete-word] Complete word with appropriate possibilities.
2417 \\[verilog-mark-defun] Mark function.
2418 \\[verilog-beg-of-defun] Move to beginning of current function.
2419 \\[verilog-end-of-defun] Move to end of current function.
2420 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
2422 \\[verilog-comment-region] Put marked area in a comment.
2423 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
2424 \\[verilog-insert-block] Insert begin ... end.
2425 \\[verilog-star-comment] Insert /* ... */.
2427 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
2428 \\[verilog-sk-begin] Insert a begin .. end block.
2429 \\[verilog-sk-case] Insert a case block, prompting for details.
2430 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
2431 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
2432 \\[verilog-sk-header] Insert a header block at the top of file.
2433 \\[verilog-sk-initial] Insert an initial begin .. end block.
2434 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
2435 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
2436 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
2437 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
2438 \\[verilog-sk-specify] Insert a specify .. endspecify block.
2439 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
2440 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
2441 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
2442 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
2443 \\[verilog-sk-if] Insert an if (..) begin .. end block.
2444 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
2445 \\[verilog-sk-comment] Insert a comment block.
2446 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
2447 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
2448 \\[verilog-sk-input] Insert an input declaration, prompting for details.
2449 \\[verilog-sk-output] Insert an output declaration, prompting for details.
2450 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
2451 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
2452 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
2453 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
2454 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
2456 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
2457 Key bindings specific to `verilog-mode-map' are:
2459 \\{verilog-mode-map}"
2460 (interactive)
2461 (kill-all-local-variables)
2462 (use-local-map verilog-mode-map)
2463 (setq major-mode 'verilog-mode)
2464 (setq mode-name "Verilog")
2465 (setq local-abbrev-table verilog-mode-abbrev-table)
2466 (set (make-local-variable 'beginning-of-defun-function)
2467 'verilog-beg-of-defun)
2468 (set (make-local-variable 'end-of-defun-function)
2469 'verilog-end-of-defun)
2470 (set-syntax-table verilog-mode-syntax-table)
2471 (make-local-variable 'indent-line-function)
2472 (setq indent-line-function 'verilog-indent-line-relative)
2473 (setq comment-indent-function 'verilog-comment-indent)
2474 (make-local-variable 'parse-sexp-ignore-comments)
2475 (setq parse-sexp-ignore-comments nil)
2476 (make-local-variable 'comment-start)
2477 (make-local-variable 'comment-end)
2478 (make-local-variable 'comment-multi-line)
2479 (make-local-variable 'comment-start-skip)
2480 (setq comment-start "// "
2481 comment-end ""
2482 comment-start-skip "/\\*+ *\\|// *"
2483 comment-multi-line nil)
2484 ;; Set up for compilation
2485 (setq verilog-which-tool 1)
2486 (setq verilog-tool 'verilog-linter)
2487 (verilog-set-compile-command)
2488 (when (boundp 'hack-local-variables-hook) ;; Also modify any file-local-variables
2489 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
2491 ;; Setting up menus
2492 (when (featurep 'xemacs)
2493 (easy-menu-add verilog-stmt-menu)
2494 (easy-menu-add verilog-menu)
2495 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
2497 ;; Stuff for GNU Emacs
2498 (set (make-local-variable 'font-lock-defaults)
2499 '((verilog-font-lock-keywords verilog-font-lock-keywords-1
2500 verilog-font-lock-keywords-2
2501 verilog-font-lock-keywords-3)
2502 nil nil nil verilog-beg-of-defun))
2503 ;;------------------------------------------------------------
2504 ;; now hook in 'verilog-colorize-include-files (eldo-mode.el&spice-mode.el)
2505 ;; all buffer local:
2506 (when (featurep 'xemacs)
2507 (make-local-hook 'font-lock-mode-hook)
2508 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
2509 (make-local-hook 'after-change-functions))
2510 (add-hook 'font-lock-mode-hook 'verilog-colorize-include-files-buffer t t)
2511 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-colorize-include-files-buffer t t) ; not in Emacs
2512 (add-hook 'after-change-functions 'verilog-colorize-include-files t t)
2514 ;; Tell imenu how to handle Verilog.
2515 (make-local-variable 'imenu-generic-expression)
2516 (setq imenu-generic-expression verilog-imenu-generic-expression)
2517 ;; Tell which-func-modes that imenu knows about verilog
2518 (when (boundp 'which-function-modes)
2519 (add-to-list 'which-func-modes 'verilog-mode))
2520 ;; hideshow support
2521 (when (boundp 'hs-special-modes-alist)
2522 (unless (assq 'verilog-mode hs-special-modes-alist)
2523 (setq hs-special-modes-alist
2524 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
2525 verilog-forward-sexp-function)
2526 hs-special-modes-alist))))
2528 ;; Stuff for autos
2529 (add-hook 'write-contents-hooks 'verilog-auto-save-check) ; already local
2530 ;; (verilog-auto-reeval-locals t) ; Save locals in case user changes them
2531 ;; (verilog-getopt-flags)
2532 (run-hooks 'verilog-mode-hook))
2536 ;; Electric functions
2538 (defun electric-verilog-terminate-line (&optional arg)
2539 "Terminate line and indent next line.
2540 With optional ARG, remove existing end of line comments."
2541 (interactive)
2542 ;; before that see if we are in a comment
2543 (let ((state (save-excursion (verilog-syntax-ppss))))
2544 (cond
2545 ((nth 7 state) ; Inside // comment
2546 (if (eolp)
2547 (progn
2548 (delete-horizontal-space)
2549 (newline))
2550 (progn
2551 (newline)
2552 (insert "// ")
2553 (beginning-of-line)))
2554 (verilog-indent-line))
2555 ((nth 4 state) ; Inside any comment (hence /**/)
2556 (newline)
2557 (verilog-more-comment))
2558 ((eolp)
2559 ;; First, check if current line should be indented
2560 (if (save-excursion
2561 (delete-horizontal-space)
2562 (beginning-of-line)
2563 (skip-chars-forward " \t")
2564 (if (looking-at verilog-auto-end-comment-lines-re)
2565 (let ((indent-str (verilog-indent-line)))
2566 ;; Maybe we should set some endcomments
2567 (if verilog-auto-endcomments
2568 (verilog-set-auto-endcomments indent-str arg))
2569 (end-of-line)
2570 (delete-horizontal-space)
2571 (if arg
2573 (newline))
2574 nil)
2575 (progn
2576 (end-of-line)
2577 (delete-horizontal-space)
2578 't)))
2579 ;; see if we should line up assignments
2580 (progn
2581 (if (or (memq 'all verilog-auto-lineup)
2582 (memq 'assignments verilog-auto-lineup))
2583 (verilog-pretty-expr))
2584 (newline))
2585 (forward-line 1))
2586 ;; Indent next line
2587 (if verilog-auto-indent-on-newline
2588 (verilog-indent-line)))
2590 (newline)))))
2592 (defun electric-verilog-terminate-and-indent ()
2593 "Insert a newline and indent for the next statement."
2594 (interactive)
2595 (electric-verilog-terminate-line 1))
2597 (defun electric-verilog-semi ()
2598 "Insert `;' character and reindent the line."
2599 (interactive)
2600 (insert last-command-char)
2602 (if (or (verilog-in-comment-or-string-p)
2603 (verilog-in-escaped-name-p))
2605 (save-excursion
2606 (beginning-of-line)
2607 (verilog-forward-ws&directives)
2608 (verilog-indent-line))
2609 (if (and verilog-auto-newline
2610 (not (verilog-parenthesis-depth)))
2611 (electric-verilog-terminate-line))))
2613 (defun electric-verilog-semi-with-comment ()
2614 "Insert `;' character, reindent the line and indent for comment."
2615 (interactive)
2616 (insert "\;")
2617 (save-excursion
2618 (beginning-of-line)
2619 (verilog-indent-line))
2620 (indent-for-comment))
2622 (defun electric-verilog-colon ()
2623 "Insert `:' and do all indentations except line indent on this line."
2624 (interactive)
2625 (insert last-command-char)
2626 ;; Do nothing if within string.
2627 (if (or
2628 (verilog-within-string)
2629 (not (verilog-in-case-region-p)))
2631 (save-excursion
2632 (let ((p (point))
2633 (lim (progn (verilog-beg-of-statement) (point))))
2634 (goto-char p)
2635 (verilog-backward-case-item lim)
2636 (verilog-indent-line)))
2637 ;; (let ((verilog-tab-always-indent nil))
2638 ;; (verilog-indent-line))
2641 ;;(defun electric-verilog-equal ()
2642 ;; "Insert `=', and do indentation if within block."
2643 ;; (interactive)
2644 ;; (insert last-command-char)
2645 ;; Could auto line up expressions, but not yet
2646 ;; (if (eq (car (verilog-calculate-indent)) 'block)
2647 ;; (let ((verilog-tab-always-indent nil))
2648 ;; (verilog-indent-command)))
2649 ;; )
2651 (defun electric-verilog-tick ()
2652 "Insert back-tick, and indent to column 0 if this is a CPP directive."
2653 (interactive)
2654 (insert last-command-char)
2655 (save-excursion
2656 (if (progn
2657 (beginning-of-line)
2658 (looking-at verilog-directive-re-1))
2659 (verilog-indent-line))))
2661 (defun electric-verilog-tab ()
2662 "Function called when TAB is pressed in Verilog mode."
2663 (interactive)
2664 ;; If verilog-tab-always-indent, indent the beginning of the line.
2665 (if (or verilog-tab-always-indent
2666 (save-excursion
2667 (skip-chars-backward " \t")
2668 (bolp)))
2669 (let* ((oldpnt (point))
2670 (boi-point
2671 (save-excursion
2672 (beginning-of-line)
2673 (skip-chars-forward " \t")
2674 (verilog-indent-line)
2675 (back-to-indentation)
2676 (point))))
2677 (if (< (point) boi-point)
2678 (back-to-indentation)
2679 (cond ((not verilog-tab-to-comment))
2680 ((not (eolp))
2681 (end-of-line))
2683 (indent-for-comment)
2684 (when (and (eolp) (= oldpnt (point)))
2685 ; kill existing comment
2686 (beginning-of-line)
2687 (re-search-forward comment-start-skip oldpnt 'move)
2688 (goto-char (match-beginning 0))
2689 (skip-chars-backward " \t")
2690 (kill-region (point) oldpnt))))))
2691 (progn (insert "\t"))))
2696 ;; Interactive functions
2699 (defun verilog-indent-buffer ()
2700 "Indent-region the entire buffer as Verilog code.
2701 To call this from the command line, see \\[verilog-batch-indent]."
2702 (interactive)
2703 (verilog-mode)
2704 (indent-region (point-min) (point-max) nil))
2706 (defun verilog-insert-block ()
2707 "Insert Verilog begin ... end; block in the code with right indentation."
2708 (interactive)
2709 (verilog-indent-line)
2710 (insert "begin")
2711 (electric-verilog-terminate-line)
2712 (save-excursion
2713 (electric-verilog-terminate-line)
2714 (insert "end")
2715 (beginning-of-line)
2716 (verilog-indent-line)))
2718 (defun verilog-star-comment ()
2719 "Insert Verilog star comment at point."
2720 (interactive)
2721 (verilog-indent-line)
2722 (insert "/*")
2723 (save-excursion
2724 (newline)
2725 (insert " */"))
2726 (newline)
2727 (insert " * "))
2729 (defun verilog-insert-1 (fmt max)
2730 "Use format string FMT to insert integers 0 to MAX - 1.
2731 Inserts one integer per line, at the current column. Stops early
2732 if it reaches the end of the buffer."
2733 (let ((col (current-column))
2734 (n 0))
2735 (save-excursion
2736 (while (< n max)
2737 (insert (format fmt n))
2738 (forward-line 1)
2739 ;; Note that this function does not bother to check for lines
2740 ;; shorter than col.
2741 (if (eobp)
2742 (setq n max)
2743 (setq n (1+ n))
2744 (move-to-column col))))))
2746 (defun verilog-insert-indices (max)
2747 "Insert a set of indices into a rectangle.
2748 The upper left corner is defined by point. Indices begin with 0
2749 and extend to the MAX - 1. If no prefix arg is given, the user
2750 is prompted for a value. The indices are surrounded by square
2751 brackets \[]. For example, the following code with the point
2752 located after the first 'a' gives:
2754 a = b a[ 0] = b
2755 a = b a[ 1] = b
2756 a = b a[ 2] = b
2757 a = b a[ 3] = b
2758 a = b ==> insert-indices ==> a[ 4] = b
2759 a = b a[ 5] = b
2760 a = b a[ 6] = b
2761 a = b a[ 7] = b
2762 a = b a[ 8] = b"
2764 (interactive "NMAX: ")
2765 (verilog-insert-1 "[%3d]" max))
2767 (defun verilog-generate-numbers (max)
2768 "Insert a set of generated numbers into a rectangle.
2769 The upper left corner is defined by point. The numbers are padded to three
2770 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
2771 is supplied, then the user is prompted for the MAX number. Consider the
2772 following code fragment:
2774 buf buf buf buf000
2775 buf buf buf buf001
2776 buf buf buf buf002
2777 buf buf buf buf003
2778 buf buf ==> generate-numbers ==> buf buf004
2779 buf buf buf buf005
2780 buf buf buf buf006
2781 buf buf buf buf007
2782 buf buf buf buf008"
2784 (interactive "NMAX: ")
2785 (verilog-insert-1 "%3.3d" max))
2787 (defun verilog-mark-defun ()
2788 "Mark the current Verilog function (or procedure).
2789 This puts the mark at the end, and point at the beginning."
2790 (interactive)
2791 (if (featurep 'xemacs)
2792 (progn
2793 (push-mark (point))
2794 (verilog-end-of-defun)
2795 (push-mark (point))
2796 (verilog-beg-of-defun)
2797 (if (fboundp 'zmacs-activate-region)
2798 (zmacs-activate-region)))
2799 (mark-defun)))
2801 (defun verilog-comment-region (start end)
2802 ; checkdoc-params: (start end)
2803 "Put the region into a Verilog comment.
2804 The comments that are in this area are \"deformed\":
2805 `*)' becomes `!(*' and `}' becomes `!{'.
2806 These deformed comments are returned to normal if you use
2807 \\[verilog-uncomment-region] to undo the commenting.
2809 The commented area starts with `verilog-exclude-str-start', and ends with
2810 `verilog-exclude-str-end'. But if you change these variables,
2811 \\[verilog-uncomment-region] won't recognize the comments."
2812 (interactive "r")
2813 (save-excursion
2814 ;; Insert start and endcomments
2815 (goto-char end)
2816 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
2817 (not (save-excursion (skip-chars-backward " \t") (bolp))))
2818 (forward-line 1)
2819 (beginning-of-line))
2820 (insert verilog-exclude-str-end)
2821 (setq end (point))
2822 (newline)
2823 (goto-char start)
2824 (beginning-of-line)
2825 (insert verilog-exclude-str-start)
2826 (newline)
2827 ;; Replace end-comments within commented area
2828 (goto-char end)
2829 (save-excursion
2830 (while (re-search-backward "\\*/" start t)
2831 (replace-match "*-/" t t)))
2832 (save-excursion
2833 (let ((s+1 (1+ start)))
2834 (while (re-search-backward "/\\*" s+1 t)
2835 (replace-match "/-*" t t))))))
2837 (defun verilog-uncomment-region ()
2838 "Uncomment a commented area; change deformed comments back to normal.
2839 This command does nothing if the pointer is not in a commented
2840 area. See also `verilog-comment-region'."
2841 (interactive)
2842 (save-excursion
2843 (let ((start (point))
2844 (end (point)))
2845 ;; Find the boundaries of the comment
2846 (save-excursion
2847 (setq start (progn (search-backward verilog-exclude-str-start nil t)
2848 (point)))
2849 (setq end (progn (search-forward verilog-exclude-str-end nil t)
2850 (point))))
2851 ;; Check if we're really inside a comment
2852 (if (or (equal start (point)) (<= end (point)))
2853 (message "Not standing within commented area.")
2854 (progn
2855 ;; Remove endcomment
2856 (goto-char end)
2857 (beginning-of-line)
2858 (let ((pos (point)))
2859 (end-of-line)
2860 (delete-region pos (1+ (point))))
2861 ;; Change comments back to normal
2862 (save-excursion
2863 (while (re-search-backward "\\*-/" start t)
2864 (replace-match "*/" t t)))
2865 (save-excursion
2866 (while (re-search-backward "/-\\*" start t)
2867 (replace-match "/*" t t)))
2868 ;; Remove start comment
2869 (goto-char start)
2870 (beginning-of-line)
2871 (let ((pos (point)))
2872 (end-of-line)
2873 (delete-region pos (1+ (point)))))))))
2875 (defun verilog-beg-of-defun ()
2876 "Move backward to the beginning of the current function or procedure."
2877 (interactive)
2878 (verilog-re-search-backward verilog-defun-re nil 'move))
2880 (defun verilog-end-of-defun ()
2881 "Move forward to the end of the current function or procedure."
2882 (interactive)
2883 (verilog-re-search-forward verilog-end-defun-re nil 'move))
2885 (defun verilog-get-beg-of-defun (&optional warn)
2886 (save-excursion
2887 (cond ((verilog-re-search-forward-quick verilog-defun-re nil t)
2888 (point))
2890 (error "%s: Can't find module beginning" (verilog-point-text))
2891 (point-max)))))
2892 (defun verilog-get-end-of-defun (&optional warn)
2893 (save-excursion
2894 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
2895 (point))
2897 (error "%s: Can't find endmodule" (verilog-point-text))
2898 (point-max)))))
2900 (defun verilog-label-be (&optional arg)
2901 "Label matching begin ... end, fork ... join and case ... endcase statements.
2902 With ARG, first kill any existing labels."
2903 (interactive)
2904 (let ((cnt 0)
2905 (oldpos (point))
2906 (b (progn
2907 (verilog-beg-of-defun)
2908 (point-marker)))
2909 (e (progn
2910 (verilog-end-of-defun)
2911 (point-marker))))
2912 (goto-char (marker-position b))
2913 (if (> (- e b) 200)
2914 (message "Relabeling module..."))
2915 (while (and
2916 (> (marker-position e) (point))
2917 (verilog-re-search-forward
2918 (concat
2919 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|\\(primitive\\)\\|\\(interface\\)\\|\\(package\\)\\|\\(case\\)\\)?\\>"
2920 "\\|\\(`endif\\)\\|\\(`else\\)")
2921 nil 'move))
2922 (goto-char (match-beginning 0))
2923 (let ((indent-str (verilog-indent-line)))
2924 (verilog-set-auto-endcomments indent-str 't)
2925 (end-of-line)
2926 (delete-horizontal-space))
2927 (setq cnt (1+ cnt))
2928 (if (= 9 (% cnt 10))
2929 (message "%d..." cnt)))
2930 (goto-char oldpos)
2931 (if (or
2932 (> (- e b) 200)
2933 (> cnt 20))
2934 (message "%d lines auto commented" cnt))))
2936 (defun verilog-beg-of-statement ()
2937 "Move backward to beginning of statement."
2938 (interactive)
2939 ;; Move back token by token until we see the end
2940 ;; of some ealier line.
2941 (while
2942 ;; If the current point does not begin a new
2943 ;; statement, as in the character ahead of us is a ';', or SOF
2944 ;; or the string after us unambiguosly starts a statement,
2945 ;; or the token before us unambiguously ends a statement,
2946 ;; then move back a token and test again.
2947 (not (or
2948 (bolp)
2949 (= (preceding-char) ?\;)
2950 (not (or
2951 (looking-at "\\<")
2952 (forward-word -1)))
2953 (and
2954 (looking-at verilog-extended-complete-re)
2955 (not (save-excursion
2956 (verilog-backward-token)
2957 (looking-at verilog-extended-complete-re))))
2958 (looking-at verilog-basic-complete-re)
2959 (save-excursion
2960 (verilog-backward-token)
2962 (looking-at verilog-end-block-re)
2963 (looking-at verilog-preprocessor-re)))))
2964 (verilog-backward-syntactic-ws)
2965 (verilog-backward-token))
2966 ;; Now point is where the previous line ended.
2967 (verilog-forward-syntactic-ws))
2969 (defun verilog-beg-of-statement-1 ()
2970 "Move backward to beginning of statement."
2971 (interactive)
2972 (let ((pt (point)))
2974 (while (and (not (looking-at verilog-complete-reg))
2975 (setq pt (point))
2976 (verilog-backward-token)
2977 (not (looking-at verilog-complete-reg))
2978 (verilog-backward-syntactic-ws)
2979 (setq pt (point))
2980 (not (bolp))
2981 (not (= (preceding-char) ?\;))))
2982 (goto-char pt)
2983 (verilog-forward-ws&directives)))
2985 (defun verilog-end-of-statement ()
2986 "Move forward to end of current statement."
2987 (interactive)
2988 (let ((nest 0) pos)
2989 (or (looking-at verilog-beg-block-re)
2990 ;; Skip to end of statement
2991 (setq pos (catch 'found
2992 (while t
2993 (forward-sexp 1)
2994 (verilog-skip-forward-comment-or-string)
2995 (cond ((looking-at "[ \t]*;")
2996 (skip-chars-forward "^;")
2997 (forward-char 1)
2998 (throw 'found (point)))
2999 ((save-excursion
3000 (forward-sexp -1)
3001 (looking-at verilog-beg-block-re))
3002 (goto-char (match-beginning 0))
3003 (throw 'found nil))
3004 ((looking-at "[ \t]*)")
3005 (throw 'found (point)))
3006 ((eobp)
3007 (throw 'found (point))))))))
3008 (if (not pos)
3009 ;; Skip a whole block
3010 (catch 'found
3011 (while t
3012 (verilog-re-search-forward verilog-end-statement-re nil 'move)
3013 (setq nest (if (match-end 1)
3014 (1+ nest)
3015 (1- nest)))
3016 (cond ((eobp)
3017 (throw 'found (point)))
3018 ((= 0 nest)
3019 (throw 'found (verilog-end-of-statement))))))
3020 pos)))
3022 (defun verilog-in-case-region-p ()
3023 "Return true if in a case region.
3024 More specifically, point @ in the line foo : @ begin"
3025 (interactive)
3026 (save-excursion
3027 (if (and
3028 (progn (verilog-forward-syntactic-ws)
3029 (looking-at "\\<begin\\>"))
3030 (progn (verilog-backward-syntactic-ws)
3031 (= (preceding-char) ?\:)))
3032 (catch 'found
3033 (let ((nest 1))
3034 (while t
3035 (verilog-re-search-backward
3036 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
3037 "\\(\\<endcase\\>\\)\\>")
3038 nil 'move)
3039 (cond
3040 ((match-end 3)
3041 (setq nest (1+ nest)))
3042 ((match-end 2)
3043 (if (= nest 1)
3044 (throw 'found 1))
3045 (setq nest (1- nest)))
3047 (throw 'found (= nest 0)))))))
3048 nil)))
3049 (defun verilog-in-struct-region-p ()
3050 "Return true if in a struct region.
3051 More specifically, in a list after a struct|union keyword."
3052 (interactive)
3053 (save-excursion
3054 (let* ((state (verilog-syntax-ppss))
3055 (depth (nth 0 state)))
3056 (if depth
3057 (progn (backward-up-list depth)
3058 (verilog-beg-of-statement)
3059 (looking-at "\\<typedef\\>?\\s-*\\<struct\\|union\\>"))))))
3061 (defun verilog-in-generate-region-p ()
3062 "Return true if in a generate region.
3063 More specifically, after a generate and before an endgenerate."
3064 (interactive)
3065 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
3066 (nest 1))
3067 (save-excursion
3068 (while (and
3069 (/= nest 0)
3070 (verilog-re-search-backward "\\<\\(generate\\)\\|\\(endgenerate\\)\\>" lim 'move)
3071 (cond
3072 ((match-end 1) ; generate
3073 (setq nest (1- nest)))
3074 ((match-end 2) ; endgenerate
3075 (setq nest (1+ nest)))))))
3076 (= nest 0) )) ; return nest
3078 (defun verilog-in-fork-region-p ()
3079 "Return true if between a fork and join."
3080 (interactive)
3081 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
3082 (nest 1))
3083 (save-excursion
3084 (while (and
3085 (/= nest 0)
3086 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
3087 (cond
3088 ((match-end 1) ; fork
3089 (setq nest (1- nest)))
3090 ((match-end 2) ; join
3091 (setq nest (1+ nest)))))))
3092 (= nest 0) )) ; return nest
3094 (defun verilog-backward-case-item (lim)
3095 "Skip backward to nearest enclosing case item.
3096 Limit search to point LIM."
3097 (interactive)
3098 (let ((str 'nil)
3099 (lim1
3100 (progn
3101 (save-excursion
3102 (verilog-re-search-backward verilog-endcomment-reason-re
3103 lim 'move)
3104 (point)))))
3105 ;; Try to find the real :
3106 (if (save-excursion (search-backward ":" lim1 t))
3107 (let ((colon 0)
3108 b e )
3109 (while
3110 (and
3111 (< colon 1)
3112 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
3113 lim1 'move))
3114 (cond
3115 ((match-end 1) ;; [
3116 (setq colon (1+ colon))
3117 (if (>= colon 0)
3118 (error "%s: unbalanced [" (verilog-point-text))))
3119 ((match-end 2) ;; ]
3120 (setq colon (1- colon)))
3122 ((match-end 3) ;; :
3123 (setq colon (1+ colon)))))
3124 ;; Skip back to beginning of case item
3125 (skip-chars-backward "\t ")
3126 (verilog-skip-backward-comment-or-string)
3127 (setq e (point))
3128 (setq b
3129 (progn
3131 (verilog-re-search-backward
3132 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
3133 (progn
3134 (cond
3135 ((match-end 1)
3136 (goto-char (match-end 1))
3137 (verilog-forward-ws&directives)
3138 (if (looking-at "(")
3139 (progn
3140 (forward-sexp)
3141 (verilog-forward-ws&directives)))
3142 (point))
3144 (goto-char (match-end 0))
3145 (verilog-forward-ws&directives)
3146 (point))))
3147 (error "Malformed case item"))))
3148 (setq str (buffer-substring b e))
3150 (setq e
3151 (string-match
3152 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
3153 (setq str (concat (substring str 0 e) "...")))
3154 str)
3155 'nil)))
3159 ;; Other functions
3162 (defun verilog-kill-existing-comment ()
3163 "Kill auto comment on this line."
3164 (save-excursion
3165 (let* (
3166 (e (progn
3167 (end-of-line)
3168 (point)))
3169 (b (progn
3170 (beginning-of-line)
3171 (search-forward "//" e t))))
3172 (if b
3173 (delete-region (- b 2) e)))))
3175 (defconst verilog-directive-nest-re
3176 (concat "\\(`else\\>\\)\\|"
3177 "\\(`endif\\>\\)\\|"
3178 "\\(`if\\>\\)\\|"
3179 "\\(`ifdef\\>\\)\\|"
3180 "\\(`ifndef\\>\\)"))
3181 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
3182 "Add ending comment with given INDENT-STR.
3183 With KILL-EXISTING-COMMENT, remove what was there before.
3184 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
3185 Insert `// case expr ' if this line ends a case block.
3186 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
3187 Insert `// NAME ' if this line ends a function, task, module,
3188 primitive or interface named NAME."
3189 (save-excursion
3190 (cond
3191 (; Comment close preprocessor directives
3192 (and
3193 (looking-at "\\(`endif\\)\\|\\(`else\\)")
3194 (or kill-existing-comment
3195 (not (save-excursion
3196 (end-of-line)
3197 (search-backward "//" (verilog-get-beg-of-line) t)))))
3198 (let ((nest 1) b e
3200 (else (if (match-end 2) "!" " ")))
3201 (end-of-line)
3202 (if kill-existing-comment
3203 (verilog-kill-existing-comment))
3204 (delete-horizontal-space)
3205 (save-excursion
3206 (backward-sexp 1)
3207 (while (and (/= nest 0)
3208 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
3209 (cond
3210 ((match-end 1) ; `else
3211 (if (= nest 1)
3212 (setq else "!")))
3213 ((match-end 2) ; `endif
3214 (setq nest (1+ nest)))
3215 ((match-end 3) ; `if
3216 (setq nest (1- nest)))
3217 ((match-end 4) ; `ifdef
3218 (setq nest (1- nest)))
3219 ((match-end 5) ; `ifndef
3220 (setq nest (1- nest)))))
3221 (if (match-end 0)
3222 (setq
3223 m (buffer-substring
3224 (match-beginning 0)
3225 (match-end 0))
3226 b (progn
3227 (skip-chars-forward "^ \t")
3228 (verilog-forward-syntactic-ws)
3229 (point))
3230 e (progn
3231 (skip-chars-forward "a-zA-Z0-9_")
3232 (point)))))
3233 (if b
3234 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
3235 (insert (concat " // " else m " " (buffer-substring b e))))
3236 (progn
3237 (insert " // unmatched `else or `endif")
3238 (ding 't)))))
3240 (; Comment close case/class/function/task/module and named block
3241 (and (looking-at "\\<end")
3242 (or kill-existing-comment
3243 (not (save-excursion
3244 (end-of-line)
3245 (search-backward "//" (verilog-get-beg-of-line) t)))))
3246 (let ((type (car indent-str)))
3247 (unless (eq type 'declaration)
3248 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ;; ignore named ends
3249 (if (looking-at verilog-end-block-ordered-re)
3250 (cond
3251 (;- This is a case block; search back for the start of this case
3252 (match-end 1) ;; of verilog-end-block-ordered-re
3254 (let ((err 't)
3255 (str "UNMATCHED!!"))
3256 (save-excursion
3257 (verilog-leap-to-head)
3258 (cond
3259 ((looking-at "\\<randcase\\>")
3260 (setq str "randcase")
3261 (setq err nil))
3262 ((match-end 0)
3263 (goto-char (match-end 1))
3264 (if nil
3265 (let (s f)
3266 (setq s (match-beginning 1))
3267 (setq f (progn (end-of-line)
3268 (point)))
3269 (setq str (buffer-substring s f)))
3270 (setq err nil))
3271 (setq str (concat (buffer-substring (match-beginning 1) (match-end 1))
3273 (verilog-get-expr))))))
3274 (end-of-line)
3275 (if kill-existing-comment
3276 (verilog-kill-existing-comment))
3277 (delete-horizontal-space)
3278 (insert (concat " // " str ))
3279 (if err (ding 't))))
3281 (;- This is a begin..end block
3282 (match-end 2) ;; of verilog-end-block-ordered-re
3283 (let ((str " // UNMATCHED !!")
3284 (err 't)
3285 (here (point))
3286 there
3287 cntx)
3288 (save-excursion
3289 (verilog-leap-to-head)
3290 (setq there (point))
3291 (if (not (match-end 0))
3292 (progn
3293 (goto-char here)
3294 (end-of-line)
3295 (if kill-existing-comment
3296 (verilog-kill-existing-comment))
3297 (delete-horizontal-space)
3298 (insert str)
3299 (ding 't))
3300 (let ((lim
3301 (save-excursion (verilog-beg-of-defun) (point)))
3302 (here (point)))
3303 (cond
3304 (;-- handle named block differently
3305 (looking-at verilog-named-block-re)
3306 (search-forward ":")
3307 (setq there (point))
3308 (setq str (verilog-get-expr))
3309 (setq err nil)
3310 (setq str (concat " // block: " str )))
3312 ((verilog-in-case-region-p) ;-- handle case item differently
3313 (goto-char here)
3314 (setq str (verilog-backward-case-item lim))
3315 (setq there (point))
3316 (setq err nil)
3317 (setq str (concat " // case: " str )))
3319 (;- try to find "reason" for this begin
3320 (cond
3322 (eq here (progn
3323 (verilog-backward-token)
3324 (verilog-beg-of-statement-1)
3325 (point)))
3326 (setq err nil)
3327 (setq str ""))
3328 ((looking-at verilog-endcomment-reason-re)
3329 (setq there (match-end 0))
3330 (setq cntx (concat
3331 (buffer-substring (match-beginning 0) (match-end 0)) " "))
3332 (cond
3333 (;- begin
3334 (match-end 2)
3335 (setq err nil)
3336 (save-excursion
3337 (if (and (verilog-continued-line)
3338 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
3339 (progn
3340 (goto-char (match-end 0))
3341 (setq there (point))
3342 (setq str
3343 (concat " // "
3344 (buffer-substring (match-beginning 0) (match-end 0)) " "
3345 (verilog-get-expr))))
3346 (setq str ""))))
3348 (;- else
3349 (match-end 4)
3350 (let ((nest 0)
3351 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)"))
3352 (catch 'skip
3353 (while (verilog-re-search-backward reg nil 'move)
3354 (cond
3355 ((match-end 1) ; begin
3356 (setq nest (1- nest)))
3357 ((match-end 2) ; end
3358 (setq nest (1+ nest)))
3359 ((match-end 3)
3360 (if (= 0 nest)
3361 (progn
3362 (goto-char (match-end 0))
3363 (setq there (point))
3364 (setq err nil)
3365 (setq str (verilog-get-expr))
3366 (setq str (concat " // else: !if" str ))
3367 (throw 'skip 1)))))))))
3369 (;- end else
3370 (match-end 5)
3371 (goto-char there)
3372 (let ((nest 0)
3373 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)"))
3374 (catch 'skip
3375 (while (verilog-re-search-backward reg nil 'move)
3376 (cond
3377 ((match-end 1) ; begin
3378 (setq nest (1- nest)))
3379 ((match-end 2) ; end
3380 (setq nest (1+ nest)))
3381 ((match-end 3)
3382 (if (= 0 nest)
3383 (progn
3384 (goto-char (match-end 0))
3385 (setq there (point))
3386 (setq err nil)
3387 (setq str (verilog-get-expr))
3388 (setq str (concat " // else: !if" str ))
3389 (throw 'skip 1)))))))))
3391 (;- task/function/initial et cetera
3393 (match-end 0)
3394 (goto-char (match-end 0))
3395 (setq there (point))
3396 (setq err nil)
3397 (setq str (verilog-get-expr))
3398 (setq str (concat " // " cntx str )))
3400 (;-- otherwise...
3401 (setq str " // auto-endcomment confused "))))
3403 ((and
3404 (verilog-in-case-region-p) ;-- handle case item differently
3405 (progn
3406 (setq there (point))
3407 (goto-char here)
3408 (setq str (verilog-backward-case-item lim))))
3409 (setq err nil)
3410 (setq str (concat " // case: " str )))
3412 ((verilog-in-fork-region-p)
3413 (setq err nil)
3414 (setq str " // fork branch" ))
3416 ((looking-at "\\<end\\>")
3417 ;; HERE
3418 (forward-word 1)
3419 (verilog-forward-syntactic-ws)
3420 (setq err nil)
3421 (setq str (verilog-get-expr))
3422 (setq str (concat " // " cntx str )))
3424 ))))
3425 (goto-char here)
3426 (end-of-line)
3427 (if kill-existing-comment
3428 (verilog-kill-existing-comment))
3429 (delete-horizontal-space)
3430 (if (or err
3431 (> (count-lines here there) verilog-minimum-comment-distance))
3432 (insert str))
3433 (if err (ding 't))
3434 ))))
3435 (;- this is endclass, which can be nested
3436 (match-end 11) ;; of verilog-end-block-ordered-re
3437 ;;(goto-char there)
3438 (let ((nest 0)
3439 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
3440 string)
3441 (save-excursion
3442 (catch 'skip
3443 (while (verilog-re-search-backward reg nil 'move)
3444 (cond
3445 ((match-end 3) ; endclass
3446 (ding 't)
3447 (setq string "unmatched endclass")
3448 (throw 'skip 1))
3450 ((match-end 2) ; endclass
3451 (setq nest (1+ nest)))
3453 ((match-end 1) ; class
3454 (setq nest (1- nest))
3455 (if (< nest 0)
3456 (progn
3457 (goto-char (match-end 0))
3458 (let (b e)
3459 (setq b (progn
3460 (skip-chars-forward "^ \t")
3461 (verilog-forward-ws&directives)
3462 (point))
3463 e (progn
3464 (skip-chars-forward "a-zA-Z0-9_")
3465 (point)))
3466 (setq string (buffer-substring b e)))
3467 (throw 'skip 1))))
3468 ))))
3469 (end-of-line)
3470 (insert (concat " // " string ))))
3472 (;- this is end{function,generate,task,module,primitive,table,generate}
3473 ;- which can not be nested.
3475 (let (string reg (name-re nil))
3476 (end-of-line)
3477 (if kill-existing-comment
3478 (save-match-data
3479 (verilog-kill-existing-comment)))
3480 (delete-horizontal-space)
3481 (backward-sexp)
3482 (cond
3483 ((match-end 5) ;; of verilog-end-block-ordered-re
3484 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
3485 (setq name-re "\\w+\\s-*(")
3487 ((match-end 6) ;; of verilog-end-block-ordered-re
3488 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)"))
3489 ((match-end 7) ;; of verilog-end-block-ordered-re
3490 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
3491 ((match-end 8) ;; of verilog-end-block-ordered-re
3492 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3493 ((match-end 9) ;; of verilog-end-block-ordered-re
3494 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
3495 ((match-end 10) ;; of verilog-end-block-ordered-re
3496 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3497 ((match-end 11) ;; of verilog-end-block-ordered-re
3498 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3499 ((match-end 12) ;; of verilog-end-block-ordered-re
3500 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3501 ((match-end 13) ;; of verilog-end-block-ordered-re
3502 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3503 ((match-end 14) ;; of verilog-end-block-ordered-re
3504 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3505 ((match-end 15) ;; of verilog-end-block-ordered-re
3506 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
3508 (t (error "Problem in verilog-set-auto-endcomments")))
3509 (let (b e)
3510 (save-excursion
3511 (verilog-re-search-backward reg nil 'move)
3512 (cond
3513 ((match-end 1)
3514 (setq b (progn
3515 (skip-chars-forward "^ \t")
3516 (verilog-forward-ws&directives)
3517 (if (and name-re (verilog-re-search-forward name-re nil 'move))
3518 (progn
3519 (goto-char (match-beginning 0))
3520 (verilog-forward-ws&directives)))
3521 (point))
3522 e (progn
3523 (skip-chars-forward "a-zA-Z0-9_")
3524 (point)))
3525 (setq string (buffer-substring b e)))
3527 (ding 't)
3528 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
3529 (end-of-line)
3530 (insert (concat " // " string )))
3531 ))))))))))
3533 (defun verilog-get-expr()
3534 "Grab expression at point, e.g, case ( a | b & (c ^d))."
3535 (let* ((b (progn
3536 (verilog-forward-syntactic-ws)
3537 (skip-chars-forward " \t")
3538 (point)))
3539 (e (let ((par 1))
3540 (cond
3541 ((looking-at "@")
3542 (forward-char 1)
3543 (verilog-forward-syntactic-ws)
3544 (if (looking-at "(")
3545 (progn
3546 (forward-char 1)
3547 (while (and (/= par 0)
3548 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
3549 (cond
3550 ((match-end 1)
3551 (setq par (1+ par)))
3552 ((match-end 2)
3553 (setq par (1- par)))))))
3554 (point))
3555 ((looking-at "(")
3556 (forward-char 1)
3557 (while (and (/= par 0)
3558 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
3559 (cond
3560 ((match-end 1)
3561 (setq par (1+ par)))
3562 ((match-end 2)
3563 (setq par (1- par)))))
3564 (point))
3565 ((looking-at "\\[")
3566 (forward-char 1)
3567 (while (and (/= par 0)
3568 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
3569 (cond
3570 ((match-end 1)
3571 (setq par (1+ par)))
3572 ((match-end 2)
3573 (setq par (1- par)))))
3574 (verilog-forward-syntactic-ws)
3575 (skip-chars-forward "^ \t\n\f")
3576 (point))
3577 ((looking-at "/[/\\*]")
3580 (skip-chars-forward "^: \t\n\f")
3581 (point)))))
3582 (str (buffer-substring b e)))
3583 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
3584 (setq str (concat (substring str 0 e) "...")))
3585 str))
3587 (defun verilog-expand-vector ()
3588 "Take a signal vector on the current line and expand it to multiple lines.
3589 Useful for creating tri's and other expanded fields."
3590 (interactive)
3591 (verilog-expand-vector-internal "[" "]"))
3593 (defun verilog-expand-vector-internal (bra ket)
3594 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
3595 (save-excursion
3596 (forward-line 0)
3597 (let ((signal-string (buffer-substring (point)
3598 (progn
3599 (end-of-line) (point)))))
3600 (if (string-match
3601 (concat "\\(.*\\)"
3602 (regexp-quote bra)
3603 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
3604 (regexp-quote ket)
3605 "\\(.*\\)$") signal-string)
3606 (let* ((sig-head (match-string 1 signal-string))
3607 (vec-start (string-to-number (match-string 2 signal-string)))
3608 (vec-end (if (= (match-beginning 3) (match-end 3))
3609 vec-start
3610 (string-to-number
3611 (substring signal-string (1+ (match-beginning 3))
3612 (match-end 3)))))
3613 (vec-range
3614 (if (= (match-beginning 4) (match-end 4))
3616 (string-to-number
3617 (substring signal-string (+ 2 (match-beginning 4))
3618 (match-end 4)))))
3619 (sig-tail (match-string 5 signal-string))
3620 vec)
3621 ;; Decode vectors
3622 (setq vec nil)
3623 (if (< vec-range 0)
3624 (let ((tmp vec-start))
3625 (setq vec-start vec-end
3626 vec-end tmp
3627 vec-range (- vec-range))))
3628 (if (< vec-end vec-start)
3629 (while (<= vec-end vec-start)
3630 (setq vec (append vec (list vec-start)))
3631 (setq vec-start (- vec-start vec-range)))
3632 (while (<= vec-start vec-end)
3633 (setq vec (append vec (list vec-start)))
3634 (setq vec-start (+ vec-start vec-range))))
3636 ;; Delete current line
3637 (delete-region (point) (progn (forward-line 0) (point)))
3639 ;; Expand vector
3640 (while vec
3641 (insert (concat sig-head bra
3642 (int-to-string (car vec)) ket sig-tail "\n"))
3643 (setq vec (cdr vec)))
3644 (delete-char -1)
3646 )))))
3648 (defun verilog-strip-comments ()
3649 "Strip all comments from the Verilog code."
3650 (interactive)
3651 (goto-char (point-min))
3652 (while (re-search-forward "//" nil t)
3653 (if (verilog-within-string)
3654 (re-search-forward "\"" nil t)
3655 (if (verilog-in-star-comment-p)
3656 (re-search-forward "\*/" nil t)
3657 (let ((bpt (- (point) 2)))
3658 (end-of-line)
3659 (delete-region bpt (point))))))
3661 (goto-char (point-min))
3662 (while (re-search-forward "/\\*" nil t)
3663 (if (verilog-within-string)
3664 (re-search-forward "\"" nil t)
3665 (let ((bpt (- (point) 2)))
3666 (re-search-forward "\\*/")
3667 (delete-region bpt (point))))))
3669 (defun verilog-one-line ()
3670 "Convert structural Verilog instances to occupy one line."
3671 (interactive)
3672 (goto-char (point-min))
3673 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
3674 (replace-match "\\1 " nil nil)))
3676 (defun verilog-linter-name ()
3677 "Return name of linter, either surelint or verilint."
3678 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
3679 compile-command))
3680 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
3681 verilog-linter)))
3682 (cond ((equal compile-word1 "surelint") `surelint)
3683 ((equal compile-word1 "verilint") `verilint)
3684 ((equal lint-word1 "surelint") `surelint)
3685 ((equal lint-word1 "verilint") `verilint)
3686 (t `surelint)))) ;; back compatibility
3688 (defun verilog-lint-off ()
3689 "Convert a Verilog linter warning line into a disable statement.
3690 For example:
3691 pci_bfm_null.v, line 46: Unused input: pci_rst_
3692 becomes a comment for the appropriate tool.
3694 The first word of the `compile-command' or `verilog-linter'
3695 variables is used to determine which product is being used.
3697 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
3698 (interactive)
3699 (let ((linter (verilog-linter-name)))
3700 (cond ((equal linter `surelint)
3701 (verilog-surelint-off))
3702 ((equal linter `verilint)
3703 (verilog-verilint-off))
3704 (t (error "Linter name not set")))))
3706 (defvar compilation-last-buffer)
3708 (defun verilog-surelint-off ()
3709 "Convert a SureLint warning line into a disable statement.
3710 Run from Verilog source window; assumes there is a *compile* buffer
3711 with point set appropriately.
3713 For example:
3714 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
3715 becomes:
3716 // surefire lint_line_off UDDONX"
3717 (interactive)
3718 (let ((buff (if (boundp 'next-error-last-buffer)
3719 next-error-last-buffer
3720 compilation-last-buffer)))
3721 (when (buffer-live-p buff)
3722 ;; FIXME with-current-buffer?
3723 (save-excursion
3724 (switch-to-buffer buff)
3725 (beginning-of-line)
3726 (when
3727 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
3728 (let* ((code (match-string 2))
3729 (file (match-string 3))
3730 (line (match-string 4))
3731 (buffer (get-file-buffer file))
3732 dir filename)
3733 (unless buffer
3734 (progn
3735 (setq buffer
3736 (and (file-exists-p file)
3737 (find-file-noselect file)))
3738 (or buffer
3739 (let* ((pop-up-windows t))
3740 (let ((name (expand-file-name
3741 (read-file-name
3742 (format "Find this error in: (default %s) "
3743 file)
3744 dir file t))))
3745 (if (file-directory-p name)
3746 (setq name (expand-file-name filename name)))
3747 (setq buffer
3748 (and (file-exists-p name)
3749 (find-file-noselect name))))))))
3750 (switch-to-buffer buffer)
3751 (goto-line (string-to-number line))
3752 (end-of-line)
3753 (catch 'already
3754 (cond
3755 ((verilog-in-slash-comment-p)
3756 (re-search-backward "//")
3757 (cond
3758 ((looking-at "// surefire lint_off_line ")
3759 (goto-char (match-end 0))
3760 (let ((lim (save-excursion (end-of-line) (point))))
3761 (if (re-search-forward code lim 'move)
3762 (throw 'already t)
3763 (insert (concat " " code)))))
3766 ((verilog-in-star-comment-p)
3767 (re-search-backward "/\*")
3768 (insert (format " // surefire lint_off_line %6s" code )))
3770 (insert (format " // surefire lint_off_line %6s" code ))
3771 )))))))))
3773 (defun verilog-verilint-off ()
3774 "Convert a Verilint warning line into a disable statement.
3776 For example:
3777 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
3778 becomes:
3779 //Verilint 240 off // WARNING: Unused input"
3780 (interactive)
3781 (save-excursion
3782 (beginning-of-line)
3783 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
3784 (replace-match (format
3785 ;; %3s makes numbers 1-999 line up nicely
3786 "\\1//Verilint %3s off // WARNING: \\3"
3787 (match-string 2)))
3788 (beginning-of-line)
3789 (verilog-indent-line))))
3791 (defun verilog-auto-save-compile ()
3792 "Update automatics with \\[verilog-auto], save the buffer, and compile."
3793 (interactive)
3794 (verilog-auto) ; Always do it for safety
3795 (save-buffer)
3796 (compile compile-command))
3801 ;; Batch
3804 (defmacro verilog-batch-error-wrapper (&rest body)
3805 "Execute BODY and add error prefix to any errors found.
3806 This lets programs calling batch mode to easily extract error messages."
3807 `(condition-case err
3808 (progn ,@body)
3809 (error
3810 (error "%%Error: %s%s" (error-message-string err)
3811 (if (featurep 'xemacs) "\n" ""))))) ;; XEmacs forgets to add a newline
3813 (defun verilog-batch-execute-func (funref)
3814 "Internal processing of a batch command, running FUNREF on all command arguments."
3815 (verilog-batch-error-wrapper
3816 ;; General globals needed
3817 (setq make-backup-files nil)
3818 (setq-default make-backup-files nil)
3819 (setq enable-local-variables t)
3820 (setq enable-local-eval t)
3821 ;; Make sure any sub-files we read get proper mode
3822 (setq default-major-mode `verilog-mode)
3823 ;; Ditto files already read in
3824 (mapc (lambda (buf)
3825 (when (buffer-file-name buf)
3826 (save-excursion
3827 (set-buffer buf)
3828 (verilog-mode))))
3829 (buffer-list))
3830 ;; Process the files
3831 (mapcar '(lambda (buf)
3832 (when (buffer-file-name buf)
3833 (save-excursion
3834 (if (not (file-exists-p (buffer-file-name buf)))
3835 (error
3836 (concat "File not found: " (buffer-file-name buf))))
3837 (message (concat "Processing " (buffer-file-name buf)))
3838 (set-buffer buf)
3839 (funcall funref)
3840 (save-buffer))))
3841 (buffer-list))))
3843 (defun verilog-batch-auto ()
3844 "For use with --batch, perform automatic expansions as a stand-alone tool.
3845 This sets up the appropriate Verilog mode environment, updates automatics
3846 with \\[verilog-auto] on all command-line files, and saves the buffers.
3847 For proper results, multiple filenames need to be passed on the command
3848 line in bottom-up order."
3849 (unless noninteractive
3850 (error "Use verilog-batch-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
3851 (verilog-batch-execute-func `verilog-auto))
3853 (defun verilog-batch-delete-auto ()
3854 "For use with --batch, perform automatic deletion as a stand-alone tool.
3855 This sets up the appropriate Verilog mode environment, deletes automatics
3856 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
3857 (unless noninteractive
3858 (error "Use verilog-batch-delete-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
3859 (verilog-batch-execute-func `verilog-delete-auto))
3861 (defun verilog-batch-inject-auto ()
3862 "For use with --batch, perform automatic injection as a stand-alone tool.
3863 This sets up the appropriate Verilog mode environment, injects new automatics
3864 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
3865 For proper results, multiple filenames need to be passed on the command
3866 line in bottom-up order."
3867 (unless noninteractive
3868 (error "Use verilog-batch-inject-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
3869 (verilog-batch-execute-func `verilog-inject-auto))
3871 (defun verilog-batch-indent ()
3872 "For use with --batch, reindent an a entire file as a stand-alone tool.
3873 This sets up the appropriate Verilog mode environment, calls
3874 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
3875 (unless noninteractive
3876 (error "Use verilog-batch-indent only with --batch")) ;; Otherwise we'd mess up buffer modes
3877 (verilog-batch-execute-func `verilog-indent-buffer))
3881 ;; Indentation
3883 (defconst verilog-indent-alist
3884 '((block . (+ ind verilog-indent-level))
3885 (case . (+ ind verilog-case-indent))
3886 (cparenexp . (+ ind verilog-indent-level))
3887 (cexp . (+ ind verilog-cexp-indent))
3888 (defun . verilog-indent-level-module)
3889 (declaration . verilog-indent-level-declaration)
3890 (directive . (verilog-calculate-indent-directive))
3891 (tf . verilog-indent-level)
3892 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
3893 (statement . ind)
3894 (cpp . 0)
3895 (comment . (verilog-comment-indent))
3896 (unknown . 3)
3897 (string . 0)))
3899 (defun verilog-continued-line-1 (lim)
3900 "Return true if this is a continued line.
3901 Set point to where line starts. Limit search to point LIM."
3902 (let ((continued 't))
3903 (if (eq 0 (forward-line -1))
3904 (progn
3905 (end-of-line)
3906 (verilog-backward-ws&directives lim)
3907 (if (bobp)
3908 (setq continued nil)
3909 (setq continued (verilog-backward-token))))
3910 (setq continued nil))
3911 continued))
3913 (defun verilog-calculate-indent ()
3914 "Calculate the indent of the current Verilog line.
3915 Examine previous lines. Once a line is found that is definitive as to the
3916 type of the current line, return that lines' indent level and its type.
3917 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
3918 (save-excursion
3919 (let* ((starting_position (point))
3920 (par 0)
3921 (begin (looking-at "[ \t]*begin\\>"))
3922 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
3923 (type (catch 'nesting
3924 ;; Keep working backwards until we can figure out
3925 ;; what type of statement this is.
3926 ;; Basically we need to figure out
3927 ;; 1) if this is a continuation of the previous line;
3928 ;; 2) are we in a block scope (begin..end)
3930 ;; if we are in a comment, done.
3931 (if (verilog-in-star-comment-p)
3932 (throw 'nesting 'comment))
3934 ;; if we have a directive, done.
3935 (if (save-excursion (beginning-of-line) (looking-at verilog-directive-re-1))
3936 (throw 'nesting 'directive))
3938 ;; unless we are in the newfangled coverpoint or constraint blocks
3939 ;; if we are in a parenthesized list, and the user likes to indent these, return.
3940 (if (and
3941 verilog-indent-lists
3942 (not (verilog-in-coverage))
3943 (verilog-in-paren))
3944 (progn (setq par 1)
3945 (throw 'nesting 'block)))
3947 ;; See if we are continuing a previous line
3948 (while t
3949 ;; trap out if we crawl off the top of the buffer
3950 (if (bobp) (throw 'nesting 'cpp))
3952 (if (verilog-continued-line-1 lim)
3953 (let ((sp (point)))
3954 (if (and
3955 (not (looking-at verilog-complete-reg))
3956 (verilog-continued-line-1 lim))
3957 (progn (goto-char sp)
3958 (throw 'nesting 'cexp))
3960 (goto-char sp))
3962 (if (and begin
3963 (not verilog-indent-begin-after-if)
3964 (looking-at verilog-no-indent-begin-re))
3965 (progn
3966 (beginning-of-line)
3967 (skip-chars-forward " \t")
3968 (throw 'nesting 'statement))
3969 (progn
3970 (throw 'nesting 'cexp))))
3971 ;; not a continued line
3972 (goto-char starting_position))
3974 (if (looking-at "\\<else\\>")
3975 ;; search back for governing if, striding across begin..end pairs
3976 ;; appropriately
3977 (let ((elsec 1))
3978 (while (verilog-re-search-backward verilog-ends-re nil 'move)
3979 (cond
3980 ((match-end 1) ; else, we're in deep
3981 (setq elsec (1+ elsec)))
3982 ((match-end 2) ; if
3983 (setq elsec (1- elsec))
3984 (if (= 0 elsec)
3985 (if verilog-align-ifelse
3986 (throw 'nesting 'statement)
3987 (progn ;; back up to first word on this line
3988 (beginning-of-line)
3989 (verilog-forward-syntactic-ws)
3990 (throw 'nesting 'statement)))))
3991 (t ; endblock
3992 ; try to leap back to matching outward block by striding across
3993 ; indent level changing tokens then immediately
3994 ; previous line governs indentation.
3995 (let (( reg) (nest 1))
3996 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
3997 (cond
3998 ((match-end 3) ; end
3999 ;; Search back for matching begin
4000 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
4001 ((match-end 4) ; endcase
4002 ;; Search back for matching case
4003 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
4004 ((match-end 5) ; endfunction
4005 ;; Search back for matching function
4006 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
4007 ((match-end 6) ; endtask
4008 ;; Search back for matching task
4009 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
4010 ((match-end 7) ; endspecify
4011 ;; Search back for matching specify
4012 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
4013 ((match-end 8) ; endtable
4014 ;; Search back for matching table
4015 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
4016 ((match-end 9) ; endgenerate
4017 ;; Search back for matching generate
4018 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
4019 ((match-end 10) ; joins
4020 ;; Search back for matching fork
4021 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
4022 ((match-end 11) ; class
4023 ;; Search back for matching class
4024 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
4025 ((match-end 12) ; covergroup
4026 ;; Search back for matching covergroup
4027 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
4028 (catch 'skip
4029 (while (verilog-re-search-backward reg nil 'move)
4030 (cond
4031 ((match-end 1) ; begin
4032 (setq nest (1- nest))
4033 (if (= 0 nest)
4034 (throw 'skip 1)))
4035 ((match-end 2) ; end
4036 (setq nest (1+ nest)))))
4037 )))))))
4038 (throw 'nesting (verilog-calc-1)))
4039 );; catch nesting
4040 );; type
4042 ;; Return type of block and indent level.
4043 (if (not type)
4044 (setq type 'cpp))
4045 (if (> par 0) ; Unclosed Parenthesis
4046 (list 'cparenexp par)
4047 (cond
4048 ((eq type 'case)
4049 (list type (verilog-case-indent-level)))
4050 ((eq type 'statement)
4051 (list type (current-column)))
4052 ((eq type 'defun)
4053 (list type 0))
4055 (list type (verilog-current-indent-level))))))))
4057 (defun verilog-wai ()
4058 "Show matching nesting block for debugging."
4059 (interactive)
4060 (save-excursion
4061 (let ((nesting (verilog-calc-1)))
4062 (message "You are at nesting %s" nesting))))
4064 (defun verilog-calc-1 ()
4065 (catch 'nesting
4066 (while (verilog-re-search-backward (concat "\\({\\|}\\|" verilog-indent-re "\\)") nil 'move)
4067 (cond
4068 ((equal (char-after) ?\{)
4069 (if (verilog-at-constraint-p)
4070 (throw 'nesting 'block)))
4071 ((equal (char-after) ?\})
4073 (let ((there (verilog-at-close-constraint-p)))
4074 (if there (goto-char there))))
4076 ((looking-at verilog-beg-block-re-ordered)
4077 (cond
4078 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
4079 (let ((here (point)))
4080 (verilog-beg-of-statement)
4081 (if (looking-at verilog-extended-case-re)
4082 (throw 'nesting 'case)
4083 (goto-char here)))
4084 (throw 'nesting 'case))
4086 ((match-end 4) ; *sigh* could be "disable fork"
4087 (let ((here (point)))
4088 (verilog-beg-of-statement)
4089 (if (looking-at verilog-disable-fork-re)
4090 t ; is disable fork, this is a normal statement
4091 (progn ; or is fork, starts a new block
4092 (goto-char here)
4093 (throw 'nesting 'block)))))
4096 ;; need to consider typedef struct here...
4097 ((looking-at "\\<class\\|struct\\|function\\|task\\|property\\>")
4098 ; *sigh* These words have an optional prefix:
4099 ; extern {virtual|protected}? function a();
4100 ; assert property (p_1);
4101 ; typedef class foo;
4102 ; and we don't want to confuse this with
4103 ; function a();
4104 ; property
4105 ; ...
4106 ; endfunction
4107 (let ((here (point)))
4108 (save-excursion
4109 (verilog-beg-of-statement)
4110 (if (= (point) here)
4111 (throw 'nesting 'block)))))
4112 (t (throw 'nesting 'block))))
4114 ((looking-at verilog-end-block-re)
4115 (verilog-leap-to-head)
4116 (if (verilog-in-case-region-p)
4117 (progn
4118 (verilog-leap-to-case-head)
4119 (if (looking-at verilog-case-re)
4120 (throw 'nesting 'case)))))
4122 ((looking-at (if (verilog-in-generate-region-p)
4123 verilog-defun-level-not-generate-re
4124 verilog-defun-level-re))
4125 (throw 'nesting 'defun))
4127 ((looking-at verilog-cpp-level-re)
4128 (throw 'nesting 'cpp))
4130 ((bobp)
4131 (throw 'nesting 'cpp))))
4132 (throw 'nesting 'cpp)))
4134 (defun verilog-calculate-indent-directive ()
4135 "Return indentation level for directive.
4136 For speed, the searcher looks at the last directive, not the indent
4137 of the appropriate enclosing block."
4138 (let ((base -1) ;; Indent of the line that determines our indentation
4139 (ind 0)) ;; Relative offset caused by other directives (like `endif on same line as `else)
4140 ;; Start at current location, scan back for another directive
4142 (save-excursion
4143 (beginning-of-line)
4144 (while (and (< base 0)
4145 (verilog-re-search-backward verilog-directive-re nil t))
4146 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
4147 (setq base (current-indentation))))
4148 (cond ((and (looking-at verilog-directive-end) (< base 0)) ;; Only matters when not at BOL
4149 (setq ind (- ind verilog-indent-level-directive)))
4150 ((and (looking-at verilog-directive-middle) (>= base 0)) ;; Only matters when at BOL
4151 (setq ind (+ ind verilog-indent-level-directive)))
4152 ((looking-at verilog-directive-begin)
4153 (setq ind (+ ind verilog-indent-level-directive)))))
4154 ;; Adjust indent to starting indent of critical line
4155 (setq ind (max 0 (+ ind base))))
4157 (save-excursion
4158 (beginning-of-line)
4159 (skip-chars-forward " \t")
4160 (cond ((or (looking-at verilog-directive-middle)
4161 (looking-at verilog-directive-end))
4162 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
4163 ind))
4165 (defun verilog-leap-to-case-head ()
4166 (let ((nest 1))
4167 (while (/= 0 nest)
4168 (verilog-re-search-backward "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" nil 'move)
4169 (cond
4170 ((match-end 1)
4171 (setq nest (1- nest)))
4172 ((match-end 2)
4173 (setq nest (1+ nest)))
4174 ((bobp)
4175 (ding 't)
4176 (setq nest 0))))))
4178 (defun verilog-leap-to-head ()
4179 "Move point to the head of this block.
4180 Jump from end to matching begin, from endcase to matching case, and so on."
4181 (let ((reg nil)
4182 snest
4183 (nest 1))
4184 (cond
4185 ((looking-at "\\<end\\>")
4186 ;; 1: Search back for matching begin
4187 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
4188 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
4189 ((looking-at "\\<endcase\\>")
4190 ;; 2: Search back for matching case
4191 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)" ))
4192 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
4193 ;; 3: Search back for matching fork
4194 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
4195 ((looking-at "\\<endclass\\>")
4196 ;; 4: Search back for matching class
4197 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
4198 ((looking-at "\\<endtable\\>")
4199 ;; 5: Search back for matching table
4200 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
4201 ((looking-at "\\<endspecify\\>")
4202 ;; 6: Search back for matching specify
4203 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
4204 ((looking-at "\\<endfunction\\>")
4205 ;; 7: Search back for matching function
4206 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
4207 ((looking-at "\\<endgenerate\\>")
4208 ;; 8: Search back for matching generate
4209 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
4210 ((looking-at "\\<endtask\\>")
4211 ;; 9: Search back for matching task
4212 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
4213 ((looking-at "\\<endgroup\\>")
4214 ;; 10: Search back for matching covergroup
4215 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
4216 ((looking-at "\\<endproperty\\>")
4217 ;; 11: Search back for matching property
4218 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
4219 ((looking-at "\\<endinterface\\>")
4220 ;; 12: Search back for matching interface
4221 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
4222 ((looking-at "\\<endsequence\\>")
4223 ;; 12: Search back for matching sequence
4224 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
4225 ((looking-at "\\<endclocking\\>")
4226 ;; 12: Search back for matching clocking
4227 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
4228 (if reg
4229 (catch 'skip
4230 (let (sreg)
4231 (while (verilog-re-search-backward reg nil 'move)
4232 (cond
4233 ((match-end 1) ; begin
4234 (setq nest (1- nest))
4235 (if (= 0 nest)
4236 ;; Now previous line describes syntax
4237 (throw 'skip 1))
4238 (if (and snest
4239 (= snest nest))
4240 (setq reg sreg)))
4241 ((match-end 2) ; end
4242 (setq nest (1+ nest)))
4243 ((match-end 3)
4244 ;; endcase, jump to case
4245 (setq snest nest)
4246 (setq nest (1+ nest))
4247 (setq sreg reg)
4248 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
4249 ((match-end 4)
4250 ;; join, jump to fork
4251 (setq snest nest)
4252 (setq nest (1+ nest))
4253 (setq sreg reg)
4254 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
4255 )))))))
4257 (defun verilog-continued-line ()
4258 "Return true if this is a continued line.
4259 Set point to where line starts."
4260 (let ((continued 't))
4261 (if (eq 0 (forward-line -1))
4262 (progn
4263 (end-of-line)
4264 (verilog-backward-ws&directives)
4265 (if (bobp)
4266 (setq continued nil)
4267 (while (and continued
4268 (save-excursion
4269 (skip-chars-backward " \t")
4270 (not (bolp))))
4271 (setq continued (verilog-backward-token)))))
4272 (setq continued nil))
4273 continued))
4275 (defun verilog-backward-token ()
4276 "Step backward token, returning true if we are now at an end of line token."
4277 (interactive)
4278 (verilog-backward-syntactic-ws)
4279 (cond
4280 ((bolp)
4281 nil)
4282 (;-- Anything ending in a ; is complete
4283 (= (preceding-char) ?\;)
4284 nil)
4285 (; If a "}" is prefixed by a ";", then this is a complete statement
4286 ; i.e.: constraint foo { a = b; }
4287 (= (preceding-char) ?\})
4288 (progn
4289 (backward-char)
4290 (verilog-at-close-constraint-p)))
4291 (;-- constraint foo { a = b }
4292 ; is a complete statement. *sigh*
4293 (= (preceding-char) ?\{)
4294 (progn
4295 (backward-char)
4296 (not (verilog-at-constraint-p))))
4297 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
4298 ; also could be simply '@(foo)'
4299 ; or foo u1 #(a=8)
4300 ; (b, ... which ISN'T complete
4301 ;;;; Do we need this???
4302 (= (preceding-char) ?\))
4303 (progn
4304 (backward-char)
4305 (backward-up-list 1)
4306 (verilog-backward-syntactic-ws)
4307 (let ((back (point)))
4308 (forward-word -1)
4309 (cond
4310 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
4311 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
4313 (goto-char back)
4314 (cond
4315 ((= (preceding-char) ?\@)
4316 (backward-char)
4317 (save-excursion
4318 (verilog-backward-token)
4319 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
4320 ((= (preceding-char) ?\#)
4321 (backward-char))
4322 (t t)))))))
4324 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
4326 (forward-word -1)
4327 (cond
4328 ((looking-at "\\<else\\>")
4330 ((looking-at verilog-behavioral-block-beg-re)
4332 ((looking-at verilog-indent-re)
4333 nil)
4335 (let
4336 ((back (point)))
4337 (verilog-backward-syntactic-ws)
4338 (cond
4339 ((= (preceding-char) ?\:)
4340 (backward-char)
4341 (verilog-backward-syntactic-ws)
4342 (backward-sexp)
4343 (if (looking-at verilog-nameable-item-re )
4346 ((= (preceding-char) ?\#)
4347 (backward-char)
4349 ((= (preceding-char) ?\`)
4350 (backward-char)
4354 (goto-char back)
4355 t))))))))
4357 (defun verilog-backward-syntactic-ws (&optional bound)
4358 "Backward skip over syntactic whitespace for Emacs 19.
4359 Optional BOUND limits search."
4360 (save-restriction
4361 (let* ((bound (or bound (point-min))) (here bound) )
4362 (if (< bound (point))
4363 (progn
4364 (narrow-to-region bound (point))
4365 (while (/= here (point))
4366 (setq here (point))
4367 (verilog-skip-backward-comments))))))
4370 (defun verilog-forward-syntactic-ws (&optional bound)
4371 "Forward skip over syntactic whitespace for Emacs 19.
4372 Optional BOUND limits search."
4373 (save-restriction
4374 (let* ((bound (or bound (point-max)))
4375 (here bound))
4376 (if (> bound (point))
4377 (progn
4378 (narrow-to-region (point) bound)
4379 (while (/= here (point))
4380 (setq here (point))
4381 (forward-comment (buffer-size))))))))
4383 (defun verilog-backward-ws&directives (&optional bound)
4384 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
4385 Optional BOUND limits search."
4386 (save-restriction
4387 (let* ((bound (or bound (point-min)))
4388 (here bound)
4389 (p nil) )
4390 (if (< bound (point))
4391 (progn
4392 (let ((state (save-excursion (verilog-syntax-ppss))))
4393 (cond
4394 ((nth 7 state) ;; in // comment
4395 (verilog-re-search-backward "//" nil 'move)
4396 (skip-chars-backward "/"))
4397 ((nth 4 state) ;; in /* */ comment
4398 (verilog-re-search-backward "/\*" nil 'move))))
4399 (narrow-to-region bound (point))
4400 (while (/= here (point))
4401 (setq here (point))
4402 (verilog-skip-backward-comments)
4403 (setq p
4404 (save-excursion
4405 (beginning-of-line)
4406 (cond
4407 ((verilog-within-translate-off)
4408 (verilog-back-to-start-translate-off (point-min)))
4409 ((looking-at verilog-directive-re-1)
4410 (point))
4412 nil))))
4413 (if p (goto-char p))))))))
4415 (defun verilog-forward-ws&directives (&optional bound)
4416 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
4417 Optional BOUND limits search."
4418 (save-restriction
4419 (let* ((bound (or bound (point-max)))
4420 (here bound)
4421 jump)
4422 (if (> bound (point))
4423 (progn
4424 (let ((state (save-excursion (verilog-syntax-ppss))))
4425 (cond
4426 ((nth 7 state) ;; in // comment
4427 (verilog-re-search-forward "//" nil 'move))
4428 ((nth 4 state) ;; in /* */ comment
4429 (verilog-re-search-forward "/\*" nil 'move))))
4430 (narrow-to-region (point) bound)
4431 (while (/= here (point))
4432 (setq here (point)
4433 jump nil)
4434 (forward-comment (buffer-size))
4435 (save-excursion
4436 (beginning-of-line)
4437 (if (looking-at verilog-directive-re-1)
4438 (setq jump t)))
4439 (if jump
4440 (beginning-of-line 2))))))))
4442 (defun verilog-in-comment-p ()
4443 "Return true if in a star or // comment."
4444 (let ((state (save-excursion (verilog-syntax-ppss))))
4445 (or (nth 4 state) (nth 7 state))))
4447 (defun verilog-in-star-comment-p ()
4448 "Return true if in a star comment."
4449 (let ((state (save-excursion (verilog-syntax-ppss))))
4450 (and
4451 (nth 4 state) ; t if in a comment of style a // or b /**/
4452 (not
4453 (nth 7 state) ; t if in a comment of style b /**/
4454 ))))
4456 (defun verilog-in-slash-comment-p ()
4457 "Return true if in a slash comment."
4458 (let ((state (save-excursion (verilog-syntax-ppss))))
4459 (nth 7 state)))
4461 (defun verilog-in-comment-or-string-p ()
4462 "Return true if in a string or comment."
4463 (let ((state (save-excursion (verilog-syntax-ppss))))
4464 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
4466 (defun verilog-in-escaped-name-p ()
4467 "Return true if in an escaped name."
4468 (save-excursion
4469 (backward-char)
4470 (skip-chars-backward "^ \t\n\f")
4471 (if (equal (char-after (point) ) ?\\ )
4473 nil)))
4475 (defun verilog-in-paren ()
4476 "Return true if in a parenthetical expression."
4477 (let ((state (save-excursion (verilog-syntax-ppss))))
4478 (> (nth 0 state) 0 )))
4480 (defun verilog-in-coverage ()
4481 "Return true if in a constraint or coverpoint expression."
4482 (interactive)
4483 (save-excursion
4484 (if (verilog-in-paren)
4485 (progn
4486 (backward-up-list 1)
4487 (verilog-at-constraint-p)
4489 nil)))
4490 (defun verilog-at-close-constraint-p ()
4491 "If at the } that closes a constraint or covergroup, return true."
4492 (if (and
4493 (equal (char-after) ?\})
4494 (verilog-in-paren))
4496 (save-excursion
4497 (verilog-backward-ws&directives)
4498 (if (equal (char-before) ?\;)
4499 (point)
4500 nil))))
4502 (defun verilog-at-constraint-p ()
4503 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
4504 (if (save-excursion
4505 (and
4506 (equal (char-after) ?\{)
4507 (forward-list)
4508 (progn (backward-char 1)
4509 (verilog-backward-ws&directives)
4510 (equal (char-before) ?\;))))
4511 ;; maybe
4512 (verilog-re-search-backward "\\<constraint\\|coverpoint\\|cross\\>" nil 'move)
4513 ;; not
4514 nil))
4516 (defun verilog-parenthesis-depth ()
4517 "Return non zero if in parenthetical-expression."
4518 (save-excursion (nth 1 (verilog-syntax-ppss))))
4521 (defun verilog-skip-forward-comment-or-string ()
4522 "Return true if in a string or comment."
4523 (let ((state (save-excursion (verilog-syntax-ppss))))
4524 (cond
4525 ((nth 3 state) ;Inside string
4526 (search-forward "\"")
4528 ((nth 7 state) ;Inside // comment
4529 (forward-line 1)
4531 ((nth 4 state) ;Inside any comment (hence /**/)
4532 (search-forward "*/"))
4534 nil))))
4536 (defun verilog-skip-backward-comment-or-string ()
4537 "Return true if in a string or comment."
4538 (let ((state (save-excursion (verilog-syntax-ppss))))
4539 (cond
4540 ((nth 3 state) ;Inside string
4541 (search-backward "\"")
4543 ((nth 7 state) ;Inside // comment
4544 (search-backward "//")
4545 (skip-chars-backward "/")
4547 ((nth 4 state) ;Inside /* */ comment
4548 (search-backward "/*")
4551 nil))))
4553 (defun verilog-skip-backward-comments ()
4554 "Return true if a comment was skipped."
4555 (let ((more t))
4556 (while more
4557 (setq more
4558 (let ((state (save-excursion (verilog-syntax-ppss))))
4559 (cond
4560 ((nth 7 state) ;Inside // comment
4561 (search-backward "//")
4562 (skip-chars-backward "/")
4563 (skip-chars-backward " \t\n\f")
4565 ((nth 4 state) ;Inside /* */ comment
4566 (search-backward "/*")
4567 (skip-chars-backward " \t\n\f")
4569 ((and (not (bobp))
4570 (= (char-before) ?\/)
4571 (= (char-before (1- (point))) ?\*))
4572 (goto-char (- (point) 2))
4575 (skip-chars-backward " \t\n\f")
4576 nil)))))))
4578 (defun verilog-skip-forward-comment-p ()
4579 "If in comment, move to end and return true."
4580 (let (state)
4581 (progn
4582 (setq state (save-excursion (verilog-syntax-ppss)))
4583 (cond
4584 ((nth 3 state)
4586 ((nth 7 state) ;Inside // comment
4587 (end-of-line)
4588 (forward-char 1)
4590 ((nth 4 state) ;Inside any comment
4593 nil)))))
4595 (defun verilog-indent-line-relative ()
4596 "Cheap version of indent line.
4597 Only look at a few lines to determine indent level."
4598 (interactive)
4599 (let ((indent-str)
4600 (sp (point)))
4601 (if (looking-at "^[ \t]*$")
4602 (cond ;- A blank line; No need to be too smart.
4603 ((bobp)
4604 (setq indent-str (list 'cpp 0)))
4605 ((verilog-continued-line)
4606 (let ((sp1 (point)))
4607 (if (verilog-continued-line)
4608 (progn
4609 (goto-char sp)
4610 (setq indent-str
4611 (list 'statement (verilog-current-indent-level))))
4612 (goto-char sp1)
4613 (setq indent-str (list 'block (verilog-current-indent-level)))))
4614 (goto-char sp))
4615 ((goto-char sp)
4616 (setq indent-str (verilog-calculate-indent))))
4617 (progn (skip-chars-forward " \t")
4618 (setq indent-str (verilog-calculate-indent))))
4619 (verilog-do-indent indent-str)))
4621 (defun verilog-indent-line ()
4622 "Indent for special part of code."
4623 (verilog-do-indent (verilog-calculate-indent)))
4625 (defun verilog-do-indent (indent-str)
4626 (let ((type (car indent-str))
4627 (ind (car (cdr indent-str))))
4628 (cond
4629 (; handle continued exp
4630 (eq type 'cexp)
4631 (let ((here (point)))
4632 (verilog-backward-syntactic-ws)
4633 (cond
4634 ((or
4635 (= (preceding-char) ?\,)
4636 (= (preceding-char) ?\])
4637 (save-excursion
4638 (verilog-beg-of-statement-1)
4639 (looking-at verilog-declaration-re)))
4640 (let* ( fst
4641 (val
4642 (save-excursion
4643 (backward-char 1)
4644 (verilog-beg-of-statement-1)
4645 (setq fst (point))
4646 (if (looking-at verilog-declaration-re)
4647 (progn ;; we have multiple words
4648 (goto-char (match-end 0))
4649 (skip-chars-forward " \t")
4650 (cond
4651 ((and verilog-indent-declaration-macros
4652 (= (following-char) ?\`))
4653 (progn
4654 (forward-char 1)
4655 (forward-word 1)
4656 (skip-chars-forward " \t")))
4657 ((= (following-char) ?\[)
4658 (progn
4659 (forward-char 1)
4660 (backward-up-list -1)
4661 (skip-chars-forward " \t"))))
4662 (current-column))
4663 (progn
4664 (goto-char fst)
4665 (+ (current-column) verilog-cexp-indent))))))
4666 (goto-char here)
4667 (indent-line-to val)))
4668 ((= (preceding-char) ?\) )
4669 (goto-char here)
4670 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
4671 (indent-line-to val)))
4673 (goto-char here)
4674 (let ((val))
4675 (verilog-beg-of-statement-1)
4676 (if (and (< (point) here)
4677 (verilog-re-search-forward "=[ \\t]*" here 'move))
4678 (setq val (current-column))
4679 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
4680 (goto-char here)
4681 (indent-line-to val))))))
4683 (; handle inside parenthetical expressions
4684 (eq type 'cparenexp)
4685 (let ((val (save-excursion
4686 (backward-up-list 1)
4687 (forward-char 1)
4688 (skip-chars-forward " \t")
4689 (current-column))))
4690 (indent-line-to val)
4693 (;-- Handle the ends
4695 (looking-at verilog-end-block-re )
4696 (verilog-at-close-constraint-p))
4697 (let ((val (if (eq type 'statement)
4698 (- ind verilog-indent-level)
4699 ind)))
4700 (indent-line-to val)))
4702 (;-- Case -- maybe line 'em up
4703 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
4704 (progn
4705 (cond
4706 ((looking-at "\\<endcase\\>")
4707 (indent-line-to ind))
4709 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
4710 (indent-line-to val))))))
4712 (;-- defun
4713 (and (eq type 'defun)
4714 (looking-at verilog-zero-indent-re))
4715 (indent-line-to 0))
4717 (;-- declaration
4718 (and (or
4719 (eq type 'defun)
4720 (eq type 'block))
4721 (looking-at verilog-declaration-re))
4722 (verilog-indent-declaration ind))
4724 (;-- Everything else
4726 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
4727 (indent-line-to val))))
4729 (if (looking-at "[ \t]+$")
4730 (skip-chars-forward " \t"))
4731 indent-str ; Return indent data
4734 (defun verilog-current-indent-level ()
4735 "Return the indent-level of the current statement."
4736 (save-excursion
4737 (let (par-pos)
4738 (beginning-of-line)
4739 (setq par-pos (verilog-parenthesis-depth))
4740 (while par-pos
4741 (goto-char par-pos)
4742 (beginning-of-line)
4743 (setq par-pos (verilog-parenthesis-depth)))
4744 (skip-chars-forward " \t")
4745 (current-column))))
4747 (defun verilog-case-indent-level ()
4748 "Return the indent-level of the current statement.
4749 Do not count named blocks or case-statements."
4750 (save-excursion
4751 (skip-chars-forward " \t")
4752 (cond
4753 ((looking-at verilog-named-block-re)
4754 (current-column))
4755 ((and (not (looking-at verilog-case-re))
4756 (looking-at "^[^:;]+[ \t]*:"))
4757 (verilog-re-search-forward ":" nil t)
4758 (skip-chars-forward " \t")
4759 (current-column))
4761 (current-column)))))
4763 (defun verilog-indent-comment ()
4764 "Indent current line as comment."
4765 (let* ((stcol
4766 (cond
4767 ((verilog-in-star-comment-p)
4768 (save-excursion
4769 (re-search-backward "/\\*" nil t)
4770 (1+(current-column))))
4771 (comment-column
4772 comment-column )
4774 (save-excursion
4775 (re-search-backward "//" nil t)
4776 (current-column))))))
4777 (indent-line-to stcol)
4778 stcol))
4780 (defun verilog-more-comment ()
4781 "Make more comment lines like the previous."
4782 (let* ((star 0)
4783 (stcol
4784 (cond
4785 ((verilog-in-star-comment-p)
4786 (save-excursion
4787 (setq star 1)
4788 (re-search-backward "/\\*" nil t)
4789 (1+(current-column))))
4790 (comment-column
4791 comment-column )
4793 (save-excursion
4794 (re-search-backward "//" nil t)
4795 (current-column))))))
4796 (progn
4797 (indent-to stcol)
4798 (if (and star
4799 (save-excursion
4800 (forward-line -1)
4801 (skip-chars-forward " \t")
4802 (looking-at "\*")))
4803 (insert "* ")))))
4805 (defun verilog-comment-indent (&optional arg)
4806 "Return the column number the line should be indented to.
4807 ARG is ignored, for `comment-indent-function' compatibility."
4808 (cond
4809 ((verilog-in-star-comment-p)
4810 (save-excursion
4811 (re-search-backward "/\\*" nil t)
4812 (1+(current-column))))
4813 ( comment-column
4814 comment-column )
4816 (save-excursion
4817 (re-search-backward "//" nil t)
4818 (current-column)))))
4822 (defun verilog-pretty-declarations (&optional quiet)
4823 "Line up declarations around point."
4824 (interactive)
4825 (save-excursion
4826 (if (progn
4827 (verilog-beg-of-statement-1)
4828 (looking-at verilog-declaration-re))
4829 (let* ((m1 (make-marker))
4830 (e) (r)
4831 (here (point))
4832 ;; Start of declaration range
4833 (start
4834 (progn
4835 (verilog-beg-of-statement-1)
4836 (while (looking-at verilog-declaration-re)
4837 (beginning-of-line)
4838 (setq e (point))
4839 (verilog-backward-syntactic-ws)
4840 (backward-char)
4841 (verilog-beg-of-statement-1)) ;Ack, need to grok `define
4843 ;; End of declaration range
4844 (end
4845 (progn
4846 (goto-char here)
4847 (verilog-end-of-statement)
4848 (setq e (point)) ;Might be on last line
4849 (verilog-forward-syntactic-ws)
4850 (while (looking-at verilog-declaration-re)
4851 (beginning-of-line)
4852 (verilog-end-of-statement)
4853 (setq e (point))
4854 (verilog-forward-syntactic-ws))
4856 (edpos (set-marker (make-marker) end))
4857 (ind)
4858 (base-ind
4859 (progn
4860 (goto-char start)
4861 (verilog-do-indent (verilog-calculate-indent))
4862 (verilog-forward-ws&directives)
4863 (current-column))))
4864 (goto-char end)
4865 (goto-char start)
4866 (if (and (not quiet)
4867 (> (- end start) 100))
4868 (message "Lining up declarations..(please stand by)"))
4869 ;; Get the beginning of line indent first
4870 (while (progn (setq e (marker-position edpos))
4871 (< (point) e))
4872 (cond
4873 ( (save-excursion (skip-chars-backward " \t")
4874 (bolp))
4875 (verilog-forward-ws&directives)
4876 (indent-line-to base-ind)
4877 (verilog-forward-ws&directives)
4878 (verilog-re-search-forward "[ \t\n\f]" e 'move))
4880 (just-one-space)
4881 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
4882 ;;(forward-line)
4884 ;; Now find biggest prefix
4885 (setq ind (verilog-get-lineup-indent start edpos))
4886 ;; Now indent each line.
4887 (goto-char start)
4888 (while (progn (setq e (marker-position edpos))
4889 (setq r (- e (point)))
4890 (> r 0))
4891 (setq e (point))
4892 (unless quiet (message "%d" r))
4893 (cond
4894 ((or (and verilog-indent-declaration-macros
4895 (looking-at verilog-declaration-re-1-macro))
4896 (looking-at verilog-declaration-re-1-no-macro))
4897 (let ((p (match-end 0)))
4898 (set-marker m1 p)
4899 (if (verilog-re-search-forward "[[#`]" p 'move)
4900 (progn
4901 (forward-char -1)
4902 (just-one-space)
4903 (goto-char (marker-position m1))
4904 (just-one-space)
4905 (indent-to ind))
4906 (progn
4907 (just-one-space)
4908 (indent-to ind)))))
4909 ((verilog-continued-line-1 start)
4910 (goto-char e)
4911 (indent-line-to ind))
4912 (t ; Must be comment or white space
4913 (goto-char e)
4914 (verilog-forward-ws&directives)
4915 (forward-line -1)))
4916 (forward-line 1))
4917 (unless quiet (message ""))))))
4919 (defun verilog-pretty-expr (&optional quiet myre)
4920 "Line up expressions around point, or optional regexp MYRE."
4921 (interactive "sRegular Expression: ((<|:)?=) ")
4922 (save-excursion
4923 (if (or (eq myre nil)
4924 (string-equal myre ""))
4925 (setq myre "\\(<\\|:\\)?="))
4926 (setq myre (concat "\\(^[^;#<=>]*\\)\\(" myre "\\)"))
4927 (let ((rexp(concat "^\\s-*" verilog-complete-reg)))
4928 (beginning-of-line)
4929 (if (and (not (looking-at rexp ))
4930 (looking-at myre))
4931 (let* ((here (point))
4932 (e) (r)
4933 (start
4934 (progn
4935 (beginning-of-line)
4936 (setq e (point))
4937 (verilog-backward-syntactic-ws)
4938 (beginning-of-line)
4939 (while (and (not (looking-at rexp ))
4940 (looking-at myre)
4941 (not (bobp))
4943 (setq e (point))
4944 (verilog-backward-syntactic-ws)
4945 (beginning-of-line)
4946 ) ;Ack, need to grok `define
4948 (end
4949 (progn
4950 (goto-char here)
4951 (end-of-line)
4952 (setq e (point)) ;Might be on last line
4953 (verilog-forward-syntactic-ws)
4954 (beginning-of-line)
4955 (while (and (not (looking-at rexp ))
4956 (looking-at myre))
4957 (end-of-line)
4958 (setq e (point))
4959 (verilog-forward-syntactic-ws)
4960 (beginning-of-line)
4963 (edpos (set-marker (make-marker) end))
4964 (ind)
4966 (goto-char start)
4967 (verilog-do-indent (verilog-calculate-indent))
4968 (if (and (not quiet)
4969 (> (- end start) 100))
4970 (message "Lining up expressions..(please stand by)"))
4972 ;; Set indent to minimum throughout region
4973 (while (< (point) (marker-position edpos))
4974 (beginning-of-line)
4975 (verilog-just-one-space myre)
4976 (end-of-line)
4977 (verilog-forward-syntactic-ws)
4980 ;; Now find biggest prefix
4981 (setq ind (verilog-get-lineup-indent-2 myre start edpos))
4983 ;; Now indent each line.
4984 (goto-char start)
4985 (while (progn (setq e (marker-position edpos))
4986 (setq r (- e (point)))
4987 (> r 0))
4988 (setq e (point))
4989 (if (not quiet) (message "%d" r))
4990 (cond
4991 ((looking-at myre)
4992 (goto-char (match-end 1))
4993 (if (not (verilog-parenthesis-depth)) ;; ignore parenthsized exprs
4994 (if (eq (char-after) ?=)
4995 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
4996 (indent-to ind)
4998 ((verilog-continued-line-1 start)
4999 (goto-char e)
5000 (indent-line-to ind))
5001 (t ; Must be comment or white space
5002 (goto-char e)
5003 (verilog-forward-ws&directives)
5004 (forward-line -1))
5006 (forward-line 1))
5007 (unless quiet (message ""))
5008 )))))
5010 (defun verilog-just-one-space (myre)
5011 "Remove extra spaces around regular expression MYRE."
5012 (interactive)
5013 (if (and (not(looking-at verilog-complete-reg))
5014 (looking-at myre))
5015 (let ((p1 (match-end 1))
5016 (p2 (match-end 2)))
5017 (progn
5018 (goto-char p2)
5019 (if (looking-at "\\s-") (just-one-space))
5020 (goto-char p1)
5021 (forward-char -1)
5022 (if (looking-at "\\s-") (just-one-space))
5023 ))))
5025 (defun verilog-indent-declaration (baseind)
5026 "Indent current lines as declaration.
5027 Line up the variable names based on previous declaration's indentation.
5028 BASEIND is the base indent to offset everything."
5029 (interactive)
5030 (let ((pos (point-marker))
5031 (lim (save-excursion
5032 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
5033 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
5034 (point)))
5035 (ind)
5036 (val)
5037 (m1 (make-marker)))
5038 (setq val
5039 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
5040 (indent-line-to val)
5042 ;; Use previous declaration (in this module) as template.
5043 (if (or (memq 'all verilog-auto-lineup)
5044 (memq 'declaration verilog-auto-lineup))
5045 (if (verilog-re-search-backward
5046 (or (and verilog-indent-declaration-macros
5047 verilog-declaration-re-1-macro)
5048 verilog-declaration-re-1-no-macro) lim t)
5049 (progn
5050 (goto-char (match-end 0))
5051 (skip-chars-forward " \t")
5052 (setq ind (current-column))
5053 (goto-char pos)
5054 (setq val
5055 (+ baseind
5056 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
5057 (indent-line-to val)
5058 (if (and verilog-indent-declaration-macros
5059 (looking-at verilog-declaration-re-2-macro))
5060 (let ((p (match-end 0)))
5061 (set-marker m1 p)
5062 (if (verilog-re-search-forward "[[#`]" p 'move)
5063 (progn
5064 (forward-char -1)
5065 (just-one-space)
5066 (goto-char (marker-position m1))
5067 (just-one-space)
5068 (indent-to ind))
5069 (if (/= (current-column) ind)
5070 (progn
5071 (just-one-space)
5072 (indent-to ind)))))
5073 (if (looking-at verilog-declaration-re-2-no-macro)
5074 (let ((p (match-end 0)))
5075 (set-marker m1 p)
5076 (if (verilog-re-search-forward "[[`#]" p 'move)
5077 (progn
5078 (forward-char -1)
5079 (just-one-space)
5080 (goto-char (marker-position m1))
5081 (just-one-space)
5082 (indent-to ind))
5083 (if (/= (current-column) ind)
5084 (progn
5085 (just-one-space)
5086 (indent-to ind))))))))))
5087 (goto-char pos)))
5089 (defun verilog-get-lineup-indent (b edpos)
5090 "Return the indent level that will line up several lines within the region.
5091 Region is defined by B and EDPOS."
5092 (save-excursion
5093 (let ((ind 0) e)
5094 (goto-char b)
5095 ;; Get rightmost position
5096 (while (progn (setq e (marker-position edpos))
5097 (< (point) e))
5098 (if (verilog-re-search-forward
5099 (or (and verilog-indent-declaration-macros
5100 verilog-declaration-re-1-macro)
5101 verilog-declaration-re-1-no-macro) e 'move)
5102 (progn
5103 (goto-char (match-end 0))
5104 (verilog-backward-syntactic-ws)
5105 (if (> (current-column) ind)
5106 (setq ind (current-column)))
5107 (goto-char (match-end 0)))))
5108 (if (> ind 0)
5109 (1+ ind)
5110 ;; No lineup-string found
5111 (goto-char b)
5112 (end-of-line)
5113 (skip-chars-backward " \t")
5114 (1+ (current-column))))))
5116 (defun verilog-get-lineup-indent-2 (myre b edpos)
5117 "Return the indent level that will line up several lines within the region."
5118 (save-excursion
5119 (let ((ind 0) e)
5120 (goto-char b)
5121 ;; Get rightmost position
5122 (while (progn (setq e (marker-position edpos))
5123 (< (point) e))
5124 (if (and (verilog-re-search-forward myre e 'move)
5125 (not (verilog-parenthesis-depth))) ;; skip parenthsized exprs
5126 (progn
5127 (goto-char (match-beginning 2))
5128 (verilog-backward-syntactic-ws)
5129 (if (> (current-column) ind)
5130 (setq ind (current-column)))
5131 (goto-char (match-end 0)))
5133 (if (> ind 0)
5134 (1+ ind)
5135 ;; No lineup-string found
5136 (goto-char b)
5137 (end-of-line)
5138 (skip-chars-backward " \t")
5139 (1+ (current-column))))))
5141 (defun verilog-comment-depth (type val)
5142 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
5143 (save-excursion
5144 (let
5145 ((b (prog2
5146 (beginning-of-line)
5147 (point-marker)
5148 (end-of-line)))
5149 (e (point-marker)))
5150 (if (re-search-backward " /\\* \[#-\]# \[a-zA-Z\]+ \[0-9\]+ ## \\*/" b t)
5151 (progn
5152 (replace-match " /* -# ## */")
5153 (end-of-line))
5154 (progn
5155 (end-of-line)
5156 (insert " /* ## ## */"))))
5157 (backward-char 6)
5158 (insert
5159 (format "%s %d" type val))))
5161 ;; \f
5163 ;; Completion
5165 (defvar verilog-str nil)
5166 (defvar verilog-all nil)
5167 (defvar verilog-pred nil)
5168 (defvar verilog-buffer-to-use nil)
5169 (defvar verilog-flag nil)
5170 (defvar verilog-toggle-completions nil
5171 "*True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
5172 Repeated use of \\[verilog-complete-word] will show you all of them.
5173 Normally, when there is more than one possible completion,
5174 it displays a list of all possible completions.")
5177 (defvar verilog-type-keywords
5179 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
5180 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
5181 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pullup"
5182 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
5183 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
5184 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
5186 "*Keywords for types used when completing a word in a declaration or parmlist.
5187 \(Eg. integer, real, reg...)")
5189 (defvar verilog-cpp-keywords
5190 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
5191 "endif")
5192 "*Keywords to complete when at first word of a line in declarative scope.
5193 \(Eg. initial, always, begin, assign.)
5194 The procedures and variables defined within the Verilog program
5195 will be completed at runtime and should not be added to this list.")
5197 (defvar verilog-defun-keywords
5198 (append
5200 "always" "always_comb" "always_ff" "always_latch" "assign"
5201 "begin" "end" "generate" "endgenerate" "module" "endmodule"
5202 "specify" "endspecify" "function" "endfunction" "initial" "final"
5203 "task" "endtask" "primitive" "endprimitive"
5205 verilog-type-keywords)
5206 "*Keywords to complete when at first word of a line in declarative scope.
5207 \(Eg. initial, always, begin, assign.)
5208 The procedures and variables defined within the Verilog program
5209 will be completed at runtime and should not be added to this list.")
5211 (defvar verilog-block-keywords
5213 "begin" "break" "case" "continue" "else" "end" "endfunction"
5214 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
5215 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
5216 "while")
5217 "*Keywords to complete when at first word of a line in behavioral scope.
5218 \(Eg. begin, if, then, else, for, fork.)
5219 The procedures and variables defined within the Verilog program
5220 will be completed at runtime and should not be added to this list.")
5222 (defvar verilog-tf-keywords
5223 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
5224 "*Keywords to complete when at first word of a line in a task or function.
5225 \(Eg. begin, if, then, else, for, fork.)
5226 The procedures and variables defined within the Verilog program
5227 will be completed at runtime and should not be added to this list.")
5229 (defvar verilog-case-keywords
5230 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
5231 "*Keywords to complete when at first word of a line in case scope.
5232 \(Eg. begin, if, then, else, for, fork.)
5233 The procedures and variables defined within the Verilog program
5234 will be completed at runtime and should not be added to this list.")
5236 (defvar verilog-separator-keywords
5237 '("else" "then" "begin")
5238 "*Keywords to complete when NOT standing at the first word of a statement.
5239 \(Eg. else, then.)
5240 Variables and function names defined within the Verilog program
5241 will be completed at runtime and should not be added to this list.")
5243 (defun verilog-string-diff (str1 str2)
5244 "Return index of first letter where STR1 and STR2 differs."
5245 (catch 'done
5246 (let ((diff 0))
5247 (while t
5248 (if (or (> (1+ diff) (length str1))
5249 (> (1+ diff) (length str2)))
5250 (throw 'done diff))
5251 (or (equal (aref str1 diff) (aref str2 diff))
5252 (throw 'done diff))
5253 (setq diff (1+ diff))))))
5255 ;; Calculate all possible completions for functions if argument is `function',
5256 ;; completions for procedures if argument is `procedure' or both functions and
5257 ;; procedures otherwise.
5259 (defun verilog-func-completion (type)
5260 "Build regular expression for module/task/function names.
5261 TYPE is 'module, 'tf for task or function, or t if unknown."
5262 (if (string= verilog-str "")
5263 (setq verilog-str "[a-zA-Z_]"))
5264 (let ((verilog-str (concat (cond
5265 ((eq type 'module) "\\<\\(module\\)\\s +")
5266 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
5267 (t "\\<\\(task\\|function\\|module\\)\\s +"))
5268 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
5269 match)
5271 (if (not (looking-at verilog-defun-re))
5272 (verilog-re-search-backward verilog-defun-re nil t))
5273 (forward-char 1)
5275 ;; Search through all reachable functions
5276 (goto-char (point-min))
5277 (while (verilog-re-search-forward verilog-str (point-max) t)
5278 (progn (setq match (buffer-substring (match-beginning 2)
5279 (match-end 2)))
5280 (if (or (null verilog-pred)
5281 (funcall verilog-pred match))
5282 (setq verilog-all (cons match verilog-all)))))
5283 (if (match-beginning 0)
5284 (goto-char (match-beginning 0)))))
5286 (defun verilog-get-completion-decl (end)
5287 "Macro for searching through current declaration (var, type or const)
5288 for matches of `str' and adding the occurrence tp `all' through point END."
5289 (let ((re (or (and verilog-indent-declaration-macros
5290 verilog-declaration-re-2-macro)
5291 verilog-declaration-re-2-no-macro))
5292 decl-end match)
5293 ;; Traverse lines
5294 (while (and (< (point) end)
5295 (verilog-re-search-forward re end t))
5296 ;; Traverse current line
5297 (setq decl-end (save-excursion (verilog-declaration-end)))
5298 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
5299 (not (match-end 1)))
5300 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
5301 (if (string-match (concat "\\<" verilog-str) match)
5302 (if (or (null verilog-pred)
5303 (funcall verilog-pred match))
5304 (setq verilog-all (cons match verilog-all)))))
5305 (forward-line 1)))
5306 verilog-all)
5308 (defun verilog-type-completion ()
5309 "Calculate all possible completions for types."
5310 (let ((start (point))
5311 goon)
5312 ;; Search for all reachable type declarations
5313 (while (or (verilog-beg-of-defun)
5314 (setq goon (not goon)))
5315 (save-excursion
5316 (if (and (< start (prog1 (save-excursion (verilog-end-of-defun)
5317 (point))
5318 (forward-char 1)))
5319 (verilog-re-search-forward
5320 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
5321 start t)
5322 (not (match-end 1)))
5323 ;; Check current type declaration
5324 (verilog-get-completion-decl start))))))
5326 (defun verilog-var-completion ()
5327 "Calculate all possible completions for variables (or constants)."
5328 (let ((start (point)))
5329 ;; Search for all reachable var declarations
5330 (verilog-beg-of-defun)
5331 (save-excursion
5332 ;; Check var declarations
5333 (verilog-get-completion-decl start))))
5335 (defun verilog-keyword-completion (keyword-list)
5336 "Give list of all possible completions of keywords in KEYWORD-LIST."
5337 (mapcar '(lambda (s)
5338 (if (string-match (concat "\\<" verilog-str) s)
5339 (if (or (null verilog-pred)
5340 (funcall verilog-pred s))
5341 (setq verilog-all (cons s verilog-all)))))
5342 keyword-list))
5345 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
5346 "Function passed to `completing-read', `try-completion' or `all-completions'.
5347 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
5348 must be a function to be called for every match to check if this should
5349 really be a match. If VERILOG-FLAG is t, the function returns a list of
5350 all possible completions. If VERILOG-FLAG is nil it returns a string,
5351 the longest possible completion, or t if VERILOG-STR is an exact match.
5352 If VERILOG-FLAG is 'lambda, the function returns t if VERILOG-STR is an
5353 exact match, nil otherwise."
5354 (save-excursion
5355 (let ((verilog-all nil))
5356 ;; Set buffer to use for searching labels. This should be set
5357 ;; within functions which use verilog-completions
5358 (set-buffer verilog-buffer-to-use)
5360 ;; Determine what should be completed
5361 (let ((state (car (verilog-calculate-indent))))
5362 (cond ((eq state 'defun)
5363 (save-excursion (verilog-var-completion))
5364 (verilog-func-completion 'module)
5365 (verilog-keyword-completion verilog-defun-keywords))
5367 ((eq state 'behavioral)
5368 (save-excursion (verilog-var-completion))
5369 (verilog-func-completion 'module)
5370 (verilog-keyword-completion verilog-defun-keywords))
5372 ((eq state 'block)
5373 (save-excursion (verilog-var-completion))
5374 (verilog-func-completion 'tf)
5375 (verilog-keyword-completion verilog-block-keywords))
5377 ((eq state 'case)
5378 (save-excursion (verilog-var-completion))
5379 (verilog-func-completion 'tf)
5380 (verilog-keyword-completion verilog-case-keywords))
5382 ((eq state 'tf)
5383 (save-excursion (verilog-var-completion))
5384 (verilog-func-completion 'tf)
5385 (verilog-keyword-completion verilog-tf-keywords))
5387 ((eq state 'cpp)
5388 (save-excursion (verilog-var-completion))
5389 (verilog-keyword-completion verilog-cpp-keywords))
5391 ((eq state 'cparenexp)
5392 (save-excursion (verilog-var-completion)))
5394 (t;--Anywhere else
5395 (save-excursion (verilog-var-completion))
5396 (verilog-func-completion 'both)
5397 (verilog-keyword-completion verilog-separator-keywords))))
5399 ;; Now we have built a list of all matches. Give response to caller
5400 (verilog-completion-response))))
5402 (defun verilog-completion-response ()
5403 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
5404 ;; This was not called by all-completions
5405 (if (null verilog-all)
5406 ;; Return nil if there was no matching label
5408 ;; Get longest string common in the labels
5409 (let* ((elm (cdr verilog-all))
5410 (match (car verilog-all))
5411 (min (length match))
5412 tmp)
5413 (if (string= match verilog-str)
5414 ;; Return t if first match was an exact match
5415 (setq match t)
5416 (while (not (null elm))
5417 ;; Find longest common string
5418 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
5419 (progn
5420 (setq min tmp)
5421 (setq match (substring match 0 min))))
5422 ;; Terminate with match=t if this is an exact match
5423 (if (string= (car elm) verilog-str)
5424 (progn
5425 (setq match t)
5426 (setq elm nil))
5427 (setq elm (cdr elm)))))
5428 ;; If this is a test just for exact match, return nil ot t
5429 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
5431 match))))
5432 ;; If flag is t, this was called by all-completions. Return
5433 ;; list of all possible completions
5434 (verilog-flag
5435 verilog-all)))
5437 (defvar verilog-last-word-numb 0)
5438 (defvar verilog-last-word-shown nil)
5439 (defvar verilog-last-completions nil)
5441 (defun verilog-complete-word ()
5442 "Complete word at current point.
5443 \(See also `verilog-toggle-completions', `verilog-type-keywords',
5444 and `verilog-separator-keywords'.)"
5445 (interactive)
5446 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
5447 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
5448 (verilog-str (buffer-substring b e))
5449 ;; The following variable is used in verilog-completion
5450 (verilog-buffer-to-use (current-buffer))
5451 (allcomp (if (and verilog-toggle-completions
5452 (string= verilog-last-word-shown verilog-str))
5453 verilog-last-completions
5454 (all-completions verilog-str 'verilog-completion)))
5455 (match (if verilog-toggle-completions
5456 "" (try-completion
5457 verilog-str (mapcar '(lambda (elm)
5458 (cons elm 0)) allcomp)))))
5459 ;; Delete old string
5460 (delete-region b e)
5462 ;; Toggle-completions inserts whole labels
5463 (if verilog-toggle-completions
5464 (progn
5465 ;; Update entry number in list
5466 (setq verilog-last-completions allcomp
5467 verilog-last-word-numb
5468 (if (>= verilog-last-word-numb (1- (length allcomp)))
5470 (1+ verilog-last-word-numb)))
5471 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
5472 ;; Display next match or same string if no match was found
5473 (if (not (null allcomp))
5474 (insert "" verilog-last-word-shown)
5475 (insert "" verilog-str)
5476 (message "(No match)")))
5477 ;; The other form of completion does not necessarily do that.
5479 ;; Insert match if found, or the original string if no match
5480 (if (or (null match) (equal match 't))
5481 (progn (insert "" verilog-str)
5482 (message "(No match)"))
5483 (insert "" match))
5484 ;; Give message about current status of completion
5485 (cond ((equal match 't)
5486 (if (not (null (cdr allcomp)))
5487 (message "(Complete but not unique)")
5488 (message "(Sole completion)")))
5489 ;; Display buffer if the current completion didn't help
5490 ;; on completing the label.
5491 ((and (not (null (cdr allcomp))) (= (length verilog-str)
5492 (length match)))
5493 (with-output-to-temp-buffer "*Completions*"
5494 (display-completion-list allcomp))
5495 ;; Wait for a key press. Then delete *Completion* window
5496 (momentary-string-display "" (point))
5497 (delete-window (get-buffer-window (get-buffer "*Completions*")))
5498 )))))
5500 (defun verilog-show-completions ()
5501 "Show all possible completions at current point."
5502 (interactive)
5503 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
5504 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
5505 (verilog-str (buffer-substring b e))
5506 ;; The following variable is used in verilog-completion
5507 (verilog-buffer-to-use (current-buffer))
5508 (allcomp (if (and verilog-toggle-completions
5509 (string= verilog-last-word-shown verilog-str))
5510 verilog-last-completions
5511 (all-completions verilog-str 'verilog-completion))))
5512 ;; Show possible completions in a temporary buffer.
5513 (with-output-to-temp-buffer "*Completions*"
5514 (display-completion-list allcomp))
5515 ;; Wait for a key press. Then delete *Completion* window
5516 (momentary-string-display "" (point))
5517 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
5520 (defun verilog-get-default-symbol ()
5521 "Return symbol around current point as a string."
5522 (save-excursion
5523 (buffer-substring (progn
5524 (skip-chars-backward " \t")
5525 (skip-chars-backward "a-zA-Z0-9_")
5526 (point))
5527 (progn
5528 (skip-chars-forward "a-zA-Z0-9_")
5529 (point)))))
5531 (defun verilog-build-defun-re (str &optional arg)
5532 "Return function/task/module starting with STR as regular expression.
5533 With optional second ARG non-nil, STR is the complete name of the instruction."
5534 (if arg
5535 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
5536 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
5538 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
5539 "Function passed to `completing-read', `try-completion' or `all-completions'.
5540 Returns a completion on any function name based on VERILOG-STR prefix. If
5541 VERILOG-PRED is non-nil, it must be a function to be called for every match
5542 to check if this should really be a match. If VERILOG-FLAG is t, the
5543 function returns a list of all possible completions. If it is nil it
5544 returns a string, the longest possible completion, or t if VERILOG-STR is
5545 an exact match. If VERILOG-FLAG is 'lambda, the function returns t if
5546 VERILOG-STR is an exact match, nil otherwise."
5547 (save-excursion
5548 (let ((verilog-all nil)
5549 match)
5551 ;; Set buffer to use for searching labels. This should be set
5552 ;; within functions which use verilog-completions
5553 (set-buffer verilog-buffer-to-use)
5555 (let ((verilog-str verilog-str))
5556 ;; Build regular expression for functions
5557 (if (string= verilog-str "")
5558 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
5559 (setq verilog-str (verilog-build-defun-re verilog-str)))
5560 (goto-char (point-min))
5562 ;; Build a list of all possible completions
5563 (while (verilog-re-search-forward verilog-str nil t)
5564 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
5565 (if (or (null verilog-pred)
5566 (funcall verilog-pred match))
5567 (setq verilog-all (cons match verilog-all)))))
5569 ;; Now we have built a list of all matches. Give response to caller
5570 (verilog-completion-response))))
5572 (defun verilog-goto-defun ()
5573 "Move to specified Verilog module/task/function.
5574 The default is a name found in the buffer around point.
5575 If search fails, other files are checked based on
5576 `verilog-library-flags'."
5577 (interactive)
5578 (let* ((default (verilog-get-default-symbol))
5579 ;; The following variable is used in verilog-comp-function
5580 (verilog-buffer-to-use (current-buffer))
5581 (label (if (not (string= default ""))
5582 ;; Do completion with default
5583 (completing-read (concat "Label: (default " default ") ")
5584 'verilog-comp-defun nil nil "")
5585 ;; There is no default value. Complete without it
5586 (completing-read "Label: "
5587 'verilog-comp-defun nil nil "")))
5589 ;; If there was no response on prompt, use default value
5590 (if (string= label "")
5591 (setq label default))
5592 ;; Goto right place in buffer if label is not an empty string
5593 (or (string= label "")
5594 (progn
5595 (save-excursion
5596 (goto-char (point-min))
5597 (setq pt
5598 (re-search-forward (verilog-build-defun-re label t) nil t)))
5599 (when pt
5600 (goto-char pt)
5601 (beginning-of-line))
5603 (verilog-goto-defun-file label))))
5605 ;; Eliminate compile warning
5606 (defvar occur-pos-list)
5608 (defun verilog-showscopes ()
5609 "List all scopes in this module."
5610 (interactive)
5611 (let ((buffer (current-buffer))
5612 (linenum 1)
5613 (nlines 0)
5614 (first 1)
5615 (prevpos (point-min))
5616 (final-context-start (make-marker))
5617 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
5618 (with-output-to-temp-buffer "*Occur*"
5619 (save-excursion
5620 (message (format "Searching for %s ..." regexp))
5621 ;; Find next match, but give up if prev match was at end of buffer.
5622 (while (and (not (= prevpos (point-max)))
5623 (verilog-re-search-forward regexp nil t))
5624 (goto-char (match-beginning 0))
5625 (beginning-of-line)
5626 (save-match-data
5627 (setq linenum (+ linenum (count-lines prevpos (point)))))
5628 (setq prevpos (point))
5629 (goto-char (match-end 0))
5630 (let* ((start (save-excursion
5631 (goto-char (match-beginning 0))
5632 (forward-line (if (< nlines 0) nlines (- nlines)))
5633 (point)))
5634 (end (save-excursion
5635 (goto-char (match-end 0))
5636 (if (> nlines 0)
5637 (forward-line (1+ nlines))
5638 (forward-line 1))
5639 (point)))
5640 (tag (format "%3d" linenum))
5641 (empty (make-string (length tag) ?\ ))
5642 tem)
5643 (save-excursion
5644 (setq tem (make-marker))
5645 (set-marker tem (point))
5646 (set-buffer standard-output)
5647 (setq occur-pos-list (cons tem occur-pos-list))
5648 (or first (zerop nlines)
5649 (insert "--------\n"))
5650 (setq first nil)
5651 (insert-buffer-substring buffer start end)
5652 (backward-char (- end start))
5653 (setq tem (if (< nlines 0) (- nlines) nlines))
5654 (while (> tem 0)
5655 (insert empty ?:)
5656 (forward-line 1)
5657 (setq tem (1- tem)))
5658 (let ((this-linenum linenum))
5659 (set-marker final-context-start
5660 (+ (point) (- (match-end 0) (match-beginning 0))))
5661 (while (< (point) final-context-start)
5662 (if (null tag)
5663 (setq tag (format "%3d" this-linenum)))
5664 (insert tag ?:)))))))
5665 (set-buffer-modified-p nil))))
5668 ;; Highlight helper functions
5669 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
5670 (defun verilog-within-translate-off ()
5671 "Return point if within translate-off region, else nil."
5672 (and (save-excursion
5673 (re-search-backward
5674 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
5675 nil t))
5676 (equal "off" (match-string 2))
5677 (point)))
5679 (defun verilog-start-translate-off (limit)
5680 "Return point before translate-off directive if before LIMIT, else nil."
5681 (when (re-search-forward
5682 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
5683 limit t)
5684 (match-beginning 0)))
5686 (defun verilog-back-to-start-translate-off (limit)
5687 "Return point before translate-off directive if before LIMIT, else nil."
5688 (when (re-search-backward
5689 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
5690 limit t)
5691 (match-beginning 0)))
5693 (defun verilog-end-translate-off (limit)
5694 "Return point after translate-on directive if before LIMIT, else nil."
5696 (re-search-forward (concat
5697 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
5699 (defun verilog-match-translate-off (limit)
5700 "Match a translate-off block, setting `match-data' and returning t, else nil.
5701 Bound search by LIMIT."
5702 (when (< (point) limit)
5703 (let ((start (or (verilog-within-translate-off)
5704 (verilog-start-translate-off limit)))
5705 (case-fold-search t))
5706 (when start
5707 (let ((end (or (verilog-end-translate-off limit) limit)))
5708 (set-match-data (list start end))
5709 (goto-char end))))))
5711 (defun verilog-font-lock-match-item (limit)
5712 "Match, and move over, any declaration item after point.
5713 Bound search by LIMIT. Adapted from
5714 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
5715 (condition-case nil
5716 (save-restriction
5717 (narrow-to-region (point-min) limit)
5718 ;; match item
5719 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
5720 (save-match-data
5721 (goto-char (match-end 1))
5722 ;; move to next item
5723 (if (looking-at "\\(\\s-*,\\)")
5724 (goto-char (match-end 1))
5725 (end-of-line) t))))
5726 (error nil)))
5729 ;; Added by Subbu Meiyappan for Header
5731 (defun verilog-header ()
5732 "Insert a standard Verilog file header."
5733 (interactive)
5734 (let ((start (point)))
5735 (insert "\
5736 //-----------------------------------------------------------------------------
5737 // Title : <title>
5738 // Project : <project>
5739 //-----------------------------------------------------------------------------
5740 // File : <filename>
5741 // Author : <author>
5742 // Created : <credate>
5743 // Last modified : <moddate>
5744 //-----------------------------------------------------------------------------
5745 // Description :
5746 // <description>
5747 //-----------------------------------------------------------------------------
5748 // Copyright (c) <copydate> by <company> This model is the confidential and
5749 // proprietary property of <company> and the possession or use of this
5750 // file requires a written license from <company>.
5751 //------------------------------------------------------------------------------
5752 // Modification history :
5753 // <modhist>
5754 //-----------------------------------------------------------------------------
5757 (goto-char start)
5758 (search-forward "<filename>")
5759 (replace-match (buffer-name) t t)
5760 (search-forward "<author>") (replace-match "" t t)
5761 (insert (user-full-name))
5762 (insert " <" (user-login-name) "@" (system-name) ">")
5763 (search-forward "<credate>") (replace-match "" t t)
5764 (verilog-insert-date)
5765 (search-forward "<moddate>") (replace-match "" t t)
5766 (verilog-insert-date)
5767 (search-forward "<copydate>") (replace-match "" t t)
5768 (verilog-insert-year)
5769 (search-forward "<modhist>") (replace-match "" t t)
5770 (verilog-insert-date)
5771 (insert " : created")
5772 (goto-char start)
5773 (let (string)
5774 (setq string (read-string "title: "))
5775 (search-forward "<title>")
5776 (replace-match string t t)
5777 (setq string (read-string "project: " verilog-project))
5778 (setq verilog-project string)
5779 (search-forward "<project>")
5780 (replace-match string t t)
5781 (setq string (read-string "Company: " verilog-company))
5782 (setq verilog-company string)
5783 (search-forward "<company>")
5784 (replace-match string t t)
5785 (search-forward "<company>")
5786 (replace-match string t t)
5787 (search-forward "<company>")
5788 (replace-match string t t)
5789 (search-backward "<description>")
5790 (replace-match "" t t))))
5792 ;; verilog-header Uses the verilog-insert-date function
5794 (defun verilog-insert-date ()
5795 "Insert date from the system."
5796 (interactive)
5797 (let ((timpos))
5798 (setq timpos (point))
5799 (if verilog-date-scientific-format
5800 (shell-command "date \"+@%Y/%m/%d\"" t)
5801 (shell-command "date \"+@%d.%m.%Y\"" t))
5802 (search-forward "@")
5803 (delete-region timpos (point))
5804 (end-of-line))
5805 (delete-char 1))
5807 (defun verilog-insert-year ()
5808 "Insert year from the system."
5809 (interactive)
5810 (let ((timpos))
5811 (setq timpos (point))
5812 (shell-command "date \"+@%Y\"" t)
5813 (search-forward "@")
5814 (delete-region timpos (point))
5815 (end-of-line))
5816 (delete-char 1))
5820 ;; Signal list parsing
5823 ;; Elements of a signal list
5824 (defsubst verilog-sig-name (sig)
5825 (car sig))
5826 (defsubst verilog-sig-bits (sig)
5827 (nth 1 sig))
5828 (defsubst verilog-sig-comment (sig)
5829 (nth 2 sig))
5830 (defsubst verilog-sig-memory (sig)
5831 (nth 3 sig))
5832 (defsubst verilog-sig-enum (sig)
5833 (nth 4 sig))
5834 (defsubst verilog-sig-signed (sig)
5835 (nth 5 sig))
5836 (defsubst verilog-sig-type (sig)
5837 (nth 6 sig))
5838 (defsubst verilog-sig-multidim (sig)
5839 (nth 7 sig))
5840 (defsubst verilog-sig-multidim-string (sig)
5841 (if (verilog-sig-multidim sig)
5842 (let ((str "") (args (verilog-sig-multidim sig)))
5843 (while args
5844 (setq str (concat str (car args)))
5845 (setq args (cdr args)))
5846 str)))
5847 (defsubst verilog-sig-width (sig)
5848 (verilog-make-width-expression (verilog-sig-bits sig)))
5850 (defsubst verilog-alw-get-inputs (sigs)
5851 (nth 2 sigs))
5852 (defsubst verilog-alw-get-outputs (sigs)
5853 (nth 0 sigs))
5854 (defsubst verilog-alw-get-uses-delayed (sigs)
5855 (nth 3 sigs))
5857 (defun verilog-signals-not-in (in-list not-list)
5858 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
5859 Also remove any duplicates in IN-LIST.
5860 Signals must be in standard (base vector) form."
5861 (let (out-list)
5862 (while in-list
5863 (if (not (or (assoc (car (car in-list)) not-list)
5864 (assoc (car (car in-list)) out-list)))
5865 (setq out-list (cons (car in-list) out-list)))
5866 (setq in-list (cdr in-list)))
5867 (nreverse out-list)))
5868 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
5870 (defun verilog-signals-in (in-list other-list)
5871 "Return list of signals in IN-LIST that are also in OTHER-LIST.
5872 Signals must be in standard (base vector) form."
5873 (let (out-list)
5874 (while in-list
5875 (if (assoc (car (car in-list)) other-list)
5876 (setq out-list (cons (car in-list) out-list)))
5877 (setq in-list (cdr in-list)))
5878 (nreverse out-list)))
5879 ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
5881 (defun verilog-signals-memory (in-list)
5882 "Return list of signals in IN-LIST that are memoried (multidimensional)."
5883 (let (out-list)
5884 (while in-list
5885 (if (nth 3 (car in-list))
5886 (setq out-list (cons (car in-list) out-list)))
5887 (setq in-list (cdr in-list)))
5888 out-list))
5889 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
5891 (defun verilog-signals-sort-compare (a b)
5892 "Compare signal A and B for sorting."
5893 (string< (car a) (car b)))
5895 (defun verilog-signals-not-params (in-list)
5896 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
5897 (let (out-list)
5898 (while in-list
5899 (unless (boundp (intern (concat "vh-" (car (car in-list)))))
5900 (setq out-list (cons (car in-list) out-list)))
5901 (setq in-list (cdr in-list)))
5902 (nreverse out-list)))
5904 (defun verilog-signals-combine-bus (in-list)
5905 "Return a list of signals in IN-LIST, with busses combined.
5906 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
5907 (let (combo buswarn
5908 out-list
5909 sig highbit lowbit ; Temp information about current signal
5910 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
5911 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
5912 bus)
5913 ;; Shove signals so duplicated signals will be adjacent
5914 (setq in-list (sort in-list `verilog-signals-sort-compare))
5915 (while in-list
5916 (setq sig (car in-list))
5917 ;; No current signal; form from existing details
5918 (unless sv-name
5919 (setq sv-name (verilog-sig-name sig)
5920 sv-highbit nil
5921 sv-busstring nil
5922 sv-comment (verilog-sig-comment sig)
5923 sv-memory (verilog-sig-memory sig)
5924 sv-enum (verilog-sig-enum sig)
5925 sv-signed (verilog-sig-signed sig)
5926 sv-type (verilog-sig-type sig)
5927 sv-multidim (verilog-sig-multidim sig)
5928 combo ""
5929 buswarn ""))
5930 ;; Extract bus details
5931 (setq bus (verilog-sig-bits sig))
5932 (cond ((and bus
5933 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
5934 (setq highbit (string-to-number (match-string 1 bus))
5935 lowbit (string-to-number
5936 (match-string 2 bus))))
5937 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
5938 (setq highbit (string-to-number (match-string 1 bus))
5939 lowbit highbit))))
5940 ;; Combine bits in bus
5941 (if sv-highbit
5942 (setq sv-highbit (max highbit sv-highbit)
5943 sv-lowbit (min lowbit sv-lowbit))
5944 (setq sv-highbit highbit
5945 sv-lowbit lowbit)))
5946 (bus
5947 ;; String, probably something like `preproc:0
5948 (setq sv-busstring bus)))
5949 ;; Peek ahead to next signal
5950 (setq in-list (cdr in-list))
5951 (setq sig (car in-list))
5952 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
5953 ;; Combine with this signal
5954 (when (and sv-busstring
5955 (not (equal sv-busstring (verilog-sig-bits sig))))
5956 (when nil ;; Debugging
5957 (message (concat "Warning, can't merge into single bus "
5958 sv-name bus
5959 ", the AUTOs may be wrong")))
5960 (setq buswarn ", Couldn't Merge"))
5961 (if (verilog-sig-comment sig) (setq combo ", ..."))
5962 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
5963 sv-enum (or sv-enum (verilog-sig-enum sig))
5964 sv-signed (or sv-signed (verilog-sig-signed sig))
5965 sv-type (or sv-type (verilog-sig-type sig))
5966 sv-multidim (or sv-multidim (verilog-sig-multidim sig))))
5967 ;; Doesn't match next signal, add to queue, zero in prep for next
5968 ;; Note sig may also be nil for the last signal in the list
5970 (setq out-list
5971 (cons
5972 (list sv-name
5973 (or sv-busstring
5974 (if sv-highbit
5975 (concat "[" (int-to-string sv-highbit) ":"
5976 (int-to-string sv-lowbit) "]")))
5977 (concat sv-comment combo buswarn)
5978 sv-memory sv-enum sv-signed sv-type sv-multidim)
5979 out-list)
5980 sv-name nil))))
5982 out-list))
5984 (defun verilog-sig-tieoff (sig &optional no-width)
5985 "Return tieoff expression for given SIG, with appropriate width.
5986 Ignore width if optional NO-WIDTH is set."
5987 (let* ((width (if no-width nil (verilog-sig-width sig))))
5988 (concat
5989 (if (and verilog-active-low-regexp
5990 (string-match verilog-active-low-regexp (verilog-sig-name sig)))
5991 "~" "")
5992 (cond ((not width)
5993 "0")
5994 ((string-match "^[0-9]+$" width)
5995 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
5997 (concat "{" width "{1'b0}}"))))))
6000 ;; Port/Wire/Etc Reading
6003 (defun verilog-read-inst-backward-name ()
6004 "Internal. Move point back to beginning of inst-name."
6005 (verilog-backward-open-paren)
6006 (let (done)
6007 (while (not done)
6008 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil) ; ] isn't word boundary
6009 (cond ((looking-at ")")
6010 (verilog-backward-open-paren))
6011 (t (setq done t)))))
6012 (while (looking-at "\\]")
6013 (verilog-backward-open-bracket)
6014 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil))
6015 (skip-chars-backward "a-zA-Z0-9`_$"))
6017 (defun verilog-read-inst-module ()
6018 "Return module_name when point is inside instantiation."
6019 (save-excursion
6020 (verilog-read-inst-backward-name)
6021 ;; Skip over instantiation name
6022 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
6023 ;; Check for parameterized instantiations
6024 (when (looking-at ")")
6025 (verilog-backward-open-paren)
6026 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
6027 (skip-chars-backward "a-zA-Z0-9'_$")
6028 (looking-at "[a-zA-Z0-9`_\$]+")
6029 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6030 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
6032 (defun verilog-read-inst-name ()
6033 "Return instance_name when point is inside instantiation."
6034 (save-excursion
6035 (verilog-read-inst-backward-name)
6036 (looking-at "[a-zA-Z0-9`_\$]+")
6037 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6038 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
6040 (defun verilog-read-module-name ()
6041 "Return module name when after its ( or ;."
6042 (save-excursion
6043 (re-search-backward "[(;]")
6044 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil)
6045 (skip-chars-backward "a-zA-Z0-9`_$")
6046 (looking-at "[a-zA-Z0-9`_\$]+")
6047 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6048 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
6050 (defun verilog-read-auto-params (num-param &optional max-param)
6051 "Return parameter list inside auto.
6052 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
6053 (let ((olist))
6054 (save-excursion
6055 ;; /*AUTOPUNT("parameter", "parameter")*/
6056 (search-backward "(")
6057 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
6058 (setq olist (cons (match-string 1) olist))
6059 (goto-char (match-end 0))))
6060 (or (eq nil num-param)
6061 (<= num-param (length olist))
6062 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
6063 (if (eq max-param nil) (setq max-param num-param))
6064 (or (eq nil max-param)
6065 (>= max-param (length olist))
6066 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
6067 (nreverse olist)))
6069 (defun verilog-read-decls ()
6070 "Compute signal declaration information for the current module at point.
6071 Return a array of [outputs inouts inputs wire reg assign const]."
6072 (let ((end-mod-point (or (verilog-get-end-of-defun t) (point-max)))
6073 (functask 0) (paren 0) (sig-paren 0)
6074 sigs-in sigs-out sigs-inout sigs-wire sigs-reg sigs-assign sigs-const sigs-gparam
6075 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim)
6076 (save-excursion
6077 (verilog-beg-of-defun)
6078 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
6079 (while (< (point) end-mod-point)
6080 ;;(if dbg (setq dbg (cons (format "Pt %s Vec %s Kwd'%s'\n" (point) vec keywd) dbg)))
6081 (cond
6082 ((looking-at "//")
6083 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
6084 (setq enum (match-string 1)))
6085 (search-forward "\n"))
6086 ((looking-at "/\\*")
6087 (forward-char 2)
6088 (if (looking-at "[^*]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
6089 (setq enum (match-string 1)))
6090 (or (search-forward "*/")
6091 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
6092 ((looking-at "(\\*")
6093 (forward-char 2)
6094 (or (looking-at "\\s-*)") ; It's a "always @ (*)"
6095 (search-forward "*)")
6096 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
6097 ((eq ?\" (following-char))
6098 (or (re-search-forward "[^\\]\"" nil t) ;; don't forward-char first, since we look for a non backslash first
6099 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
6100 ((eq ?\; (following-char))
6101 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil)
6102 (forward-char 1))
6103 ((eq ?= (following-char))
6104 (setq rvalue t newsig nil)
6105 (forward-char 1))
6106 ((and (or rvalue sig-paren)
6107 (cond ((and (eq ?, (following-char))
6108 (eq paren sig-paren))
6109 (setq rvalue nil)
6110 (forward-char 1)
6112 ;; ,'s can occur inside {} & funcs
6113 ((looking-at "[{(]")
6114 (setq paren (1+ paren))
6115 (forward-char 1)
6117 ((looking-at "[})]")
6118 (setq paren (1- paren))
6119 (forward-char 1)
6120 (when (< paren sig-paren)
6121 (setq expect-signal nil)) ; ) that ends variables inside v2k arg list
6122 t))))
6123 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
6124 (goto-char (match-end 0))
6125 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
6126 (setcar (cdr (cdr (cdr newsig))) (match-string 1)))
6127 (vec ;; Multidimensional
6128 (setq multidim (cons vec multidim))
6129 (setq vec (verilog-string-replace-matches
6130 "\\s-+" "" nil nil (match-string 1))))
6131 (t ;; Bit width
6132 (setq vec (verilog-string-replace-matches
6133 "\\s-+" "" nil nil (match-string 1))))))
6134 ;; Normal or escaped identifier -- note we remember the \ if escaped
6135 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
6136 (goto-char (match-end 0))
6137 (setq keywd (match-string 1))
6138 (when (string-match "^\\\\" keywd)
6139 (setq keywd (concat keywd " "))) ;; Escaped ID needs space at end
6140 (cond ((equal keywd "input")
6141 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
6142 expect-signal 'sigs-in io t))
6143 ((equal keywd "output")
6144 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
6145 expect-signal 'sigs-out io t))
6146 ((equal keywd "inout")
6147 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
6148 expect-signal 'sigs-inout io t))
6149 ((or (equal keywd "wire")
6150 (equal keywd "tri")
6151 (equal keywd "tri0")
6152 (equal keywd "tri1"))
6153 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6154 expect-signal 'sigs-wire)))
6155 ((or (equal keywd "reg")
6156 (equal keywd "trireg"))
6157 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6158 expect-signal 'sigs-reg)))
6159 ((equal keywd "assign")
6160 (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6161 expect-signal 'sigs-assign))
6162 ((or (equal keywd "supply0")
6163 (equal keywd "supply1")
6164 (equal keywd "supply")
6165 (equal keywd "localparam")
6166 (equal keywd "genvar"))
6167 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6168 expect-signal 'sigs-const)))
6169 ((or (equal keywd "parameter"))
6170 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6171 expect-signal 'sigs-gparam)))
6172 ((equal keywd "signed")
6173 (setq signed "signed"))
6174 ((or (equal keywd "function")
6175 (equal keywd "task"))
6176 (setq functask (1+ functask)))
6177 ((or (equal keywd "endfunction")
6178 (equal keywd "endtask"))
6179 (setq functask (1- functask)))
6180 ((or (equal keywd "`ifdef")
6181 (equal keywd "`ifndef"))
6182 (setq rvalue t))
6183 ((verilog-typedef-name-p keywd)
6184 (setq typedefed keywd))
6185 ((and expect-signal
6186 (eq functask 0)
6187 (not rvalue)
6188 (eq paren sig-paren)
6189 (not (member keywd verilog-keywords)))
6190 ;; Add new signal to expect-signal's variable
6191 (setq newsig (list keywd vec nil nil enum signed typedefed multidim))
6192 (set expect-signal (cons newsig
6193 (symbol-value expect-signal))))))
6195 (forward-char 1)))
6196 (skip-syntax-forward " "))
6197 ;; Return arguments
6198 (vector (nreverse sigs-out)
6199 (nreverse sigs-inout)
6200 (nreverse sigs-in)
6201 (nreverse sigs-wire)
6202 (nreverse sigs-reg)
6203 (nreverse sigs-assign)
6204 (nreverse sigs-const)
6205 (nreverse sigs-gparam)))))
6207 (eval-when-compile
6208 ;; Prevent compile warnings; these are let's, not globals
6209 ;; Do not remove the eval-when-compile
6210 ;; - we want a error when we are debugging this code if they are refed.
6211 (defvar sigs-in)
6212 (defvar sigs-inout)
6213 (defvar sigs-out))
6216 (defsubst verilog-modi-get-decls (modi)
6217 (verilog-modi-cache-results modi 'verilog-read-decls))
6219 (defsubst verilog-modi-get-sub-decls (modi)
6220 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
6223 ;; Signal reading for given module
6224 ;; Note these all take modi's - as returned from the
6225 ;; verilog-modi-current function.
6226 (defsubst verilog-decls-get-outputs (decls)
6227 (aref decls 0))
6228 (defsubst verilog-decls-get-inouts (decls)
6229 (aref decls 1))
6230 (defsubst verilog-decls-get-inputs (decls)
6231 (aref decls 2))
6232 (defsubst verilog-decls-get-wires (decls)
6233 (aref decls 3))
6234 (defsubst verilog-decls-get-regs (decls)
6235 (aref decls 4))
6236 (defsubst verilog-decls-get-assigns (decls)
6237 (aref decls 5))
6238 (defsubst verilog-decls-get-consts (decls)
6239 (aref decls 6))
6240 (defsubst verilog-decls-get-gparams (decls)
6241 (aref decls 7))
6242 (defsubst verilog-subdecls-get-outputs (subdecls)
6243 (aref subdecls 0))
6244 (defsubst verilog-subdecls-get-inouts (subdecls)
6245 (aref subdecls 1))
6246 (defsubst verilog-subdecls-get-inputs (subdecls)
6247 (aref subdecls 2))
6250 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
6251 "For `verilog-read-sub-decls-line', add a signal."
6252 (let (portdata)
6253 (when sig
6254 (setq port (verilog-symbol-detick-denumber port))
6255 (setq sig (verilog-symbol-detick-denumber sig))
6256 (if sig (setq sig (verilog-string-replace-matches "^[---+~!|&]+" "" nil nil sig)))
6257 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
6258 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
6259 (unless (or (not sig)
6260 (equal sig "")) ;; Ignore .foo(1'b1) assignments
6261 (cond ((setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
6262 (setq sigs-inout (cons (list sig vec (concat "To/From " comment) nil nil
6263 (verilog-sig-signed portdata)
6264 (verilog-sig-type portdata)
6265 multidim)
6266 sigs-inout)))
6267 ((setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
6268 (setq sigs-out (cons (list sig vec (concat "From " comment) nil nil
6269 (verilog-sig-signed portdata)
6270 (verilog-sig-type portdata)
6271 multidim)
6272 sigs-out)))
6273 ((setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
6274 (setq sigs-in (cons (list sig vec (concat "To " comment) nil nil
6275 (verilog-sig-signed portdata)
6276 (verilog-sig-type portdata)
6277 multidim)
6278 sigs-in)))
6279 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
6280 )))))
6282 (defun verilog-read-sub-decls-line (submoddecls comment)
6283 "For `verilog-read-sub-decls', read lines of port defs until none match anymore.
6284 Return the list of signals found, using submodi to look up each port."
6285 (let (done port sig vec multidim)
6286 (save-excursion
6287 (forward-line 1)
6288 (while (not done)
6289 ;; Get port name
6290 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
6291 (setq port (match-string 1))
6292 (goto-char (match-end 0)))
6293 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
6294 (setq port (concat (match-string 1) " ")) ;; escaped id's need trailing space
6295 (goto-char (match-end 0)))
6296 ((looking-at "\\s-*\\.[^(]*(")
6297 (setq port nil) ;; skip this line
6298 (goto-char (match-end 0)))
6300 (setq port nil done t))) ;; Unknown, ignore rest of line
6301 ;; Get signal name
6302 (when port
6303 (setq multidim nil)
6304 (cond ((looking-at "\\(\\\\[^ \t\n\f]*\\)\\s-*)")
6305 (setq sig (concat (match-string 1) " ") ;; escaped id's need trailing space
6306 vec nil))
6307 ; We intentionally ignore (non-escaped) signals with .s in them
6308 ; this prevents AUTOWIRE etc from noticing hierarchical sigs.
6309 ((looking-at "\\([^[({).]*\\)\\s-*)")
6310 (setq sig (verilog-string-remove-spaces (match-string 1))
6311 vec nil))
6312 ((looking-at "\\([^[({).]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
6313 (setq sig (verilog-string-remove-spaces (match-string 1))
6314 vec (match-string 2)))
6315 ((looking-at "\\([^[({).]*\\)\\s-*/\\*\\(\\[[^*]+\\]\\)\\*/\\s-*)")
6316 (setq sig (verilog-string-remove-spaces (match-string 1))
6317 vec nil)
6318 (let ((parse (match-string 2)))
6319 (while (string-match "^\\(\\[[^]]+\\]\\)\\(.*\\)$" parse)
6320 (when vec (setq multidim (cons vec multidim)))
6321 (setq vec (match-string 1 parse))
6322 (setq parse (match-string 2 parse)))))
6323 ((looking-at "{\\(.*\\)}.*\\s-*)")
6324 (let ((mlst (split-string (match-string 1) ","))
6325 mstr)
6326 (while (setq mstr (pop mlst))
6327 ;;(unless noninteractive (message "sig: %s " mstr))
6328 (cond
6329 ((string-match "\\(['`a-zA-Z0-9_$]+\\)\\s-*$" mstr)
6330 (setq sig (verilog-string-remove-spaces (match-string 1 mstr))
6331 vec nil)
6332 ;;(unless noninteractive (message "concat sig1: %s %s" mstr (match-string 1 mstr)))
6334 ((string-match "\\([^[({).]+\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*" mstr)
6335 (setq sig (verilog-string-remove-spaces (match-string 1 mstr))
6336 vec (match-string 2 mstr))
6337 ;;(unless noninteractive (message "concat sig2: '%s' '%s' '%s'" mstr (match-string 1 mstr) (match-string 2 mstr)))
6340 (setq sig nil)))
6341 ;; Process signals
6342 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))
6344 (setq sig nil)))
6345 ;; Process signals
6346 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))
6348 (forward-line 1)))))
6350 (defun verilog-read-sub-decls ()
6351 "Internally parse signals going to modules under this module.
6352 Return a array of [ outputs inouts inputs ] signals for modules that are
6353 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
6354 is a output, then SIG will be included in the list.
6356 This only works on instantiations created with /*AUTOINST*/ converted by
6357 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
6358 component library to determine connectivity of the design.
6360 One work around for this problem is to manually create // Inputs and //
6361 Outputs comments above subcell signals, for example:
6363 module ModuleName (
6364 // Outputs
6365 .out (out),
6366 // Inputs
6367 .in (in));"
6368 (save-excursion
6369 (let ((end-mod-point (verilog-get-end-of-defun t))
6370 st-point end-inst-point
6371 ;; below 3 modified by verilog-read-sub-decls-line
6372 sigs-out sigs-inout sigs-in)
6373 (verilog-beg-of-defun)
6374 (while (verilog-re-search-forward "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
6375 (save-excursion
6376 (goto-char (match-beginning 0))
6377 (unless (verilog-inside-comment-p)
6378 ;; Attempt to snarf a comment
6379 (let* ((submod (verilog-read-inst-module))
6380 (inst (verilog-read-inst-name))
6381 (comment (concat inst " of " submod ".v"))
6382 submodi submoddecls)
6383 (when (setq submodi (verilog-modi-lookup submod t))
6384 (setq submoddecls (verilog-modi-get-decls submodi))
6385 ;; This could have used a list created by verilog-auto-inst
6386 ;; However I want it to be runnable even on user's manually added signals
6387 (verilog-backward-open-paren)
6388 (setq end-inst-point (save-excursion (forward-sexp 1) (point))
6389 st-point (point))
6390 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
6391 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
6392 (goto-char st-point)
6393 (while (re-search-forward "\\s *// Inouts" end-inst-point t)
6394 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-inout
6395 (goto-char st-point)
6396 (while (re-search-forward "\\s *// Inputs" end-inst-point t)
6397 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-in
6398 )))))
6399 ;; Combine duplicate bits
6400 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
6401 (vector (verilog-signals-combine-bus (nreverse sigs-out))
6402 (verilog-signals-combine-bus (nreverse sigs-inout))
6403 (verilog-signals-combine-bus (nreverse sigs-in))))))
6405 (defun verilog-read-inst-pins ()
6406 "Return an array of [ pins ] for the current instantiation at point.
6407 For example if declare A A (.B(SIG)) then B will be included in the list."
6408 (save-excursion
6409 (let ((end-mod-point (point)) ;; presume at /*AUTOINST*/ point
6410 pins pin)
6411 (verilog-backward-open-paren)
6412 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
6413 (setq pin (match-string 1))
6414 (unless (verilog-inside-comment-p)
6415 (setq pins (cons (list pin) pins))
6416 (when (looking-at "(")
6417 (forward-sexp 1))))
6418 (vector pins))))
6420 (defun verilog-read-arg-pins ()
6421 "Return an array of [ pins ] for the current argument declaration at point."
6422 (save-excursion
6423 (let ((end-mod-point (point)) ;; presume at /*AUTOARG*/ point
6424 pins pin)
6425 (verilog-backward-open-paren)
6426 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
6427 (setq pin (match-string 1))
6428 (unless (verilog-inside-comment-p)
6429 (setq pins (cons (list pin) pins))))
6430 (vector pins))))
6432 (defun verilog-read-auto-constants (beg end-mod-point)
6433 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
6434 ;; Insert new
6435 (save-excursion
6436 (let (sig-list tpl-end-pt)
6437 (goto-char beg)
6438 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
6439 (if (not (looking-at "\\s *("))
6440 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
6441 (search-forward "(" end-mod-point)
6442 (setq tpl-end-pt (save-excursion
6443 (backward-char 1)
6444 (forward-sexp 1) ;; Moves to paren that closes argdecl's
6445 (backward-char 1)
6446 (point)))
6447 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
6448 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
6449 sig-list)))
6451 (defun verilog-read-auto-lisp (start end)
6452 "Look for and evaluate a AUTO_LISP between START and END."
6453 (save-excursion
6454 (goto-char start)
6455 (while (re-search-forward "\\<AUTO_LISP(" end t)
6456 (backward-char)
6457 (let* ((beg-pt (prog1 (point)
6458 (forward-sexp 1))) ;; Closing paren
6459 (end-pt (point)))
6460 (eval-region beg-pt end-pt nil)))))
6462 (eval-when-compile
6463 ;; Prevent compile warnings; these are let's, not globals
6464 ;; Do not remove the eval-when-compile
6465 ;; - we want a error when we are debugging this code if they are refed.
6466 (defvar sigs-in)
6467 (defvar sigs-out)
6468 (defvar got-sig)
6469 (defvar got-rvalue)
6470 (defvar uses-delayed)
6471 (defvar vector-skip-list))
6473 (defun verilog-read-always-signals-recurse
6474 (exit-keywd rvalue ignore-next)
6475 "Recursive routine for parentheses/bracket matching.
6476 EXIT-KEYWD is expression to stop at, nil if top level.
6477 RVALUE is true if at right hand side of equal.
6478 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
6479 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ;; true if after a ; we are looking for rvalue
6480 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-rvalue end-else-check)
6481 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue ignore-next))))
6482 (while (not (or (eobp) gotend))
6483 (cond
6484 ((looking-at "//")
6485 (search-forward "\n"))
6486 ((looking-at "/\\*")
6487 (or (search-forward "*/")
6488 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
6489 ((looking-at "(\\*")
6490 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
6491 (search-forward "*)")
6492 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
6493 (t (setq keywd (buffer-substring-no-properties
6494 (point)
6495 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
6496 (forward-char 1))
6497 (point)))
6498 sig-last-tolk sig-tolk
6499 sig-tolk nil)
6500 ;;(if dbg (setq dbg (concat dbg (format "\tPt=%S %S\trv=%S in=%S ee=%S\n" (point) keywd rvalue ignore-next end-else-check))))
6501 (cond
6502 ((equal keywd "\"")
6503 (or (re-search-forward "[^\\]\"" nil t)
6504 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
6505 ;; else at top level loop, keep parsing
6506 ((and end-else-check (equal keywd "else"))
6507 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
6508 ;; no forward movement, want to see else in lower loop
6509 (setq end-else-check nil))
6510 ;; End at top level loop
6511 ((and end-else-check (looking-at "[^ \t\n\f]"))
6512 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
6513 (setq gotend t))
6514 ;; Final statement?
6515 ((and exit-keywd (equal keywd exit-keywd))
6516 (setq gotend t)
6517 (forward-char (length keywd)))
6518 ;; Standard tokens...
6519 ((equal keywd ";")
6520 (setq ignore-next nil rvalue semi-rvalue)
6521 ;; Final statement at top level loop?
6522 (when (not exit-keywd)
6523 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
6524 (setq end-else-check t))
6525 (forward-char 1))
6526 ((equal keywd "'")
6527 (if (looking-at "'s?[hdxbo][0-9a-fA-F_xz? \t]*")
6528 (goto-char (match-end 0))
6529 (forward-char 1)))
6530 ((equal keywd ":") ;; Case statement, begin/end label, x?y:z
6531 (cond ((equal "endcase" exit-keywd) ;; case x: y=z; statement next
6532 (setq ignore-next nil rvalue nil))
6533 ((equal "?" exit-keywd) ;; x?y:z rvalue
6534 ) ;; NOP
6535 (got-sig ;; label: statement
6536 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
6537 ((not rvalue) ;; begin label
6538 (setq ignore-next t rvalue nil)))
6539 (forward-char 1))
6540 ((equal keywd "=")
6541 (if (eq (char-before) ?< )
6542 (setq uses-delayed 1))
6543 (setq ignore-next nil rvalue t)
6544 (forward-char 1))
6545 ((equal keywd "?")
6546 (forward-char 1)
6547 (verilog-read-always-signals-recurse ":" rvalue nil))
6548 ((equal keywd "[")
6549 (forward-char 1)
6550 (verilog-read-always-signals-recurse "]" t nil))
6551 ((equal keywd "(")
6552 (forward-char 1)
6553 (cond (sig-last-tolk ;; Function call; zap last signal
6554 (setq got-sig nil)))
6555 (cond ((equal last-keywd "for")
6556 (verilog-read-always-signals-recurse ";" nil nil)
6557 (verilog-read-always-signals-recurse ";" t nil)
6558 (verilog-read-always-signals-recurse ")" nil nil))
6559 (t (verilog-read-always-signals-recurse ")" t nil))))
6560 ((equal keywd "begin")
6561 (skip-syntax-forward "w_")
6562 (verilog-read-always-signals-recurse "end" nil nil)
6563 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
6564 (setq ignore-next nil rvalue semi-rvalue)
6565 (if (not exit-keywd) (setq end-else-check t)))
6566 ((or (equal keywd "case")
6567 (equal keywd "casex")
6568 (equal keywd "casez"))
6569 (skip-syntax-forward "w_")
6570 (verilog-read-always-signals-recurse "endcase" t nil)
6571 (setq ignore-next nil rvalue semi-rvalue)
6572 (if (not exit-keywd) (setq gotend t))) ;; top level begin/end
6573 ((string-match "^[$`a-zA-Z_]" keywd) ;; not exactly word constituent
6574 (cond ((or (equal keywd "`ifdef")
6575 (equal keywd "`ifndef"))
6576 (setq ignore-next t))
6577 ((or ignore-next
6578 (member keywd verilog-keywords)
6579 (string-match "^\\$" keywd)) ;; PLI task
6580 (setq ignore-next nil))
6582 (setq keywd (verilog-symbol-detick-denumber keywd))
6583 (when got-sig
6584 (if got-rvalue (setq sigs-in (cons got-sig sigs-in))
6585 (setq sigs-out (cons got-sig sigs-out)))
6586 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S rv=%S\n" got-sig got-rvalue))))
6588 (setq got-rvalue rvalue
6589 got-sig (if (or (not keywd)
6590 (assoc keywd (if got-rvalue sigs-in sigs-out)))
6591 nil (list keywd nil nil))
6592 sig-tolk t)))
6593 (skip-chars-forward "a-zA-Z0-9$_.%`"))
6595 (forward-char 1)))
6596 ;; End of non-comment token
6597 (setq last-keywd keywd)))
6598 (skip-syntax-forward " "))
6599 ;; Append the final pending signal
6600 (when got-sig
6601 (if got-rvalue (setq sigs-in (cons got-sig sigs-in))
6602 (setq sigs-out (cons got-sig sigs-out)))
6603 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S rv=%S\n" got-sig got-rvalue))))
6604 (setq got-sig nil))
6605 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
6608 (defun verilog-read-always-signals ()
6609 "Parse always block at point and return list of (outputs inout inputs)."
6610 ;; Insert new
6611 (save-excursion
6612 (let* (;;(dbg "")
6613 sigs-in sigs-out
6614 uses-delayed) ;; Found signal/rvalue; push if not function
6615 (search-forward ")")
6616 (verilog-read-always-signals-recurse nil nil nil)
6617 ;;(if dbg (save-excursion (set-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg "")))
6618 ;; Return what was found
6619 (list sigs-out nil sigs-in uses-delayed))))
6621 (defun verilog-read-instants ()
6622 "Parse module at point and return list of ( ( file instance ) ... )."
6623 (verilog-beg-of-defun)
6624 (let* ((end-mod-point (verilog-get-end-of-defun t))
6625 (state nil)
6626 (instants-list nil))
6627 (save-excursion
6628 (while (< (point) end-mod-point)
6629 ;; Stay at level 0, no comments
6630 (while (progn
6631 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
6632 (or (> (car state) 0) ; in parens
6633 (nth 5 state) ; comment
6635 (forward-line 1))
6636 (beginning-of-line)
6637 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
6638 ;;(if (looking-at "^\\(.+\\)$")
6639 (let ((module (match-string 1))
6640 (instant (match-string 2)))
6641 (if (not (member module verilog-keywords))
6642 (setq instants-list (cons (list module instant) instants-list)))))
6643 (forward-line 1)))
6644 instants-list))
6647 (defun verilog-read-auto-template (module)
6648 "Look for a auto_template for the instantiation of the given MODULE.
6649 If found returns the signal name connections. Return REGEXP and
6650 list of ( (signal_name connection_name)... )."
6651 (save-excursion
6652 ;; Find beginning
6653 (let ((tpl-regexp "\\([0-9]+\\)")
6654 (lineno 0)
6655 (templateno 0)
6656 tpl-sig-list tpl-wild-list tpl-end-pt rep)
6657 (cond ((or
6658 (re-search-backward (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
6659 (progn
6660 (goto-char (point-min))
6661 (re-search-forward (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
6662 (goto-char (match-end 0))
6663 ;; Parse "REGEXP"
6664 ;; We reserve @"..." for future lisp expressions that evaluate once-per-AUTOINST
6665 (when (looking-at "\\s-*\"\\([^\"]*)\\)\"")
6666 (setq tpl-regexp (match-string 1))
6667 (goto-char (match-end 0)))
6668 (search-forward "(")
6669 ;; Parse lines in the template
6670 (when verilog-auto-inst-template-numbers
6671 (save-excursion
6672 (goto-char (point-min))
6673 (while (search-forward "AUTO_TEMPLATE" nil t)
6674 (setq templateno (1+ templateno)))))
6675 (setq tpl-end-pt (save-excursion
6676 (backward-char 1)
6677 (forward-sexp 1) ;; Moves to paren that closes argdecl's
6678 (backward-char 1)
6679 (point)))
6681 (while (< (point) tpl-end-pt)
6682 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
6683 (setq tpl-sig-list (cons (list
6684 (match-string-no-properties 1)
6685 (match-string-no-properties 2)
6686 templateno lineno)
6687 tpl-sig-list))
6688 (goto-char (match-end 0)))
6689 ;; Regexp form??
6690 ((looking-at
6691 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
6692 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
6693 (setq rep (match-string-no-properties 3))
6694 (goto-char (match-end 0))
6695 (setq tpl-wild-list
6696 (cons (list
6697 (concat "^"
6698 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
6699 (match-string 1))
6700 "$")
6702 templateno lineno)
6703 tpl-wild-list)))
6704 ((looking-at "[ \t\f]+")
6705 (goto-char (match-end 0)))
6706 ((looking-at "\n")
6707 (setq lineno (1+ lineno))
6708 (goto-char (match-end 0)))
6709 ((looking-at "//")
6710 (search-forward "\n"))
6711 ((looking-at "/\\*")
6712 (forward-char 2)
6713 (or (search-forward "*/")
6714 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
6716 (error "%s: AUTO_TEMPLATE parsing error: %s"
6717 (verilog-point-text)
6718 (progn (looking-at ".*$") (match-string 0))))))
6719 ;; Return
6720 (vector tpl-regexp
6721 (list tpl-sig-list tpl-wild-list)))
6722 ;; If no template found
6723 (t (vector tpl-regexp nil))))))
6724 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
6726 (defun verilog-set-define (defname defvalue &optional buffer enumname)
6727 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
6728 Optionally associate it with the specified enumeration ENUMNAME."
6729 (save-excursion
6730 (set-buffer (or buffer (current-buffer)))
6731 (let ((mac (intern (concat "vh-" defname))))
6732 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
6733 ;; Need to define to a constant if no value given
6734 (set (make-variable-buffer-local mac)
6735 (if (equal defvalue "") "1" defvalue)))
6736 (if enumname
6737 (let ((enumvar (intern (concat "venum-" enumname))))
6738 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
6739 (make-variable-buffer-local enumvar)
6740 (add-to-list enumvar defname)))))
6742 (defun verilog-read-defines (&optional filename recurse subcall)
6743 "Read `defines and parameters for the current file, or optional FILENAME.
6744 If the filename is provided, `verilog-library-flags' will be used to
6745 resolve it. If optional RECURSE is non-nil, recurse through `includes.
6747 Parameters must be simple assignments to constants, or have their own
6748 \"parameter\" label rather than a list of parameters. Thus:
6750 parameter X = 5, Y = 10; // Ok
6751 parameter X = {1'b1, 2'h2}; // Ok
6752 parameter X = {1'b1, 2'h2}, Y = 10; // Bad, make into 2 parameter lines
6754 Defines must be simple text substitutions, one on a line, starting
6755 at the beginning of the line. Any ifdefs or multiline comments around the
6756 define are ignored.
6758 Defines are stored inside Emacs variables using the name vh-{definename}.
6760 This function is useful for setting vh-* variables. The file variables
6761 feature can be used to set defines that `verilog-mode' can see; put at the
6762 *END* of your file something like:
6764 // Local Variables:
6765 // vh-macro:\"macro_definition\"
6766 // End:
6768 If macros are defined earlier in the same file and you want their values,
6769 you can read them automatically (provided `enable-local-eval' is on):
6771 // Local Variables:
6772 // eval:(verilog-read-defines)
6773 // eval:(verilog-read-defines \"group_standard_includes.v\")
6774 // End:
6776 Note these are only read when the file is first visited, you must use
6777 \\[find-alternate-file] RET to have these take effect after editing them!
6779 If you want to disable the \"Process `eval' or hook local variables\"
6780 warning message, you need to add to your .emacs file:
6782 (setq enable-local-eval t)"
6783 (let ((origbuf (current-buffer)))
6784 (save-excursion
6785 (unless subcall (verilog-getopt-flags))
6786 (when filename
6787 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
6788 (if fns
6789 (set-buffer (find-file-noselect (car fns)))
6790 (error (concat (verilog-point-text)
6791 ": Can't find verilog-read-defines file: " filename)))))
6792 (when recurse
6793 (goto-char (point-min))
6794 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
6795 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string-no-properties 1))))
6796 (unless (verilog-inside-comment-p)
6797 (verilog-read-defines inc recurse t)))))
6798 ;; Read `defines
6799 ;; note we don't use verilog-re... it's faster this way, and that
6800 ;; function has problems when comments are at the end of the define
6801 (goto-char (point-min))
6802 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
6803 (let ((defname (match-string-no-properties 1))
6804 (defvalue (match-string-no-properties 2)))
6805 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
6806 (verilog-set-define defname defvalue origbuf)))
6807 ;; Hack: Read parameters
6808 (goto-char (point-min))
6809 (while (re-search-forward
6810 "^\\s-*\\(parameter\\|localparam\\)\\(\\(\\s-*\\[[^]]*\\]\\|\\)\\s-+\\([a-zA-Z0-9_$]+\\)\\s-*=\\s-*\\([^;,]*\\),?\\|\\)\\s-*" nil t)
6811 (let ((var (match-string-no-properties 4))
6812 (val (match-string-no-properties 5))
6813 enumname)
6814 ;; The primary way of getting defines is verilog-read-decls
6815 ;; However, that isn't called yet for included files, so we'll add another scheme
6816 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
6817 (setq enumname (match-string-no-properties 1)))
6818 (if var
6819 (verilog-set-define var val origbuf enumname))
6820 (forward-comment 999)
6821 (while (looking-at "\\s-*,?\\s-*\\([a-zA-Z0-9_$]+\\)\\s-*=\\s-*\\([^;,]*\\),?\\s-*")
6822 (verilog-set-define (match-string-no-properties 1) (match-string-no-properties 2) origbuf enumname)
6823 (goto-char (match-end 0))
6824 (forward-comment 999)))))))
6826 (defun verilog-read-includes ()
6827 "Read `includes for the current file.
6828 This will find all of the `includes which are at the beginning of lines,
6829 ignoring any ifdefs or multiline comments around them.
6830 `verilog-read-defines' is then performed on the current and each included
6831 file.
6833 It is often useful put at the *END* of your file something like:
6835 // Local Variables:
6836 // eval:(verilog-read-defines)
6837 // eval:(verilog-read-includes)
6838 // End:
6840 Note includes are only read when the file is first visited, you must use
6841 \\[find-alternate-file] RET to have these take effect after editing them!
6843 It is good to get in the habit of including all needed files in each .v
6844 file that needs it, rather than waiting for compile time. This will aid
6845 this process, Verilint, and readability. To prevent defining the same
6846 variable over and over when many modules are compiled together, put a test
6847 around the inside each include file:
6849 foo.v (a include):
6850 `ifdef _FOO_V // include if not already included
6851 `else
6852 `define _FOO_V
6853 ... contents of file
6854 `endif // _FOO_V"
6855 ;;slow: (verilog-read-defines nil t))
6856 (save-excursion
6857 (verilog-getopt-flags)
6858 (goto-char (point-min))
6859 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
6860 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
6861 (verilog-read-defines inc nil t)))))
6863 (defun verilog-read-signals (&optional start end)
6864 "Return a simple list of all possible signals in the file.
6865 Bounded by optional region from START to END. Overly aggressive but fast.
6866 Some macros and such are also found and included. For dinotrace.el."
6867 (let (sigs-all keywd)
6868 (progn;save-excursion
6869 (goto-char (or start (point-min)))
6870 (setq end (or end (point-max)))
6871 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
6872 (forward-char -1)
6873 (cond
6874 ((looking-at "//")
6875 (search-forward "\n"))
6876 ((looking-at "/\\*")
6877 (search-forward "*/"))
6878 ((looking-at "(\\*")
6879 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
6880 (search-forward "*)")))
6881 ((eq ?\" (following-char))
6882 (re-search-forward "[^\\]\"")) ;; don't forward-char first, since we look for a non backslash first
6883 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
6884 (goto-char (match-end 0))
6885 (setq keywd (match-string-no-properties 1))
6886 (or (member keywd verilog-keywords)
6887 (member keywd sigs-all)
6888 (setq sigs-all (cons keywd sigs-all))))
6889 (t (forward-char 1))))
6890 ;; Return list
6891 sigs-all)))
6894 ;; Argument file parsing
6897 (defun verilog-getopt (arglist)
6898 "Parse -f, -v etc arguments in ARGLIST list or string."
6899 (unless (listp arglist) (setq arglist (list arglist)))
6900 (let ((space-args '())
6901 arg next-param)
6902 ;; Split on spaces, so users can pass whole command lines
6903 (while arglist
6904 (setq arg (car arglist)
6905 arglist (cdr arglist))
6906 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
6907 (setq space-args (append space-args
6908 (list (match-string-no-properties 1 arg))))
6909 (setq arg (match-string 2 arg))))
6910 ;; Parse arguments
6911 (while space-args
6912 (setq arg (car space-args)
6913 space-args (cdr space-args))
6914 (cond
6915 ;; Need another arg
6916 ((equal arg "-f")
6917 (setq next-param arg))
6918 ((equal arg "-v")
6919 (setq next-param arg))
6920 ((equal arg "-y")
6921 (setq next-param arg))
6922 ;; +libext+(ext1)+(ext2)...
6923 ((string-match "^\\+libext\\+\\(.*\\)" arg)
6924 (setq arg (match-string 1 arg))
6925 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
6926 (verilog-add-list-unique `verilog-library-extensions
6927 (match-string 1 arg))
6928 (setq arg (match-string 2 arg))))
6930 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; -Ddefine=val
6931 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ;; -Ddefine
6932 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; +define+val
6933 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ;; +define+define
6934 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
6936 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ;; +incdir+dir
6937 (string-match "^-I\\(.*\\)" arg)) ;; -Idir
6938 (verilog-add-list-unique `verilog-library-directories
6939 (match-string 1 arg)))
6940 ;; Ignore
6941 ((equal "+librescan" arg))
6942 ((string-match "^-U\\(.*\\)" arg)) ;; -Udefine
6943 ;; Second parameters
6944 ((equal next-param "-f")
6945 (setq next-param nil)
6946 (verilog-getopt-file arg))
6947 ((equal next-param "-v")
6948 (setq next-param nil)
6949 (verilog-add-list-unique `verilog-library-files arg))
6950 ((equal next-param "-y")
6951 (setq next-param nil)
6952 (verilog-add-list-unique `verilog-library-directories arg))
6953 ;; Filename
6954 ((string-match "^[^-+]" arg)
6955 (verilog-add-list-unique `verilog-library-files arg))
6956 ;; Default - ignore; no warning
6957 ))))
6958 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
6960 (defun verilog-getopt-file (filename)
6961 "Read Verilog options from the specified FILENAME."
6962 (save-excursion
6963 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
6964 (orig-buffer (current-buffer))
6965 line)
6966 (if fns
6967 (set-buffer (find-file-noselect (car fns)))
6968 (error (concat (verilog-point-text)
6969 ": Can't find verilog-getopt-file -f file: " filename)))
6970 (goto-char (point-min))
6971 (while (not (eobp))
6972 (setq line (buffer-substring (point)
6973 (save-excursion (end-of-line) (point))))
6974 (forward-line 1)
6975 (when (string-match "//" line)
6976 (setq line (substring line 0 (match-beginning 0))))
6977 (save-excursion
6978 (set-buffer orig-buffer) ; Variables are buffer-local, so need right context.
6979 (verilog-getopt line))))))
6981 (defun verilog-getopt-flags ()
6982 "Convert `verilog-library-flags' into standard library variables."
6983 ;; If the flags are local, then all the outputs should be local also
6984 (when (local-variable-p `verilog-library-flags (current-buffer))
6985 (mapc 'make-local-variable '(verilog-library-extensions
6986 verilog-library-directories
6987 verilog-library-files
6988 verilog-library-flags)))
6989 ;; Allow user to customize
6990 (run-hooks 'verilog-before-getopt-flags-hook)
6991 ;; Process arguments
6992 (verilog-getopt verilog-library-flags)
6993 ;; Allow user to customize
6994 (run-hooks 'verilog-getopt-flags-hook))
6996 (defun verilog-add-list-unique (varref object)
6997 "Append to VARREF list the given OBJECT,
6998 unless it is already a member of the variable's list."
6999 (unless (member object (symbol-value varref))
7000 (set varref (append (symbol-value varref) (list object))))
7001 varref)
7002 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
7006 ;; Cached directory support
7009 (defvar verilog-dir-cache-preserving nil
7010 "If set, the directory cache is enabled, and file system changes are ignored.
7011 See `verilog-dir-exists-p' and `verilog-dir-files'.")
7013 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
7014 (defvar verilog-dir-cache-list nil
7015 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
7016 (defvar verilog-dir-cache-lib-filenames nil
7017 "Cached data for `verilog-library-filenames'.")
7019 (defmacro verilog-preserve-dir-cache (&rest body)
7020 "Execute the BODY forms, allowing directory cache preservation within BODY.
7021 This means that changes inside BODY made to the file system will not be
7022 seen by the `verilog-dir-files' and related functions."
7023 `(let ((verilog-dir-cache-preserving t)
7024 verilog-dir-cache-list
7025 verilog-dir-cache-lib-filenames)
7026 (progn ,@body)))
7028 (defun verilog-dir-files (dirname)
7029 "Return all filenames in the DIRNAME directory.
7030 Relative paths depend on the `default-directory'.
7031 Results are cached if inside `verilog-preserve-dir-cache'."
7032 (unless verilog-dir-cache-preserving
7033 (setq verilog-dir-cache-list nil)) ;; Cache disabled
7034 ;; We don't use expand-file-name on the dirname to make key, as it's slow
7035 (let* ((cache-key (list dirname default-directory))
7036 (fass (assoc cache-key verilog-dir-cache-list))
7037 exp-dirname data)
7038 (cond (fass ;; Return data from cache hit
7039 (nth 1 fass))
7041 (setq exp-dirname (expand-file-name dirname)
7042 data (and (file-directory-p exp-dirname)
7043 (directory-files exp-dirname nil nil nil)))
7044 ;; Note we also encache nil for non-existing dirs.
7045 (setq verilog-dir-cache-list (cons (list cache-key data)
7046 verilog-dir-cache-list))
7047 data))))
7048 ;; Miss-and-hit test:
7049 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
7050 ;; (prin1 (verilog-dir-files ".")) nil)
7052 (defun verilog-dir-file-exists-p (filename)
7053 "Return true if FILENAME exists.
7054 Like `file-exists-p' but results are cached if inside
7055 `verilog-preserve-dir-cache'."
7056 (let* ((dirname (file-name-directory filename))
7057 ;; Correct for file-name-nondirectory returning same if no slash.
7058 (dirnamed (if (or (not dirname) (equal dirname filename))
7059 default-directory dirname))
7060 (flist (verilog-dir-files dirnamed)))
7061 (and flist
7062 (member (file-name-nondirectory filename) flist)
7063 t)))
7064 ;;(verilog-dir-file-exists-p "verilog-mode.el")
7065 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
7069 ;; Module name lookup
7072 (defun verilog-module-inside-filename-p (module filename)
7073 "Return point if MODULE is specified inside FILENAME, else nil.
7074 Allows version control to check out the file if need be."
7075 (and (or (file-exists-p filename)
7076 (and (fboundp 'vc-backend)
7077 (vc-backend filename)))
7078 (let (pt)
7079 (save-excursion
7080 (set-buffer (find-file-noselect filename))
7081 (goto-char (point-min))
7082 (while (and
7083 ;; It may be tempting to look for verilog-defun-re, don't, it slows things down a lot!
7084 (verilog-re-search-forward-quick "\\<module\\>" nil t)
7085 (verilog-re-search-forward-quick "[(;]" nil t))
7086 (if (equal module (verilog-read-module-name))
7087 (setq pt (point))))
7088 pt))))
7090 (defun verilog-is-number (symbol)
7091 "Return true if SYMBOL is number-like."
7092 (or (string-match "^[0-9 \t:]+$" symbol)
7093 (string-match "^[---]*[0-9]+$" symbol)
7094 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
7096 (defun verilog-symbol-detick (symbol wing-it)
7097 "Return an expanded SYMBOL name without any defines.
7098 If the variable vh-{symbol} is defined, return that value.
7099 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
7100 (while (and symbol (string-match "^`" symbol))
7101 (setq symbol (substring symbol 1))
7102 (setq symbol
7103 (if (boundp (intern (concat "vh-" symbol)))
7104 ;; Emacs has a bug where boundp on a buffer-local
7105 ;; variable in only one buffer returns t in another.
7106 ;; This can confuse, so check for nil.
7107 (let ((val (eval (intern (concat "vh-" symbol)))))
7108 (if (eq val nil)
7109 (if wing-it symbol nil)
7110 val))
7111 (if wing-it symbol nil))))
7112 symbol)
7113 ;;(verilog-symbol-detick "`mod" nil)
7115 (defun verilog-symbol-detick-denumber (symbol)
7116 "Return SYMBOL with defines converted and any numbers dropped to nil."
7117 (when (string-match "^`" symbol)
7118 ;; This only will work if the define is a simple signal, not
7119 ;; something like a[b]. Sorry, it should be substituted into the parser
7120 (setq symbol
7121 (verilog-string-replace-matches
7122 "\[[^0-9: \t]+\]" "" nil nil
7123 (or (verilog-symbol-detick symbol nil)
7124 (if verilog-auto-sense-defines-constant
7126 symbol)))))
7127 (if (verilog-is-number symbol)
7129 symbol))
7131 (defun verilog-symbol-detick-text (text)
7132 "Return TEXT without any known defines.
7133 If the variable vh-{symbol} is defined, substitute that value."
7134 (let ((ok t) symbol val)
7135 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
7136 (setq symbol (match-string 1 text))
7137 (message symbol)
7138 (cond ((and
7139 (boundp (intern (concat "vh-" symbol)))
7140 ;; Emacs has a bug where boundp on a buffer-local
7141 ;; variable in only one buffer returns t in another.
7142 ;; This can confuse, so check for nil.
7143 (setq val (eval (intern (concat "vh-" symbol)))))
7144 (setq text (replace-match val nil nil text)))
7145 (t (setq ok nil)))))
7146 text)
7147 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
7149 (defun verilog-expand-dirnames (&optional dirnames)
7150 "Return a list of existing directories given a list of wildcarded DIRNAMES.
7151 Or, just the existing dirnames themselves if there are no wildcards."
7152 ;; Note this function is performance critical.
7153 ;; Do not call anything that requires disk access that cannot be cached.
7154 (interactive)
7155 (unless dirnames (error "`verilog-library-directories' should include at least '.'"))
7156 (setq dirnames (reverse dirnames)) ; not nreverse
7157 (let ((dirlist nil)
7158 pattern dirfile dirfiles dirname root filename rest basefile)
7159 (while dirnames
7160 (setq dirname (substitute-in-file-name (car dirnames))
7161 dirnames (cdr dirnames))
7162 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ;; root
7163 "\\([^/\\]*[*?][^/\\]*\\)" ;; filename with *?
7164 "\\(.*\\)") ;; rest
7165 dirname)
7166 (setq root (match-string 1 dirname)
7167 filename (match-string 2 dirname)
7168 rest (match-string 3 dirname)
7169 pattern filename)
7170 ;; now replace those * and ? with .+ and .
7171 ;; use ^ and /> to get only whole file names
7172 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
7173 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
7174 pattern (concat "^" pattern "$")
7175 dirfiles (verilog-dir-files root))
7176 (while dirfiles
7177 (setq basefile (car dirfiles)
7178 dirfile (expand-file-name (concat root basefile rest))
7179 dirfiles (cdr dirfiles))
7180 (if (and (string-match pattern basefile)
7181 ;; Don't allow abc/*/rtl to match abc/rtl via ..
7182 (not (equal basefile "."))
7183 (not (equal basefile ".."))
7184 (file-directory-p dirfile))
7185 (setq dirlist (cons dirfile dirlist)))))
7186 ;; Defaults
7188 (if (file-directory-p dirname)
7189 (setq dirlist (cons dirname dirlist))))))
7190 dirlist))
7191 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
7193 (defun verilog-library-filenames (filename current &optional check-ext)
7194 "Return a search path to find the given FILENAME or module name.
7195 Uses the CURRENT filename, `verilog-library-directories' and
7196 `verilog-library-extensions' variables to build the path.
7197 With optional CHECK-EXT also check `verilog-library-extensions'."
7198 (unless verilog-dir-cache-preserving
7199 (setq verilog-dir-cache-lib-filenames nil))
7200 (let* ((cache-key (list filename current check-ext))
7201 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
7202 chkdirs chkdir chkexts fn outlist)
7203 (cond (fass ;; Return data from cache hit
7204 (nth 1 fass))
7206 ;; Note this expand can't be easily cached, as we need to
7207 ;; pick up buffer-local variables for newly read sub-module files
7208 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
7209 (while chkdirs
7210 (setq chkdir (expand-file-name (car chkdirs)
7211 (file-name-directory current))
7212 chkexts (if check-ext verilog-library-extensions `("")))
7213 (while chkexts
7214 (setq fn (expand-file-name (concat filename (car chkexts))
7215 chkdir))
7216 ;;(message "Check for %s" fn)
7217 (if (verilog-dir-file-exists-p fn)
7218 (setq outlist (cons (expand-file-name
7219 fn (file-name-directory current))
7220 outlist)))
7221 (setq chkexts (cdr chkexts)))
7222 (setq chkdirs (cdr chkdirs)))
7223 (setq outlist (nreverse outlist))
7224 (setq verilog-dir-cache-lib-filenames
7225 (cons (list cache-key outlist)
7226 verilog-dir-cache-lib-filenames))
7227 outlist))))
7229 (defun verilog-module-filenames (module current)
7230 "Return a search path to find the given MODULE name.
7231 Uses the CURRENT filename, `verilog-library-extensions',
7232 `verilog-library-directories' and `verilog-library-files'
7233 variables to build the path."
7234 ;; Return search locations for it
7235 (append (list current) ; first, current buffer
7236 (verilog-library-filenames module current t)
7237 verilog-library-files)) ; finally, any libraries
7240 ;; Module Information
7242 ;; Many of these functions work on "modi" a module information structure
7243 ;; A modi is: [module-name-string file-name begin-point]
7245 (defvar verilog-cache-enabled t
7246 "If true, enable caching of signals, etc. Set to nil for debugging to make things SLOW!")
7248 (defvar verilog-modi-cache-list nil
7249 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
7250 For speeding up verilog-modi-get-* commands.
7251 Buffer-local.")
7253 (make-variable-buffer-local 'verilog-modi-cache-list)
7255 (defvar verilog-modi-cache-preserve-tick nil
7256 "Modification tick after which the cache is still considered valid.
7257 Use `verilog-preserve-modi-cache' to set it.")
7258 (defvar verilog-modi-cache-preserve-buffer nil
7259 "Modification tick after which the cache is still considered valid.
7260 Use `verilog-preserve-modi-cache' to set it.")
7262 (defun verilog-modi-current ()
7263 "Return the modi structure for the module currently at point."
7264 (let* (name pt)
7265 ;; read current module's name
7266 (save-excursion
7267 (verilog-re-search-backward-quick verilog-defun-re nil nil)
7268 (verilog-re-search-forward-quick "(" nil nil)
7269 (setq name (verilog-read-module-name))
7270 (setq pt (point)))
7271 ;; return
7272 (vector name (or (buffer-file-name) (current-buffer)) pt)))
7274 (defvar verilog-modi-lookup-last-mod nil "Cache of last module looked up.")
7275 (defvar verilog-modi-lookup-last-modi nil "Cache of last modi returned.")
7276 (defvar verilog-modi-lookup-last-current nil "Cache of last `current-buffer' looked up.")
7277 (defvar verilog-modi-lookup-last-tick nil "Cache of last `buffer-modified-tick' looked up.")
7279 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
7280 "Find the file and point at which MODULE is defined.
7281 If ALLOW-CACHE is set, check and remember cache of previous lookups.
7282 Return modi if successful, else print message unless IGNORE-ERROR is true."
7283 (let* ((current (or (buffer-file-name) (current-buffer))))
7284 (cond ((and verilog-modi-lookup-last-modi
7285 verilog-cache-enabled
7286 allow-cache
7287 (equal verilog-modi-lookup-last-mod module)
7288 (equal verilog-modi-lookup-last-current current)
7289 (equal verilog-modi-lookup-last-tick (buffer-modified-tick)))
7290 ;; ok as is
7292 (t (let* ((realmod (verilog-symbol-detick module t))
7293 (orig-filenames (verilog-module-filenames realmod current))
7294 (filenames orig-filenames)
7296 (while (and filenames (not pt))
7297 (if (not (setq pt (verilog-module-inside-filename-p realmod (car filenames))))
7298 (setq filenames (cdr filenames))))
7299 (cond (pt (setq verilog-modi-lookup-last-modi
7300 (vector realmod (car filenames) pt)))
7301 (t (setq verilog-modi-lookup-last-modi nil)
7302 (or ignore-error
7303 (error (concat (verilog-point-text)
7304 ": Can't locate " module " module definition"
7305 (if (not (equal module realmod))
7306 (concat " (Expanded macro to " realmod ")")
7308 "\n Check the verilog-library-directories variable."
7309 "\n I looked in (if not listed, doesn't exist):\n\t"
7310 (mapconcat 'concat orig-filenames "\n\t"))))))
7311 (setq verilog-modi-lookup-last-mod module
7312 verilog-modi-lookup-last-current current
7313 verilog-modi-lookup-last-tick (buffer-modified-tick)))))
7314 verilog-modi-lookup-last-modi))
7316 (defsubst verilog-modi-name (modi)
7317 (aref modi 0))
7318 (defsubst verilog-modi-file-or-buffer (modi)
7319 (aref modi 1))
7320 (defsubst verilog-modi-point (modi)
7321 (aref modi 2))
7323 (defun verilog-modi-filename (modi)
7324 "Filename of MODI, or name of buffer if it's never been saved."
7325 (if (bufferp (verilog-modi-file-or-buffer modi))
7326 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
7327 (buffer-name (verilog-modi-file-or-buffer modi)))
7328 (verilog-modi-file-or-buffer modi)))
7330 (defun verilog-modi-goto (modi)
7331 "Move point/buffer to specified MODI."
7332 (or modi (error "Passed unfound modi to goto, check earlier"))
7333 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
7334 (verilog-modi-file-or-buffer modi)
7335 (find-file-noselect (verilog-modi-file-or-buffer modi))))
7336 (or (equal major-mode `verilog-mode) ;; Put into Verilog mode to get syntax
7337 (verilog-mode))
7338 (goto-char (verilog-modi-point modi)))
7340 (defun verilog-goto-defun-file (module)
7341 "Move point to the file at which a given MODULE is defined."
7342 (interactive "sGoto File for Module: ")
7343 (let* ((modi (verilog-modi-lookup module nil)))
7344 (when modi
7345 (verilog-modi-goto modi)
7346 (switch-to-buffer (current-buffer)))))
7348 (defun verilog-modi-cache-results (modi function)
7349 "Run on MODI the given FUNCTION. Locate the module in a file.
7350 Cache the output of function so next call may have faster access."
7351 (let (fass)
7352 (save-excursion ;; Cache is buffer-local so can't avoid this.
7353 (verilog-modi-goto modi)
7354 (if (and (setq fass (assoc (list modi function)
7355 verilog-modi-cache-list))
7356 ;; Destroy caching when incorrect; Modified or file changed
7357 (not (and verilog-cache-enabled
7358 (or (equal (buffer-modified-tick) (nth 1 fass))
7359 (and verilog-modi-cache-preserve-tick
7360 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
7361 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
7362 (equal (visited-file-modtime) (nth 2 fass)))))
7363 (setq verilog-modi-cache-list nil
7364 fass nil))
7365 (cond (fass
7366 ;; Return data from cache hit
7367 (nth 3 fass))
7369 ;; Read from file
7370 ;; Clear then restore any hilighting to make emacs19 happy
7371 (let ((fontlocked (when (and (boundp 'font-lock-mode)
7372 font-lock-mode)
7373 (font-lock-mode nil)
7375 func-returns)
7376 (setq func-returns (funcall function))
7377 (when fontlocked (font-lock-mode t))
7378 ;; Cache for next time
7379 (setq verilog-modi-cache-list
7380 (cons (list (list modi function)
7381 (buffer-modified-tick)
7382 (visited-file-modtime)
7383 func-returns)
7384 verilog-modi-cache-list))
7385 func-returns))))))
7387 (defun verilog-modi-cache-add (modi function element sig-list)
7388 "Add function return results to the module cache.
7389 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
7390 function now contains the additional SIG-LIST parameters."
7391 (let (fass)
7392 (save-excursion
7393 (verilog-modi-goto modi)
7394 (if (setq fass (assoc (list modi function)
7395 verilog-modi-cache-list))
7396 (let ((func-returns (nth 3 fass)))
7397 (aset func-returns element
7398 (append sig-list (aref func-returns element))))))))
7400 (defmacro verilog-preserve-modi-cache (&rest body)
7401 "Execute the BODY forms, allowing cache preservation within BODY.
7402 This means that changes to the buffer will not result in the cache being
7403 flushed. If the changes affect the modsig state, they must call the
7404 modsig-cache-add-* function, else the results of later calls may be
7405 incorrect. Without this, changes are assumed to be adding/removing signals
7406 and invalidating the cache."
7407 `(let ((verilog-modi-cache-preserve-tick (buffer-modified-tick))
7408 (verilog-modi-cache-preserve-buffer (current-buffer)))
7409 (progn ,@body)))
7412 (defun verilog-signals-matching-enum (in-list enum)
7413 "Return all signals in IN-LIST matching the given ENUM."
7414 (let (out-list)
7415 (while in-list
7416 (if (equal (verilog-sig-enum (car in-list)) enum)
7417 (setq out-list (cons (car in-list) out-list)))
7418 (setq in-list (cdr in-list)))
7419 ;; New scheme
7420 (let* ((enumvar (intern (concat "venum-" enum)))
7421 (enumlist (and (boundp enumvar) (eval enumvar))))
7422 (while enumlist
7423 (add-to-list 'out-list (list (car enumlist)))
7424 (setq enumlist (cdr enumlist))))
7425 (nreverse out-list)))
7427 (defun verilog-signals-matching-regexp (in-list regexp)
7428 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
7429 (if (not regexp)
7430 in-list
7431 (let (out-list)
7432 (while in-list
7433 (if (string-match regexp (verilog-sig-name (car in-list)))
7434 (setq out-list (cons (car in-list) out-list)))
7435 (setq in-list (cdr in-list)))
7436 (nreverse out-list))))
7438 (defun verilog-signals-not-matching-regexp (in-list regexp)
7439 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
7440 (if (not regexp)
7441 in-list
7442 (let (out-list)
7443 (while in-list
7444 (if (not (string-match regexp (verilog-sig-name (car in-list))))
7445 (setq out-list (cons (car in-list) out-list)))
7446 (setq in-list (cdr in-list)))
7447 (nreverse out-list))))
7449 ;; Combined
7450 (defun verilog-decls-get-signals (decls)
7451 (append
7452 (verilog-decls-get-outputs decls)
7453 (verilog-decls-get-inouts decls)
7454 (verilog-decls-get-inputs decls)
7455 (verilog-decls-get-wires decls)
7456 (verilog-decls-get-regs decls)
7457 (verilog-decls-get-assigns decls)
7458 (verilog-decls-get-consts decls)
7459 (verilog-decls-get-gparams decls)))
7461 (defun verilog-decls-get-ports (decls)
7462 (append
7463 (verilog-decls-get-outputs decls)
7464 (verilog-decls-get-inouts decls)
7465 (verilog-decls-get-inputs decls)))
7467 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
7468 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
7469 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
7470 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
7471 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
7472 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
7473 (defsubst verilog-modi-cache-add-wires (modi sig-list)
7474 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
7475 (defsubst verilog-modi-cache-add-regs (modi sig-list)
7476 (verilog-modi-cache-add modi 'verilog-read-decls 4 sig-list))
7478 (defun verilog-signals-from-signame (signame-list)
7479 "Return signals in standard form from SIGNAME-LIST, a simple list of signal names."
7480 (mapcar (function (lambda (name) (list name nil nil)))
7481 signame-list))
7484 ;; Auto creation utilities
7487 (defun verilog-auto-re-search-do (search-for func)
7488 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
7489 (goto-char (point-min))
7490 (while (verilog-re-search-forward search-for nil t)
7491 (funcall func)))
7493 (defun verilog-insert-one-definition (sig type indent-pt)
7494 "Print out a definition for SIG of the given TYPE,
7495 with appropriate INDENT-PT indentation."
7496 (indent-to indent-pt)
7497 (insert type)
7498 (when (verilog-sig-signed sig)
7499 (insert " " (verilog-sig-signed sig)))
7500 (when (verilog-sig-multidim sig)
7501 (insert " " (verilog-sig-multidim-string sig)))
7502 (when (verilog-sig-bits sig)
7503 (insert " " (verilog-sig-bits sig)))
7504 (indent-to (max 24 (+ indent-pt 16)))
7505 (unless (= (char-syntax (preceding-char)) ?\ )
7506 (insert " ")) ; Need space between "]name" if indent-to did nothing
7507 (insert (verilog-sig-name sig)))
7509 (defun verilog-insert-definition (sigs direction indent-pt v2k &optional dont-sort)
7510 "Print out a definition for a list of SIGS of the given DIRECTION,
7511 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
7512 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output."
7513 (or dont-sort
7514 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
7515 (while sigs
7516 (let ((sig (car sigs)))
7517 (verilog-insert-one-definition
7519 ;; Want "type x" or "output type x", not "wire type x"
7520 (cond ((verilog-sig-type sig)
7521 (concat
7522 (if (not (equal direction "wire"))
7523 (concat direction " "))
7524 (verilog-sig-type sig)))
7525 (t direction))
7526 indent-pt)
7527 (insert (if v2k "," ";"))
7528 (if (or (not (verilog-sig-comment sig))
7529 (equal "" (verilog-sig-comment sig)))
7530 (insert "\n")
7531 (indent-to (max 48 (+ indent-pt 40)))
7532 (insert (concat "// " (verilog-sig-comment sig) "\n")))
7533 (setq sigs (cdr sigs)))))
7535 (eval-when-compile
7536 (if (not (boundp 'indent-pt))
7537 (defvar indent-pt nil "Local used by insert-indent")))
7539 (defun verilog-insert-indent (&rest stuff)
7540 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
7541 Presumes that any newlines end a list element."
7542 (let ((need-indent t))
7543 (while stuff
7544 (if need-indent (indent-to indent-pt))
7545 (setq need-indent nil)
7546 (insert (car stuff))
7547 (setq need-indent (string-match "\n$" (car stuff))
7548 stuff (cdr stuff)))))
7549 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
7551 (defun verilog-repair-open-comma ()
7552 "If backwards-from-point is other than a open parenthesis insert comma."
7553 (save-excursion
7554 (verilog-backward-syntactic-ws)
7555 (when (save-excursion
7556 (backward-char 1)
7557 (and (not (looking-at "[(,]"))
7558 (progn
7559 (verilog-re-search-backward "[(`]" nil t)
7560 (looking-at "("))))
7561 (insert ","))))
7563 (defun verilog-repair-close-comma ()
7564 "If point is at a comma followed by a close parenthesis, fix it.
7565 This repairs those mis-inserted by a AUTOARG."
7566 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
7567 (save-excursion
7568 (verilog-forward-close-paren)
7569 (backward-char 1)
7570 (verilog-backward-syntactic-ws)
7571 (backward-char 1)
7572 (when (looking-at ",")
7573 (delete-char 1))))
7575 (defun verilog-get-list (start end)
7576 "Return the elements of a comma separated list between START and END."
7577 (interactive)
7578 (let ((my-list (list))
7579 my-string)
7580 (save-excursion
7581 (while (< (point) end)
7582 (when (re-search-forward "\\([^,{]+\\)" end t)
7583 (setq my-string (verilog-string-remove-spaces (match-string 1)))
7584 (setq my-list (nconc my-list (list my-string) ))
7585 (goto-char (match-end 0))))
7586 my-list)))
7588 (defun verilog-make-width-expression (range-exp)
7589 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
7590 ;; strip off the []
7591 (cond ((not range-exp)
7592 "1")
7594 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
7595 (setq range-exp (match-string 1 range-exp)))
7596 (cond ((not range-exp)
7597 "1")
7598 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
7599 range-exp)
7600 (int-to-string
7601 (1+ (abs (- (string-to-number (match-string 1 range-exp))
7602 (string-to-number (match-string 2 range-exp)))))))
7603 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
7604 (concat "(1+(" (match-string 1 range-exp) ")"
7605 (if (equal "0" (match-string 2 range-exp))
7606 "" ;; Don't bother with -(0)
7607 (concat "-(" (match-string 2 range-exp) ")"))
7608 ")"))
7609 (t nil)))))
7610 ;;(verilog-make-width-expression "`A:`B")
7612 (defun verilog-typedef-name-p (variable-name)
7613 "Return true if the VARIABLE-NAME is a type definition."
7614 (when verilog-typedef-regexp
7615 (string-match verilog-typedef-regexp variable-name)))
7618 ;; Auto deletion
7621 (defun verilog-delete-autos-lined ()
7622 "Delete autos that occupy multiple lines, between begin and end comments."
7623 (let ((pt (point)))
7624 (forward-line 1)
7625 (when (and
7626 (looking-at "\\s-*// Beginning")
7627 (search-forward "// End of automatic" nil t))
7628 ;; End exists
7629 (end-of-line)
7630 (delete-region pt (point))
7631 (forward-line 1))))
7633 (defun verilog-forward-close-paren ()
7634 "Find the close parenthesis that match the current point.
7635 Ignore other close parenthesis with matching open parens."
7636 (let ((parens 1))
7637 (while (> parens 0)
7638 (unless (verilog-re-search-forward-quick "[()]" nil t)
7639 (error "%s: Mismatching ()" (verilog-point-text)))
7640 (cond ((= (preceding-char) ?\( )
7641 (setq parens (1+ parens)))
7642 ((= (preceding-char) ?\) )
7643 (setq parens (1- parens)))))))
7645 (defun verilog-backward-open-paren ()
7646 "Find the open parenthesis that match the current point.
7647 Ignore other open parenthesis with matching close parens."
7648 (let ((parens 1))
7649 (while (> parens 0)
7650 (unless (verilog-re-search-backward-quick "[()]" nil t)
7651 (error "%s: Mismatching ()" (verilog-point-text)))
7652 (cond ((= (following-char) ?\) )
7653 (setq parens (1+ parens)))
7654 ((= (following-char) ?\( )
7655 (setq parens (1- parens)))))))
7657 (defun verilog-backward-open-bracket ()
7658 "Find the open bracket that match the current point.
7659 Ignore other open bracket with matching close bracket."
7660 (let ((parens 1))
7661 (while (> parens 0)
7662 (unless (verilog-re-search-backward-quick "[][]" nil t)
7663 (error "%s: Mismatching []" (verilog-point-text)))
7664 (cond ((= (following-char) ?\] )
7665 (setq parens (1+ parens)))
7666 ((= (following-char) ?\[ )
7667 (setq parens (1- parens)))))))
7669 (defun verilog-delete-to-paren ()
7670 "Delete the automatic inst/sense/arg created by autos.
7671 Deletion stops at the matching end parenthesis."
7672 (delete-region (point)
7673 (save-excursion
7674 (verilog-backward-open-paren)
7675 (forward-sexp 1) ;; Moves to paren that closes argdecl's
7676 (backward-char 1)
7677 (point))))
7679 (defun verilog-auto-star-safe ()
7680 "Return if a .* AUTOINST is safe to delete or expand.
7681 It was created by the AUTOS themselves, or by the user."
7682 (and verilog-auto-star-expand
7683 (looking-at "[ \t\n\f,]*\\([)]\\|// \\(Outputs\\|Inouts\\|Inputs\\)\\)")))
7685 (defun verilog-delete-auto-star-all ()
7686 "Delete a .* AUTOINST, if it is safe."
7687 (when (verilog-auto-star-safe)
7688 (verilog-delete-to-paren)))
7690 (defun verilog-delete-auto-star-implicit ()
7691 "Delete all .* implicit connections created by `verilog-auto-star'.
7692 This function will be called automatically at save unless
7693 `verilog-auto-star-save' is set, any non-templated expanded pins will be
7694 removed."
7695 (interactive)
7696 (let (paren-pt indent have-close-paren)
7697 (save-excursion
7698 (goto-char (point-min))
7699 ;; We need to match these even outside of comments.
7700 ;; For reasonable performance, we don't check if inside comments, sorry.
7701 (while (re-search-forward "// Implicit \\.\\*" nil t)
7702 (setq paren-pt (point))
7703 (beginning-of-line)
7704 (setq have-close-paren
7705 (save-excursion
7706 (when (search-forward ");" paren-pt t)
7707 (setq indent (current-indentation))
7708 t)))
7709 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
7710 (when have-close-paren
7711 ;; Delete extra commentary
7712 (save-excursion
7713 (while (progn
7714 (forward-line -1)
7715 (looking-at "\\s *//\\s *\\(Outputs\\|Inouts\\|Inputs\\)\n"))
7716 (delete-region (match-beginning 0) (match-end 0))))
7717 ;; If it is simple, we can put the ); on the same line as the last text
7718 (let ((rtn-pt (point)))
7719 (save-excursion
7720 (while (progn (backward-char 1)
7721 (looking-at "[ \t\n\f]")))
7722 (when (looking-at ",")
7723 (delete-region (+ 1 (point)) rtn-pt))))
7724 (when (bolp)
7725 (indent-to indent))
7726 (insert ");\n")
7727 ;; Still need to kill final comma - always is one as we put one after the .*
7728 (re-search-backward ",")
7729 (delete-char 1))))))
7731 (defun verilog-delete-auto ()
7732 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
7733 Use \\[verilog-auto] to re-insert the updated AUTOs.
7735 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
7736 called before and after this function, respectively."
7737 (interactive)
7738 (save-excursion
7739 (if (buffer-file-name)
7740 (find-file-noselect (buffer-file-name))) ;; To check we have latest version
7741 ;; Allow user to customize
7742 (run-hooks 'verilog-before-delete-auto-hook)
7744 ;; Remove those that have multi-line insertions, possibly with parameters
7745 (verilog-auto-re-search-do
7746 (concat "/\\*"
7747 (eval-when-compile
7748 (verilog-regexp-words
7749 `("AUTOASCIIENUM" "AUTOCONCATCOMMENT" "AUTODEFINEVALUE"
7750 "AUTOINOUT" "AUTOINOUTMODULE" "AUTOINPUT" "AUTOOUTPUT"
7751 "AUTOOUTPUTEVERY"
7752 "AUTOREG" "AUTOREGINPUT" "AUTORESET" "AUTOTIEOFF"
7753 "AUTOUNUSED" "AUTOWIRE")))
7754 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\)" ; Optional parens or quoted parameter
7755 "\\*/")
7756 'verilog-delete-autos-lined)
7757 ;; Remove those that are in parenthesis
7758 (verilog-auto-re-search-do
7759 (concat "/\\*"
7760 (eval-when-compile
7761 (verilog-regexp-words
7762 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
7763 "AUTOSENSE")))
7764 "\\*/")
7765 'verilog-delete-to-paren)
7766 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
7767 (verilog-auto-re-search-do "\\.\\*"
7768 'verilog-delete-auto-star-all)
7769 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
7770 (goto-char (point-min))
7771 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)[ \tLT0-9]*$" nil t)
7772 (replace-match ""))
7774 ;; Final customize
7775 (run-hooks 'verilog-delete-auto-hook)))
7778 ;; Auto inject
7781 (defun verilog-inject-auto ()
7782 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
7784 Any always @ blocks with sensitivity lists that match computed lists will
7785 be replaced with /*AS*/ comments.
7787 Any cells will get /*AUTOINST*/ added to the end of the pin list.
7788 Pins with have identical names will be deleted.
7790 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
7791 support adding new ports. You may wish to delete older ports yourself.
7793 For example:
7795 module ExampInject (i, o);
7796 input i;
7797 input j;
7798 output o;
7799 always @ (i or j)
7800 o = i | j;
7801 InstModule instName
7802 (.foobar(baz),
7803 j(j));
7804 endmodule
7806 Typing \\[verilog-inject-auto] will make this into:
7808 module ExampInject (i, o/*AUTOARG*/
7809 // Inputs
7811 input i;
7812 output o;
7813 always @ (/*AS*/i or j)
7814 o = i | j;
7815 InstModule instName
7816 (.foobar(baz),
7817 /*AUTOINST*/
7818 // Outputs
7819 j(j));
7820 endmodule"
7821 (interactive)
7822 (verilog-auto t))
7824 (defun verilog-inject-arg ()
7825 "Inject AUTOARG into new code. See `verilog-inject-auto'."
7826 ;; Presume one module per file.
7827 (save-excursion
7828 (goto-char (point-min))
7829 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
7830 (let ((endmodp (save-excursion
7831 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
7832 (point))))
7833 ;; See if there's already a comment .. inside a comment so not verilog-re-search
7834 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
7835 (verilog-re-search-forward-quick ";" nil t)
7836 (backward-char 1)
7837 (verilog-backward-syntactic-ws)
7838 (backward-char 1) ; Moves to paren that closes argdecl's
7839 (when (looking-at ")")
7840 (insert "/*AUTOARG*/")))))))
7842 (defun verilog-inject-sense ()
7843 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
7844 (save-excursion
7845 (goto-char (point-min))
7846 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
7847 (let* ((start-pt (point))
7848 (modi (verilog-modi-current))
7849 (moddecls (verilog-modi-get-decls modi))
7850 pre-sigs
7851 got-sigs)
7852 (backward-char 1)
7853 (forward-sexp 1)
7854 (backward-char 1) ;; End )
7855 (when (not (verilog-re-search-backward "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
7856 (setq pre-sigs (verilog-signals-from-signame
7857 (verilog-read-signals start-pt (point)))
7858 got-sigs (verilog-auto-sense-sigs moddecls nil))
7859 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
7860 (verilog-signals-not-in got-sigs pre-sigs)))
7861 (delete-region start-pt (point))
7862 (insert "/*AS*/")))))))
7864 (defun verilog-inject-inst ()
7865 "Inject AUTOINST into new code. See `verilog-inject-auto'."
7866 (save-excursion
7867 (goto-char (point-min))
7868 ;; It's hard to distinguish modules; we'll instead search for pins.
7869 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_\$]+\\s *(\\s *[a-zA-Z0-9`_\$]+\\s *)" nil t)
7870 (verilog-backward-open-paren) ;; Inst start
7871 (cond
7872 ((= (preceding-char) ?\#) ;; #(...) parameter section, not pin. Skip.
7873 (forward-char 1)
7874 (verilog-forward-close-paren)) ;; Parameters done
7876 (forward-char 1)
7877 (let ((indent-pt (+ (current-column)))
7878 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
7879 (cond ((verilog-re-search-forward "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
7880 (goto-char end-pt)) ;; Already there, continue search with next instance
7882 ;; Delete identical interconnect
7883 (let ((case-fold-search nil)) ;; So we don't convert upper-to-lower, etc
7884 (while (verilog-re-search-forward "\\.\\s *\\([a-zA-Z0-9`_\$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
7885 (delete-region (match-beginning 0) (match-end 0))
7886 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ;; Keep it correct
7887 (while (or (looking-at "[ \t\n\f,]+")
7888 (looking-at "//[^\n]*"))
7889 (delete-region (match-beginning 0) (match-end 0))
7890 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
7891 (verilog-forward-close-paren)
7892 (backward-char 1)
7893 ;; Not verilog-re-search, as we don't want to strip comments
7894 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
7895 (delete-region (match-beginning 0) (match-end 0)))
7896 (insert "\n")
7897 (indent-to indent-pt)
7898 (insert "/*AUTOINST*/")))))))))
7901 ;; Auto save
7904 (defun verilog-auto-save-check ()
7905 "On saving see if we need auto update."
7906 (cond ((not verilog-auto-save-policy)) ; disabled
7907 ((not (save-excursion
7908 (save-match-data
7909 (let ((case-fold-search nil))
7910 (goto-char (point-min))
7911 (re-search-forward "AUTO" nil t))))))
7912 ((eq verilog-auto-save-policy 'force)
7913 (verilog-auto))
7914 ((not (buffer-modified-p)))
7915 ((eq verilog-auto-update-tick (buffer-modified-tick))) ; up-to-date
7916 ((eq verilog-auto-save-policy 'detect)
7917 (verilog-auto))
7919 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
7920 (verilog-auto))
7921 ;; Don't ask again if didn't update
7922 (set (make-local-variable 'verilog-auto-update-tick) (buffer-modified-tick))))
7923 (when (not verilog-auto-star-save)
7924 (verilog-delete-auto-star-implicit))
7925 nil) ;; Always return nil -- we don't write the file ourselves
7927 (defun verilog-auto-read-locals ()
7928 "Return file local variable segment at bottom of file."
7929 (save-excursion
7930 (goto-char (point-max))
7931 (if (re-search-backward "Local Variables:" nil t)
7932 (buffer-substring-no-properties (point) (point-max))
7933 "")))
7935 (defun verilog-auto-reeval-locals (&optional force)
7936 "Read file local variable segment at bottom of file if it has changed.
7937 If FORCE, always reread it."
7938 (make-local-variable 'verilog-auto-last-file-locals)
7939 (let ((curlocal (verilog-auto-read-locals)))
7940 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
7941 (setq verilog-auto-last-file-locals curlocal)
7942 ;; Note this may cause this function to be recursively invoked.
7943 ;; The above when statement will prevent it from recursing forever.
7944 (hack-local-variables)
7945 t)))
7948 ;; Auto creation
7951 (defun verilog-auto-arg-ports (sigs message indent-pt)
7952 "Print a list of ports for a AUTOINST.
7953 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
7954 (when sigs
7955 (insert "\n")
7956 (indent-to indent-pt)
7957 (insert message)
7958 (insert "\n")
7959 (let ((space ""))
7960 (indent-to indent-pt)
7961 (while sigs
7962 (cond ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
7963 (insert "\n")
7964 (indent-to indent-pt))
7965 (t (insert space)))
7966 (insert (verilog-sig-name (car sigs)) ",")
7967 (setq sigs (cdr sigs)
7968 space " ")))))
7970 (defun verilog-auto-arg ()
7971 "Expand AUTOARG statements.
7972 Replace the argument declarations at the beginning of the
7973 module with ones automatically derived from input and output
7974 statements. This can be dangerous if the module is instantiated
7975 using position-based connections, so use only name-based when
7976 instantiating the resulting module. Long lines are split based
7977 on the `fill-column', see \\[set-fill-column].
7979 Limitations:
7980 Concatenation and outputting partial busses is not supported.
7982 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
7984 For example:
7986 module ExampArg (/*AUTOARG*/);
7987 input i;
7988 output o;
7989 endmodule
7991 Typing \\[verilog-auto] will make this into:
7993 module ExampArg (/*AUTOARG*/
7994 // Outputs
7996 // Inputs
7999 input i;
8000 output o;
8001 endmodule
8003 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
8004 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
8005 conservative guess on adding a comma for the first signal, if you have
8006 any ifdefs or complicated expressions before the AUTOARG you will need
8007 to choose the comma yourself.
8009 Avoid declaring ports manually, as it makes code harder to maintain."
8010 (save-excursion
8011 (let* ((modi (verilog-modi-current))
8012 (moddecls (verilog-modi-get-decls modi))
8013 (skip-pins (aref (verilog-read-arg-pins) 0)))
8014 (verilog-repair-open-comma)
8015 (verilog-auto-arg-ports (verilog-signals-not-in
8016 (verilog-decls-get-outputs moddecls)
8017 skip-pins)
8018 "// Outputs"
8019 verilog-indent-level-declaration)
8020 (verilog-auto-arg-ports (verilog-signals-not-in
8021 (verilog-decls-get-inouts moddecls)
8022 skip-pins)
8023 "// Inouts"
8024 verilog-indent-level-declaration)
8025 (verilog-auto-arg-ports (verilog-signals-not-in
8026 (verilog-decls-get-inputs moddecls)
8027 skip-pins)
8028 "// Inputs"
8029 verilog-indent-level-declaration)
8030 (verilog-repair-close-comma)
8031 (unless (eq (char-before) ?/ )
8032 (insert "\n"))
8033 (indent-to verilog-indent-level-declaration))))
8035 (defun verilog-auto-inst-port-map (port-st)
8036 nil)
8038 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
8039 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
8040 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
8041 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
8042 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
8044 (defun verilog-auto-inst-port (port-st indent-pt tpl-list tpl-num for-star)
8045 "Print out a instantiation connection for this PORT-ST.
8046 Insert to INDENT-PT, use template TPL-LIST.
8047 @ are instantiation numbers, replaced with TPL-NUM.
8048 @\"(expression @)\" are evaluated, with @ as a variable.
8049 If FOR-STAR add comment it is a .* expansion."
8050 (let* ((port (verilog-sig-name port-st))
8051 (tpl-ass (or (assoc port (car tpl-list))
8052 (verilog-auto-inst-port-map port-st)))
8053 ;; vl-* are documented for user use
8054 (vl-name (verilog-sig-name port-st))
8055 (vl-width (verilog-sig-width port-st))
8056 (vl-bits (if (or verilog-auto-inst-vector
8057 (not (assoc port vector-skip-list))
8058 (not (equal (verilog-sig-bits port-st)
8059 (verilog-sig-bits (assoc port vector-skip-list)))))
8060 (or (verilog-sig-bits port-st) "")
8061 ""))
8062 ;; Default if not found
8063 (tpl-net (if (verilog-sig-multidim port-st)
8064 (concat port "/*" (verilog-sig-multidim-string port-st)
8065 vl-bits "*/")
8066 (concat port vl-bits)))
8067 (case-fold-search nil))
8068 ;; Find template
8069 (cond (tpl-ass ; Template of exact port name
8070 (setq tpl-net (nth 1 tpl-ass)))
8071 ((nth 1 tpl-list) ; Wildcards in template, search them
8072 (let ((wildcards (nth 1 tpl-list)))
8073 (while wildcards
8074 (when (string-match (nth 0 (car wildcards)) port)
8075 (setq tpl-ass (car wildcards) ; so allow @ parsing
8076 tpl-net (replace-match (nth 1 (car wildcards))
8077 t nil port)))
8078 (setq wildcards (cdr wildcards))))))
8079 ;; Parse Templated variable
8080 (when tpl-ass
8081 ;; Evaluate @"(lispcode)"
8082 (when (string-match "@\".*[^\\]\"" tpl-net)
8083 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
8084 (setq tpl-net
8085 (concat
8086 (substring tpl-net 0 (match-beginning 0))
8087 (save-match-data
8088 (let* ((expr (match-string 1 tpl-net))
8089 (value
8090 (progn
8091 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
8092 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
8093 (prin1 (eval (car (read-from-string expr)))
8094 (lambda (ch) ())))))
8095 (if (numberp value) (setq value (number-to-string value)))
8096 value))
8097 (substring tpl-net (match-end 0))))))
8098 ;; Replace @ and [] magic variables in final output
8099 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
8100 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
8101 (indent-to indent-pt)
8102 (insert "." port)
8103 (indent-to verilog-auto-inst-column)
8104 (insert "(" tpl-net "),")
8105 (cond (tpl-ass
8106 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
8107 verilog-auto-inst-column))
8108 (insert " // Templated")
8109 (when verilog-auto-inst-template-numbers
8110 (insert " T" (int-to-string (nth 2 tpl-ass))
8111 " L" (int-to-string (nth 3 tpl-ass)))))
8112 (for-star
8113 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
8114 verilog-auto-inst-column))
8115 (insert " // Implicit .\*"))) ;For some reason the . or * must be escaped...
8116 (insert "\n")))
8117 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
8118 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
8119 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
8121 (defun verilog-auto-inst-first ()
8122 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
8123 ;; Do we need a trailing comma?
8124 ;; There maybe a ifdef or something similar before us. What a mess. Thus
8125 ;; to avoid trouble we only insert on preceeding ) or *.
8126 ;; Insert first port on new line
8127 (insert "\n") ;; Must insert before search, so point will move forward if insert comma
8128 (save-excursion
8129 (verilog-re-search-backward "[^ \t\n\f]" nil nil)
8130 (when (looking-at ")\\|\\*") ;; Generally don't insert, unless we are fairly sure
8131 (forward-char 1)
8132 (insert ","))))
8134 (defun verilog-auto-star ()
8135 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
8137 If `verilog-auto-star-expand' is set, .* pins are treated if they were
8138 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
8139 will also ignore any .* that are not last in your pin list (this prevents
8140 it from deleting pins following the .* when it expands the AUTOINST.)
8142 On writing your file, unless `verilog-auto-star-save' is set, any
8143 non-templated expanded pins will be removed. You may do this at any time
8144 with \\[verilog-delete-auto-star-implicit].
8146 If you are converting a module to use .* for the first time, you may wish
8147 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
8149 See `verilog-auto-inst' for examples, templates, and more information."
8150 (when (verilog-auto-star-safe)
8151 (verilog-auto-inst)))
8153 (defun verilog-auto-inst ()
8154 "Expand AUTOINST statements, as part of \\[verilog-auto].
8155 Replace the pin connections to an instantiation with ones
8156 automatically derived from the module header of the instantiated netlist.
8158 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
8159 and delete them before saving unless `verilog-auto-star-save' is set.
8160 See `verilog-auto-star' for more information.
8162 Limitations:
8163 Module names must be resolvable to filenames by adding a
8164 `verilog-library-extensions', and being found in the same directory, or
8165 by changing the variable `verilog-library-flags' or
8166 `verilog-library-directories'. Macros `modname are translated through the
8167 vh-{name} Emacs variable, if that is not found, it just ignores the `.
8169 In templates you must have one signal per line, ending in a ), or ));,
8170 and have proper () nesting, including a final ); to end the template.
8172 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
8174 SystemVerilog multidimensional input/output has only experimental support.
8176 For example, first take the submodule InstModule.v:
8178 module InstModule (o,i)
8179 output [31:0] o;
8180 input i;
8181 wire [31:0] o = {32{i}};
8182 endmodule
8184 This is then used in a upper level module:
8186 module ExampInst (o,i)
8187 output o;
8188 input i;
8189 InstModule instName
8190 (/*AUTOINST*/);
8191 endmodule
8193 Typing \\[verilog-auto] will make this into:
8195 module ExampInst (o,i)
8196 output o;
8197 input i;
8198 InstModule instName
8199 (/*AUTOINST*/
8200 // Outputs
8201 .ov (ov[31:0]),
8202 // Inputs
8203 .i (i));
8204 endmodule
8206 Where the list of inputs and outputs came from the inst module.
8208 Exceptions:
8210 Unless you are instantiating a module multiple times, or the module is
8211 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
8212 It just makes for unmaintainable code. To sanitize signal names, try
8213 vrename from http://www.veripool.com.
8215 When you need to violate this suggestion there are two ways to list
8216 exceptions, placing them before the AUTOINST, or using templates.
8218 Any ports defined before the /*AUTOINST*/ are not included in the list of
8219 automatics. This is similar to making a template as described below, but
8220 is restricted to simple connections just like you normally make. Also note
8221 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
8222 you have the appropriate // Input or // Output comment, and exactly the
8223 same line formatting as AUTOINST itself uses.
8225 InstModule instName
8226 (// Inputs
8227 .i (my_i_dont_mess_with_it),
8228 /*AUTOINST*/
8229 // Outputs
8230 .ov (ov[31:0]));
8233 Templates:
8235 For multiple instantiations based upon a single template, create a
8236 commented out template:
8238 /* InstModule AUTO_TEMPLATE (
8239 .sig3 (sigz[]),
8243 Templates go ABOVE the instantiation(s). When an instantiation is
8244 expanded `verilog-mode' simply searches up for the closest template.
8245 Thus you can have multiple templates for the same module, just alternate
8246 between the template for an instantiation and the instantiation itself.
8248 The module name must be the same as the name of the module in the
8249 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
8250 words and capitalized. Only signals that must be different for each
8251 instantiation need to be listed.
8253 Inside a template, a [] in a connection name (with nothing else inside
8254 the brackets) will be replaced by the same bus subscript as it is being
8255 connected to, or the [] will be removed if it is a single bit signal.
8256 Generally it is a good idea to do this for all connections in a template,
8257 as then they will work for any width signal, and with AUTOWIRE. See
8258 PTL_BUS becoming PTL_BUSNEW below.
8260 If you have a complicated template, set `verilog-auto-inst-template-numbers'
8261 to see which regexps are matching. Don't leave that mode set after
8262 debugging is completed though, it will result in lots of extra differences
8263 and merge conflicts.
8265 For example:
8267 /* InstModule AUTO_TEMPLATE (
8268 .ptl_bus (ptl_busnew[]),
8271 InstModule ms2m (/*AUTOINST*/);
8273 Typing \\[verilog-auto] will make this into:
8275 InstModule ms2m (/*AUTOINST*/
8276 // Outputs
8277 .NotInTemplate (NotInTemplate),
8278 .ptl_bus (ptl_busnew[3:0]), // Templated
8279 ....
8281 @ Templates:
8283 It is common to instantiate a cell multiple times, so templates make it
8284 trivial to substitute part of the cell name into the connection name.
8286 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
8287 .sig1 (sigx[@]),
8288 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
8292 If no regular expression is provided immediately after the AUTO_TEMPLATE
8293 keyword, then the @ character in any connection names will be replaced
8294 with the instantiation number; the first digits found in the cell's
8295 instantiation name.
8297 If a regular expression is provided, the @ character will be replaced
8298 with the first \(\) grouping that matches against the cell name. Using a
8299 regexp of \"\\([0-9]+\\)\" provides identical values for @ as when no
8300 regexp is provided. If you use multiple layers of parenthesis,
8301 \"test\\([^0-9]+\\)_\\([0-9]+\\)\" would replace @ with non-number
8302 characters after test and before _, whereas
8303 \"\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)\" would replace @ with the entire
8304 match.
8306 For example:
8308 /* InstModule AUTO_TEMPLATE (
8309 .ptl_mapvalidx (ptl_mapvalid[@]),
8310 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
8313 InstModule ms2m (/*AUTOINST*/);
8315 Typing \\[verilog-auto] will make this into:
8317 InstModule ms2m (/*AUTOINST*/
8318 // Outputs
8319 .ptl_mapvalidx (ptl_mapvalid[2]),
8320 .ptl_mapvalidp1x (ptl_mapvalid[3]));
8322 Note the @ character was replaced with the 2 from \"ms2m\".
8324 Alternatively, using a regular expression for @:
8326 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
8327 .ptl_mapvalidx (@_ptl_mapvalid),
8328 .ptl_mapvalidp1x (ptl_mapvalid_@),
8331 InstModule ms2_FOO (/*AUTOINST*/);
8332 InstModule ms2_BAR (/*AUTOINST*/);
8334 Typing \\[verilog-auto] will make this into:
8336 InstModule ms2_FOO (/*AUTOINST*/
8337 // Outputs
8338 .ptl_mapvalidx (FOO_ptl_mapvalid),
8339 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
8340 InstModule ms2_BAR (/*AUTOINST*/
8341 // Outputs
8342 .ptl_mapvalidx (BAR_ptl_mapvalid),
8343 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
8346 Regexp Templates:
8348 A template entry of the form
8350 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
8352 will apply an Emacs style regular expression search for any port beginning
8353 in pci_req followed by numbers and ending in _l and connecting that to
8354 the pci_req_jtag_[] net, with the bus subscript coming from what matches
8355 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
8357 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
8358 does the same thing. (Note a @ in the connection/replacement text is
8359 completely different -- still use \\1 there!) Thus this is the same as
8360 the above template:
8362 .pci_req@_l (pci_req_jtag_[\\1]),
8364 Here's another example to remove the _l, useful when naming conventions
8365 specify _ alone to mean active low. Note the use of [] to keep the bus
8366 subscript:
8368 .\\(.*\\)_l (\\1_[]),
8370 Lisp Templates:
8372 First any regular expression template is expanded.
8374 If the syntax @\"( ... )\" is found in a connection, the expression in
8375 quotes will be evaluated as a Lisp expression, with @ replaced by the
8376 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
8377 4 into the brackets. Quote all double-quotes inside the expression with
8378 a leading backslash (\\\"). There are special variables defined that are
8379 useful in these Lisp functions:
8381 vl-name Name portion of the input/output port.
8382 vl-bits Bus bits portion of the input/output port ('[2:0]').
8383 vl-width Width of the input/output port ('3' for [2:0]).
8384 May be a (...) expression if bits isn't a constant.
8385 vl-dir Direction of the pin input/output/inout.
8386 vl-cell-type Module name/type of the cell ('InstModule').
8387 vl-cell-name Instance name of the cell ('instName').
8389 Normal Lisp variables may be used in expressions. See
8390 `verilog-read-defines' which can set vh-{definename} variables for use
8391 here. Also, any comments of the form:
8393 /*AUTO_LISP(setq foo 1)*/
8395 will evaluate any Lisp expression inside the parenthesis between the
8396 beginning of the buffer and the point of the AUTOINST. This allows
8397 functions to be defined or variables to be changed between instantiations.
8399 Note that when using lisp expressions errors may occur when @ is not a
8400 number; you may need to use the standard Emacs Lisp functions
8401 `number-to-string' and `string-to-number'.
8403 After the evaluation is completed, @ substitution and [] substitution
8404 occur."
8405 (save-excursion
8406 ;; Find beginning
8407 (let* ((pt (point))
8408 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
8409 (indent-pt (save-excursion (verilog-backward-open-paren)
8410 (1+ (current-column))))
8411 (verilog-auto-inst-column (max verilog-auto-inst-column
8412 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
8413 (modi (verilog-modi-current))
8414 (moddecls (verilog-modi-get-decls modi))
8415 (vector-skip-list (unless verilog-auto-inst-vector
8416 (verilog-decls-get-signals moddecls)))
8417 submod submodi submoddecls
8418 inst skip-pins tpl-list tpl-num did-first)
8419 ;; Find module name that is instantiated
8420 (setq submod (verilog-read-inst-module)
8421 inst (verilog-read-inst-name)
8422 vl-cell-type submod
8423 vl-cell-name inst
8424 skip-pins (aref (verilog-read-inst-pins) 0))
8426 ;; Parse any AUTO_LISP() before here
8427 (verilog-read-auto-lisp (point-min) pt)
8429 ;; Lookup position, etc of submodule
8430 ;; Note this may raise an error
8431 (when (setq submodi (verilog-modi-lookup submod t))
8432 (setq submoddecls (verilog-modi-get-decls submodi))
8433 ;; If there's a number in the instantiation, it may be a argument to the
8434 ;; automatic variable instantiation program.
8435 (let* ((tpl-info (verilog-read-auto-template submod))
8436 (tpl-regexp (aref tpl-info 0)))
8437 (setq tpl-num (if (string-match tpl-regexp inst)
8438 (match-string 1 inst)
8440 tpl-list (aref tpl-info 1)))
8441 ;; Find submodule's signals and dump
8442 (let ((sig-list (verilog-signals-not-in
8443 (verilog-decls-get-outputs submoddecls)
8444 skip-pins))
8445 (vl-dir "output"))
8446 (when sig-list
8447 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
8448 (indent-to indent-pt)
8449 ;; Note these are searched for in verilog-read-sub-decls.
8450 (insert "// Outputs\n")
8451 (mapc (lambda (port)
8452 (verilog-auto-inst-port port indent-pt
8453 tpl-list tpl-num for-star))
8454 sig-list)))
8455 (let ((sig-list (verilog-signals-not-in
8456 (verilog-decls-get-inouts submoddecls)
8457 skip-pins))
8458 (vl-dir "inout"))
8459 (when sig-list
8460 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
8461 (indent-to indent-pt)
8462 (insert "// Inouts\n")
8463 (mapc (lambda (port)
8464 (verilog-auto-inst-port port indent-pt
8465 tpl-list tpl-num for-star))
8466 sig-list)))
8467 (let ((sig-list (verilog-signals-not-in
8468 (verilog-decls-get-inputs submoddecls)
8469 skip-pins))
8470 (vl-dir "input"))
8471 (when sig-list
8472 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
8473 (indent-to indent-pt)
8474 (insert "// Inputs\n")
8475 (mapc (lambda (port)
8476 (verilog-auto-inst-port port indent-pt
8477 tpl-list tpl-num for-star))
8478 sig-list)))
8479 ;; Kill extra semi
8480 (save-excursion
8481 (cond (did-first
8482 (re-search-backward "," pt t)
8483 (delete-char 1)
8484 (insert ");")
8485 (search-forward "\n") ;; Added by inst-port
8486 (delete-backward-char 1)
8487 (if (search-forward ")" nil t) ;; From user, moved up a line
8488 (delete-backward-char 1))
8489 (if (search-forward ";" nil t) ;; Don't error if user had syntax error and forgot it
8490 (delete-backward-char 1)))))))))
8492 (defun verilog-auto-inst-param ()
8493 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
8494 Replace the parameter connections to an instantiation with ones
8495 automatically derived from the module header of the instantiated netlist.
8497 See \\[verilog-auto-inst] for limitations, and templates to customize the
8498 output.
8500 For example, first take the submodule InstModule.v:
8502 module InstModule (o,i)
8503 parameter PAR;
8504 endmodule
8506 This is then used in a upper level module:
8508 module ExampInst (o,i)
8509 parameter PAR;
8510 InstModule #(/*AUTOINSTPARAM*/)
8511 instName (/*AUTOINST*/);
8512 endmodule
8514 Typing \\[verilog-auto] will make this into:
8516 module ExampInst (o,i)
8517 output o;
8518 input i;
8519 InstModule #(/*AUTOINSTPARAM*/
8520 // Parameters
8521 .PAR (PAR));
8522 instName (/*AUTOINST*/);
8523 endmodule
8525 Where the list of parameter connections come from the inst module.
8527 Templates:
8529 You can customize the parameter connections using AUTO_TEMPLATEs,
8530 just as you would with \\[verilog-auto-inst]."
8531 (save-excursion
8532 ;; Find beginning
8533 (let* ((pt (point))
8534 (indent-pt (save-excursion (verilog-backward-open-paren)
8535 (1+ (current-column))))
8536 (verilog-auto-inst-column (max verilog-auto-inst-column
8537 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
8538 (modi (verilog-modi-current))
8539 (moddecls (verilog-modi-get-decls modi))
8540 (vector-skip-list (unless verilog-auto-inst-vector
8541 (verilog-decls-get-signals moddecls)))
8542 submod submodi submoddecls
8543 inst skip-pins tpl-list tpl-num did-first)
8544 ;; Find module name that is instantiated
8545 (setq submod (save-excursion
8546 ;; Get to the point where AUTOINST normally is to read the module
8547 (verilog-re-search-forward-quick "[(;]" nil nil)
8548 (verilog-read-inst-module))
8549 inst (save-excursion
8550 ;; Get to the point where AUTOINST normally is to read the module
8551 (verilog-re-search-forward-quick "[(;]" nil nil)
8552 (verilog-read-inst-name))
8553 vl-cell-type submod
8554 vl-cell-name inst
8555 skip-pins (aref (verilog-read-inst-pins) 0))
8557 ;; Parse any AUTO_LISP() before here
8558 (verilog-read-auto-lisp (point-min) pt)
8560 ;; Lookup position, etc of submodule
8561 ;; Note this may raise an error
8562 (when (setq submodi (verilog-modi-lookup submod t))
8563 (setq submoddecls (verilog-modi-get-decls submodi))
8564 ;; If there's a number in the instantiation, it may be a argument to the
8565 ;; automatic variable instantiation program.
8566 (let* ((tpl-info (verilog-read-auto-template submod))
8567 (tpl-regexp (aref tpl-info 0)))
8568 (setq tpl-num (if (string-match tpl-regexp inst)
8569 (match-string 1 inst)
8571 tpl-list (aref tpl-info 1)))
8572 ;; Find submodule's signals and dump
8573 (let ((sig-list (verilog-signals-not-in
8574 (verilog-decls-get-gparams submoddecls)
8575 skip-pins))
8576 (vl-dir "parameter"))
8577 (when sig-list
8578 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
8579 (indent-to indent-pt)
8580 ;; Note these are searched for in verilog-read-sub-decls.
8581 (insert "// Parameters\n")
8582 (mapc (lambda (port)
8583 (verilog-auto-inst-port port indent-pt
8584 tpl-list tpl-num nil))
8585 sig-list)))
8586 ;; Kill extra semi
8587 (save-excursion
8588 (cond (did-first
8589 (re-search-backward "," pt t)
8590 (delete-char 1)
8591 (insert ")")
8592 (search-forward "\n") ;; Added by inst-port
8593 (delete-backward-char 1)
8594 (if (search-forward ")" nil t) ;; From user, moved up a line
8595 (delete-backward-char 1)))))))))
8597 (defun verilog-auto-reg ()
8598 "Expand AUTOREG statements, as part of \\[verilog-auto].
8599 Make reg statements for any output that isn't already declared,
8600 and isn't a wire output from a block.
8602 Limitations:
8603 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
8605 This does NOT work on memories, declare those yourself.
8607 An example:
8609 module ExampReg (o,i)
8610 output o;
8611 input i;
8612 /*AUTOREG*/
8613 always o = i;
8614 endmodule
8616 Typing \\[verilog-auto] will make this into:
8618 module ExampReg (o,i)
8619 output o;
8620 input i;
8621 /*AUTOREG*/
8622 // Beginning of automatic regs (for this module's undeclared outputs)
8623 reg o;
8624 // End of automatics
8625 always o = i;
8626 endmodule"
8627 (save-excursion
8628 ;; Point must be at insertion point.
8629 (let* ((indent-pt (current-indentation))
8630 (modi (verilog-modi-current))
8631 (moddecls (verilog-modi-get-decls modi))
8632 (modsubdecls (verilog-modi-get-sub-decls modi))
8633 (sig-list (verilog-signals-not-in
8634 (verilog-decls-get-outputs moddecls)
8635 (append (verilog-decls-get-wires moddecls)
8636 (verilog-decls-get-regs moddecls)
8637 (verilog-decls-get-assigns moddecls)
8638 (verilog-decls-get-consts moddecls)
8639 (verilog-decls-get-gparams moddecls)
8640 (verilog-subdecls-get-outputs modsubdecls)
8641 (verilog-subdecls-get-inouts modsubdecls)))))
8642 (forward-line 1)
8643 (when sig-list
8644 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
8645 (verilog-insert-definition sig-list "reg" indent-pt nil)
8646 (verilog-modi-cache-add-regs modi sig-list)
8647 (verilog-insert-indent "// End of automatics\n")))))
8649 (defun verilog-auto-reg-input ()
8650 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
8651 Make reg statements instantiation inputs that aren't already declared.
8652 This is useful for making a top level shell for testing the module that is
8653 to be instantiated.
8655 Limitations:
8656 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
8658 This does NOT work on memories, declare those yourself.
8660 An example (see `verilog-auto-inst' for what else is going on here):
8662 module ExampRegInput (o,i)
8663 output o;
8664 input i;
8665 /*AUTOREGINPUT*/
8666 InstModule instName
8667 (/*AUTOINST*/);
8668 endmodule
8670 Typing \\[verilog-auto] will make this into:
8672 module ExampRegInput (o,i)
8673 output o;
8674 input i;
8675 /*AUTOREGINPUT*/
8676 // Beginning of automatic reg inputs (for undeclared ...
8677 reg [31:0] iv; // From inst of inst.v
8678 // End of automatics
8679 InstModule instName
8680 (/*AUTOINST*/
8681 // Outputs
8682 .o (o[31:0]),
8683 // Inputs
8684 .iv (iv));
8685 endmodule"
8686 (save-excursion
8687 ;; Point must be at insertion point.
8688 (let* ((indent-pt (current-indentation))
8689 (modi (verilog-modi-current))
8690 (moddecls (verilog-modi-get-decls modi))
8691 (modsubdecls (verilog-modi-get-sub-decls modi))
8692 (sig-list (verilog-signals-combine-bus
8693 (verilog-signals-not-in
8694 (append (verilog-subdecls-get-inputs modsubdecls)
8695 (verilog-subdecls-get-inouts modsubdecls))
8696 (verilog-decls-get-signals moddecls)))))
8697 (forward-line 1)
8698 (when sig-list
8699 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
8700 (verilog-insert-definition sig-list "reg" indent-pt nil)
8701 (verilog-modi-cache-add-regs modi sig-list)
8702 (verilog-insert-indent "// End of automatics\n")))))
8704 (defun verilog-auto-wire ()
8705 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
8706 Make wire statements for instantiations outputs that aren't
8707 already declared.
8709 Limitations:
8710 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
8711 and all busses must have widths, such as those from AUTOINST, or using []
8712 in AUTO_TEMPLATEs.
8714 This does NOT work on memories or SystemVerilog .name connections,
8715 declare those yourself.
8717 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
8718 determine how to bus together. This occurs when you have ports with
8719 non-numeric or non-sequential bus subscripts. If Verilog mode
8720 mis-guessed, you'll have to declare them yourself.
8722 An example (see `verilog-auto-inst' for what else is going on here):
8724 module ExampWire (o,i)
8725 output o;
8726 input i;
8727 /*AUTOWIRE*/
8728 InstModule instName
8729 (/*AUTOINST*/);
8730 endmodule
8732 Typing \\[verilog-auto] will make this into:
8734 module ExampWire (o,i)
8735 output o;
8736 input i;
8737 /*AUTOWIRE*/
8738 // Beginning of automatic wires
8739 wire [31:0] ov; // From inst of inst.v
8740 // End of automatics
8741 InstModule instName
8742 (/*AUTOINST*/
8743 // Outputs
8744 .ov (ov[31:0]),
8745 // Inputs
8746 .i (i));
8747 wire o = | ov;
8748 endmodule"
8749 (save-excursion
8750 ;; Point must be at insertion point.
8751 (let* ((indent-pt (current-indentation))
8752 (modi (verilog-modi-current))
8753 (moddecls (verilog-modi-get-decls modi))
8754 (modsubdecls (verilog-modi-get-sub-decls modi))
8755 (sig-list (verilog-signals-combine-bus
8756 (verilog-signals-not-in
8757 (append (verilog-subdecls-get-outputs modsubdecls)
8758 (verilog-subdecls-get-inouts modsubdecls))
8759 (verilog-decls-get-signals moddecls)))))
8760 (forward-line 1)
8761 (when sig-list
8762 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
8763 (verilog-insert-definition sig-list "wire" indent-pt nil)
8764 (verilog-modi-cache-add-wires modi sig-list)
8765 (verilog-insert-indent "// End of automatics\n")
8766 (when nil ;; Too slow on huge modules, plus makes everyone's module change
8767 (beginning-of-line)
8768 (setq pnt (point))
8769 (verilog-pretty-declarations quiet)
8770 (goto-char pnt)
8771 (verilog-pretty-expr "//"))))))
8773 (defun verilog-auto-output (&optional with-params)
8774 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
8775 Make output statements for any output signal from an /*AUTOINST*/ that
8776 isn't a input to another AUTOINST. This is useful for modules which
8777 only instantiate other modules.
8779 Limitations:
8780 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
8782 If placed inside the parenthesis of a module declaration, it creates
8783 Verilog 2001 style, else uses Verilog 1995 style.
8785 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
8786 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
8788 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
8790 Signals matching `verilog-auto-output-ignore-regexp' are not included.
8792 An example (see `verilog-auto-inst' for what else is going on here):
8794 module ExampOutput (ov,i)
8795 input i;
8796 /*AUTOOUTPUT*/
8797 InstModule instName
8798 (/*AUTOINST*/);
8799 endmodule
8801 Typing \\[verilog-auto] will make this into:
8803 module ExampOutput (ov,i)
8804 input i;
8805 /*AUTOOUTPUT*/
8806 // Beginning of automatic outputs (from unused autoinst outputs)
8807 output [31:0] ov; // From inst of inst.v
8808 // End of automatics
8809 InstModule instName
8810 (/*AUTOINST*/
8811 // Outputs
8812 .ov (ov[31:0]),
8813 // Inputs
8814 .i (i));
8815 endmodule
8817 You may also provide an optional regular expression, in which case only
8818 signals matching the regular expression will be included. For example the
8819 same expansion will result from only extracting outputs starting with ov:
8821 /*AUTOOUTPUT(\"^ov\")*/"
8822 (save-excursion
8823 ;; Point must be at insertion point.
8824 (let* ((indent-pt (current-indentation))
8825 (regexp (and with-params
8826 (nth 0 (verilog-read-auto-params 1))))
8827 (v2k (verilog-in-paren))
8828 (modi (verilog-modi-current))
8829 (moddecls (verilog-modi-get-decls modi))
8830 (modsubdecls (verilog-modi-get-sub-decls modi))
8831 (sig-list (verilog-signals-not-in
8832 (verilog-subdecls-get-outputs modsubdecls)
8833 (append (verilog-decls-get-outputs moddecls)
8834 (verilog-decls-get-inouts moddecls)
8835 (verilog-subdecls-get-inputs modsubdecls)
8836 (verilog-subdecls-get-inouts modsubdecls)))))
8837 (when regexp
8838 (setq sig-list (verilog-signals-matching-regexp
8839 sig-list regexp)))
8840 (setq sig-list (verilog-signals-not-matching-regexp
8841 sig-list verilog-auto-output-ignore-regexp))
8842 (forward-line 1)
8843 (when v2k (verilog-repair-open-comma))
8844 (when sig-list
8845 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
8846 (verilog-insert-definition sig-list "output" indent-pt v2k)
8847 (verilog-modi-cache-add-outputs modi sig-list)
8848 (verilog-insert-indent "// End of automatics\n"))
8849 (when v2k (verilog-repair-close-comma)))))
8851 (defun verilog-auto-output-every ()
8852 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
8853 Make output statements for any signals that aren't primary inputs or
8854 outputs already. This makes every signal in the design a output. This is
8855 useful to get Synopsys to preserve every signal in the design, since it
8856 won't optimize away the outputs.
8858 An example:
8860 module ExampOutputEvery (o,i,tempa,tempb)
8861 output o;
8862 input i;
8863 /*AUTOOUTPUTEVERY*/
8864 wire tempa = i;
8865 wire tempb = tempa;
8866 wire o = tempb;
8867 endmodule
8869 Typing \\[verilog-auto] will make this into:
8871 module ExampOutputEvery (o,i,tempa,tempb)
8872 output o;
8873 input i;
8874 /*AUTOOUTPUTEVERY*/
8875 // Beginning of automatic outputs (every signal)
8876 output tempb;
8877 output tempa;
8878 // End of automatics
8879 wire tempa = i;
8880 wire tempb = tempa;
8881 wire o = tempb;
8882 endmodule"
8883 (save-excursion
8884 ;;Point must be at insertion point
8885 (let* ((indent-pt (current-indentation))
8886 (v2k (verilog-in-paren))
8887 (modi (verilog-modi-current))
8888 (moddecls (verilog-modi-get-decls modi))
8889 (sig-list (verilog-signals-combine-bus
8890 (verilog-signals-not-in
8891 (verilog-decls-get-signals moddecls)
8892 (verilog-decls-get-ports moddecls)))))
8893 (forward-line 1)
8894 (when v2k (verilog-repair-open-comma))
8895 (when sig-list
8896 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
8897 (verilog-insert-definition sig-list "output" indent-pt v2k)
8898 (verilog-modi-cache-add-outputs modi sig-list)
8899 (verilog-insert-indent "// End of automatics\n"))
8900 (when v2k (verilog-repair-close-comma)))))
8902 (defun verilog-auto-input (&optional with-params)
8903 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
8904 Make input statements for any input signal into an /*AUTOINST*/ that
8905 isn't declared elsewhere inside the module. This is useful for modules which
8906 only instantiate other modules.
8908 Limitations:
8909 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
8911 If placed inside the parenthesis of a module declaration, it creates
8912 Verilog 2001 style, else uses Verilog 1995 style.
8914 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
8915 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
8917 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
8919 Signals matching `verilog-auto-input-ignore-regexp' are not included.
8921 An example (see `verilog-auto-inst' for what else is going on here):
8923 module ExampInput (ov,i)
8924 output [31:0] ov;
8925 /*AUTOINPUT*/
8926 InstModule instName
8927 (/*AUTOINST*/);
8928 endmodule
8930 Typing \\[verilog-auto] will make this into:
8932 module ExampInput (ov,i)
8933 output [31:0] ov;
8934 /*AUTOINPUT*/
8935 // Beginning of automatic inputs (from unused autoinst inputs)
8936 input i; // From inst of inst.v
8937 // End of automatics
8938 InstModule instName
8939 (/*AUTOINST*/
8940 // Outputs
8941 .ov (ov[31:0]),
8942 // Inputs
8943 .i (i));
8944 endmodule
8946 You may also provide an optional regular expression, in which case only
8947 signals matching the regular expression will be included. For example the
8948 same expansion will result from only extracting inputs starting with i:
8950 /*AUTOINPUT(\"^i\")*/"
8951 (save-excursion
8952 (let* ((indent-pt (current-indentation))
8953 (regexp (and with-params
8954 (nth 0 (verilog-read-auto-params 1))))
8955 (v2k (verilog-in-paren))
8956 (modi (verilog-modi-current))
8957 (moddecls (verilog-modi-get-decls modi))
8958 (modsubdecls (verilog-modi-get-sub-decls modi))
8959 (sig-list (verilog-signals-not-in
8960 (verilog-subdecls-get-inputs modsubdecls)
8961 (append (verilog-decls-get-inputs moddecls)
8962 (verilog-decls-get-inouts moddecls)
8963 (verilog-decls-get-wires moddecls)
8964 (verilog-decls-get-regs moddecls)
8965 (verilog-decls-get-consts moddecls)
8966 (verilog-decls-get-gparams moddecls)
8967 (verilog-subdecls-get-outputs modsubdecls)
8968 (verilog-subdecls-get-inouts modsubdecls)))))
8969 (when regexp
8970 (setq sig-list (verilog-signals-matching-regexp
8971 sig-list regexp)))
8972 (setq sig-list (verilog-signals-not-matching-regexp
8973 sig-list verilog-auto-input-ignore-regexp))
8974 (forward-line 1)
8975 (when v2k (verilog-repair-open-comma))
8976 (when sig-list
8977 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
8978 (verilog-insert-definition sig-list "input" indent-pt v2k)
8979 (verilog-modi-cache-add-inputs modi sig-list)
8980 (verilog-insert-indent "// End of automatics\n"))
8981 (when v2k (verilog-repair-close-comma)))))
8983 (defun verilog-auto-inout (&optional with-params)
8984 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
8985 Make inout statements for any inout signal in an /*AUTOINST*/ that
8986 isn't declared elsewhere inside the module.
8988 Limitations:
8989 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
8991 If placed inside the parenthesis of a module declaration, it creates
8992 Verilog 2001 style, else uses Verilog 1995 style.
8994 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
8995 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
8997 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
8999 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
9001 An example (see `verilog-auto-inst' for what else is going on here):
9003 module ExampInout (ov,i)
9004 input i;
9005 /*AUTOINOUT*/
9006 InstModule instName
9007 (/*AUTOINST*/);
9008 endmodule
9010 Typing \\[verilog-auto] will make this into:
9012 module ExampInout (ov,i)
9013 input i;
9014 /*AUTOINOUT*/
9015 // Beginning of automatic inouts (from unused autoinst inouts)
9016 inout [31:0] ov; // From inst of inst.v
9017 // End of automatics
9018 InstModule instName
9019 (/*AUTOINST*/
9020 // Inouts
9021 .ov (ov[31:0]),
9022 // Inputs
9023 .i (i));
9024 endmodule
9026 You may also provide an optional regular expression, in which case only
9027 signals matching the regular expression will be included. For example the
9028 same expansion will result from only extracting inouts starting with i:
9030 /*AUTOINOUT(\"^i\")*/"
9031 (save-excursion
9032 ;; Point must be at insertion point.
9033 (let* ((indent-pt (current-indentation))
9034 (regexp (and with-params
9035 (nth 0 (verilog-read-auto-params 1))))
9036 (v2k (verilog-in-paren))
9037 (modi (verilog-modi-current))
9038 (moddecls (verilog-modi-get-decls modi))
9039 (modsubdecls (verilog-modi-get-sub-decls modi))
9040 (sig-list (verilog-signals-not-in
9041 (verilog-subdecls-get-inouts modsubdecls)
9042 (append (verilog-decls-get-outputs moddecls)
9043 (verilog-decls-get-inouts moddecls)
9044 (verilog-decls-get-inputs moddecls)
9045 (verilog-subdecls-get-inputs modsubdecls)
9046 (verilog-subdecls-get-outputs modsubdecls)))))
9047 (when regexp
9048 (setq sig-list (verilog-signals-matching-regexp
9049 sig-list regexp)))
9050 (setq sig-list (verilog-signals-not-matching-regexp
9051 sig-list verilog-auto-inout-ignore-regexp))
9052 (forward-line 1)
9053 (when v2k (verilog-repair-open-comma))
9054 (when sig-list
9055 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
9056 (verilog-insert-definition sig-list "inout" indent-pt v2k)
9057 (verilog-modi-cache-add-inouts modi sig-list)
9058 (verilog-insert-indent "// End of automatics\n"))
9059 (when v2k (verilog-repair-close-comma)))))
9061 (defun verilog-auto-inout-module ()
9062 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
9063 Take input/output/inout statements from the specified module and insert
9064 into the current module. This is useful for making null templates and
9065 shell modules which need to have identical I/O with another module.
9066 Any I/O which are already defined in this module will not be redefined.
9068 Limitations:
9069 If placed inside the parenthesis of a module declaration, it creates
9070 Verilog 2001 style, else uses Verilog 1995 style.
9072 Concatenation and outputting partial busses is not supported.
9074 Module names must be resolvable to filenames. See `verilog-auto-inst'.
9076 Signals are not inserted in the same order as in the original module,
9077 though they will appear to be in the same order to a AUTOINST
9078 instantiating either module.
9080 An example:
9082 module ExampShell (/*AUTOARG*/)
9083 /*AUTOINOUTMODULE(\"ExampMain\")*/
9084 endmodule
9086 module ExampMain (i,o,io)
9087 input i;
9088 output o;
9089 inout io;
9090 endmodule
9092 Typing \\[verilog-auto] will make this into:
9094 module ExampShell (/*AUTOARG*/i,o,io)
9095 /*AUTOINOUTMODULE(\"ExampMain\")*/
9096 // Beginning of automatic in/out/inouts (from specific module)
9097 input i;
9098 output o;
9099 inout io;
9100 // End of automatics
9101 endmodule
9103 You may also provide an optional regular expression, in which case only
9104 signals matching the regular expression will be included. For example the
9105 same expansion will result from only extracting signals starting with i:
9107 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/"
9108 (save-excursion
9109 (let* ((params (verilog-read-auto-params 1 2))
9110 (submod (nth 0 params))
9111 (regexp (nth 1 params))
9112 submodi)
9113 ;; Lookup position, etc of co-module
9114 ;; Note this may raise an error
9115 (when (setq submodi (verilog-modi-lookup submod t))
9116 (let* ((indent-pt (current-indentation))
9117 (v2k (verilog-in-paren))
9118 (modi (verilog-modi-current))
9119 (moddecls (verilog-modi-get-decls modi))
9120 (submoddecls (verilog-modi-get-decls submodi))
9121 (sig-list-i (verilog-signals-not-in
9122 (verilog-decls-get-inputs submoddecls)
9123 (append (verilog-decls-get-inputs moddecls))))
9124 (sig-list-o (verilog-signals-not-in
9125 (verilog-decls-get-outputs submoddecls)
9126 (append (verilog-decls-get-outputs moddecls))))
9127 (sig-list-io (verilog-signals-not-in
9128 (verilog-decls-get-inouts submoddecls)
9129 (append (verilog-decls-get-inouts moddecls)))))
9130 (forward-line 1)
9131 (when regexp
9132 (setq sig-list-i (verilog-signals-matching-regexp
9133 sig-list-i regexp)
9134 sig-list-o (verilog-signals-matching-regexp
9135 sig-list-o regexp)
9136 sig-list-io (verilog-signals-matching-regexp
9137 sig-list-io regexp)))
9138 (when v2k (verilog-repair-open-comma))
9139 (when (or sig-list-i sig-list-o sig-list-io)
9140 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
9141 ;; Don't sort them so a upper AUTOINST will match the main module
9142 (verilog-insert-definition sig-list-o "output" indent-pt v2k t)
9143 (verilog-insert-definition sig-list-io "inout" indent-pt v2k t)
9144 (verilog-insert-definition sig-list-i "input" indent-pt v2k t)
9145 (verilog-modi-cache-add-inputs modi sig-list-i)
9146 (verilog-modi-cache-add-outputs modi sig-list-o)
9147 (verilog-modi-cache-add-inouts modi sig-list-io)
9148 (verilog-insert-indent "// End of automatics\n"))
9149 (when v2k (verilog-repair-close-comma)))))))
9151 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
9152 "Return list of signals for current AUTOSENSE block."
9153 (let* ((sigss (verilog-read-always-signals))
9154 (sig-list (verilog-signals-not-params
9155 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
9156 (append (and (not verilog-auto-sense-include-inputs)
9157 (verilog-alw-get-outputs sigss))
9158 (verilog-decls-get-consts moddecls)
9159 (verilog-decls-get-gparams moddecls)
9160 presense-sigs)))))
9161 sig-list))
9163 (defun verilog-auto-sense ()
9164 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
9165 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
9166 with one automatically derived from all inputs declared in the always
9167 statement. Signals that are generated within the same always block are NOT
9168 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
9169 Long lines are split based on the `fill-column', see \\[set-fill-column].
9171 Limitations:
9172 Verilog does not allow memories (multidimensional arrays) in sensitivity
9173 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
9175 Constant signals:
9176 AUTOSENSE cannot always determine if a `define is a constant or a signal
9177 (it could be in a include file for example). If a `define or other signal
9178 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
9179 declaration anywhere in the module (parenthesis are required):
9181 /* AUTO_CONSTANT ( `this_is_really_constant_dont_autosense_it ) */
9183 Better yet, use a parameter, which will be understood to be constant
9184 automatically.
9186 OOps!
9187 If AUTOSENSE makes a mistake, please report it. (First try putting
9188 a begin/end after your always!) As a workaround, if a signal that
9189 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
9190 If a signal should be in the sensitivity list wasn't, placing it before
9191 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
9192 autos are updated (or added if it occurs there already).
9194 An example:
9196 always @ (/*AS*/) begin
9197 /* AUTO_CONSTANT (`constant) */
9198 outin = ina | inb | `constant;
9199 out = outin;
9202 Typing \\[verilog-auto] will make this into:
9204 always @ (/*AS*/ina or inb) begin
9205 /* AUTO_CONSTANT (`constant) */
9206 outin = ina | inb | `constant;
9207 out = outin;
9210 Note in Verilog 2001, you can often get the same result from the new @*
9211 operator. (This was added to the language in part due to AUTOSENSE!)
9213 always @* begin
9214 outin = ina | inb | `constant;
9215 out = outin;
9216 end"
9217 (save-excursion
9218 ;; Find beginning
9219 (let* ((start-pt (save-excursion
9220 (verilog-re-search-backward "(" nil t)
9221 (point)))
9222 (indent-pt (save-excursion
9223 (or (and (goto-char start-pt) (1+ (current-column)))
9224 (current-indentation))))
9225 (modi (verilog-modi-current))
9226 (moddecls (verilog-modi-get-decls modi))
9227 (sig-memories (verilog-signals-memory
9228 (append
9229 (verilog-decls-get-regs moddecls)
9230 (verilog-decls-get-wires moddecls))))
9231 sig-list not-first presense-sigs)
9232 ;; Read signals in always, eliminate outputs from sense list
9233 (setq presense-sigs (verilog-signals-from-signame
9234 (save-excursion
9235 (verilog-read-signals start-pt (point)))))
9236 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
9237 (when sig-memories
9238 (let ((tlen (length sig-list)))
9239 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
9240 (if (not (eq tlen (length sig-list))) (insert " /*memory or*/ "))))
9241 (if (and presense-sigs ;; Add a "or" if not "(.... or /*AUTOSENSE*/"
9242 (save-excursion (goto-char (point))
9243 (verilog-re-search-backward "[a-zA-Z0-9$_.%`]+" start-pt t)
9244 (verilog-re-search-backward "\\s-" start-pt t)
9245 (while (looking-at "\\s-`endif")
9246 (verilog-re-search-backward "[a-zA-Z0-9$_.%`]+" start-pt t)
9247 (verilog-re-search-backward "\\s-" start-pt t))
9248 (not (looking-at "\\s-or\\b"))))
9249 (setq not-first t))
9250 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
9251 (while sig-list
9252 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
9253 (insert "\n")
9254 (indent-to indent-pt)
9255 (if not-first (insert "or ")))
9256 (not-first (insert " or ")))
9257 (insert (verilog-sig-name (car sig-list)))
9258 (setq sig-list (cdr sig-list)
9259 not-first t)))))
9261 (defun verilog-auto-reset ()
9262 "Expand AUTORESET statements, as part of \\[verilog-auto].
9263 Replace the /*AUTORESET*/ comment with code to initialize all
9264 registers set elsewhere in the always block.
9266 Limitations:
9267 AUTORESET will not clear memories.
9269 AUTORESET uses <= if there are any <= in the block, else it uses =.
9271 /*AUTORESET*/ presumes that any signals mentioned between the previous
9272 begin/case/if statement and the AUTORESET comment are being reset manually
9273 and should not be automatically reset. This includes omitting any signals
9274 used on the right hand side of assignments.
9276 By default, AUTORESET will include the width of the signal in the autos,
9277 this is a recent change. To control this behavior, see
9278 `verilog-auto-reset-widths'.
9280 AUTORESET ties signals to deasserted, which is presumed to be zero.
9281 Signals that match `verilog-active-low-regexp' will be deasserted by tieing
9282 them to a one.
9284 An example:
9286 always @(posedge clk or negedge reset_l) begin
9287 if (!reset_l) begin
9288 c <= 1;
9289 /*AUTORESET*/
9291 else begin
9292 a <= in_a;
9293 b <= in_b;
9294 c <= in_c;
9298 Typing \\[verilog-auto] will make this into:
9300 always @(posedge core_clk or negedge reset_l) begin
9301 if (!reset_l) begin
9302 c <= 1;
9303 /*AUTORESET*/
9304 // Beginning of autoreset for uninitialized flops
9305 a <= 0;
9306 b <= 0;
9307 // End of automatics
9309 else begin
9310 a <= in_a;
9311 b <= in_b;
9312 c <= in_c;
9314 end"
9316 (interactive)
9317 (save-excursion
9318 ;; Find beginning
9319 (let* ((indent-pt (current-indentation))
9320 (modi (verilog-modi-current))
9321 (moddecls (verilog-modi-get-decls modi))
9322 (all-list (verilog-decls-get-signals moddecls))
9323 sigss sig-list prereset-sigs assignment-str)
9324 ;; Read signals in always, eliminate outputs from reset list
9325 (setq prereset-sigs (verilog-signals-from-signame
9326 (save-excursion
9327 (verilog-read-signals
9328 (save-excursion
9329 (verilog-re-search-backward "\\(@\\|\\<begin\\>\\|\\<if\\>\\|\\<case\\>\\)" nil t)
9330 (point))
9331 (point)))))
9332 (save-excursion
9333 (verilog-re-search-backward "@" nil t)
9334 (setq sigss (verilog-read-always-signals)))
9335 (setq assignment-str (if (verilog-alw-get-uses-delayed sigss)
9336 (concat " <= " verilog-assignment-delay)
9337 " = "))
9338 (setq sig-list (verilog-signals-not-in (verilog-alw-get-outputs sigss)
9339 prereset-sigs))
9340 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
9341 (when sig-list
9342 (insert "\n");
9343 (indent-to indent-pt)
9344 (insert "// Beginning of autoreset for uninitialized flops\n");
9345 (indent-to indent-pt)
9346 (while sig-list
9347 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ;; As sig-list has no widths
9348 (car sig-list))))
9349 (insert (verilog-sig-name sig)
9350 assignment-str
9351 (verilog-sig-tieoff sig (not verilog-auto-reset-widths))
9352 ";\n")
9353 (indent-to indent-pt)
9354 (setq sig-list (cdr sig-list))))
9355 (insert "// End of automatics")))))
9357 (defun verilog-auto-tieoff ()
9358 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
9359 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
9360 signals to deasserted.
9362 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
9363 input/output list as another module, but no internals. Specifically, it
9364 finds all outputs in the module, and if that input is not otherwise declared
9365 as a register or wire, creates a tieoff.
9367 AUTORESET ties signals to deasserted, which is presumed to be zero.
9368 Signals that match `verilog-active-low-regexp' will be deasserted by tieing
9369 them to a one.
9371 An example of making a stub for another module:
9373 module ExampStub (/*AUTOINST*/);
9374 /*AUTOINOUTMODULE(\"Foo\")*/
9375 /*AUTOTIEOFF*/
9376 // verilator lint_off UNUSED
9377 wire _unused_ok = &{1'b0,
9378 /*AUTOUNUSED*/
9379 1'b0};
9380 // verilator lint_on UNUSED
9381 endmodule
9383 Typing \\[verilog-auto] will make this into:
9385 module ExampStub (/*AUTOINST*/...);
9386 /*AUTOINOUTMODULE(\"Foo\")*/
9387 // Beginning of autotieoff
9388 output [2:0] foo;
9389 // End of automatics
9391 /*AUTOTIEOFF*/
9392 // Beginning of autotieoff
9393 wire [2:0] foo = 3'b0;
9394 // End of automatics
9396 endmodule"
9397 (interactive)
9398 (save-excursion
9399 ;; Find beginning
9400 (let* ((indent-pt (current-indentation))
9401 (modi (verilog-modi-current))
9402 (moddecls (verilog-modi-get-decls modi))
9403 (modsubdecls (verilog-modi-get-sub-decls modi))
9404 (sig-list (verilog-signals-not-in
9405 (verilog-decls-get-outputs moddecls)
9406 (append (verilog-decls-get-wires moddecls)
9407 (verilog-decls-get-regs moddecls)
9408 (verilog-decls-get-assigns moddecls)
9409 (verilog-decls-get-consts moddecls)
9410 (verilog-decls-get-gparams moddecls)
9411 (verilog-subdecls-get-outputs modsubdecls)
9412 (verilog-subdecls-get-inouts modsubdecls)))))
9413 (when sig-list
9414 (forward-line 1)
9415 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
9416 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
9417 (verilog-modi-cache-add-wires modi sig-list) ; Before we trash list
9418 (while sig-list
9419 (let ((sig (car sig-list)))
9420 (verilog-insert-one-definition sig "wire" indent-pt)
9421 (indent-to (max 48 (+ indent-pt 40)))
9422 (insert "= " (verilog-sig-tieoff sig)
9423 ";\n")
9424 (setq sig-list (cdr sig-list))))
9425 (verilog-insert-indent "// End of automatics\n")))))
9427 (defun verilog-auto-unused ()
9428 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
9429 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
9430 input and inout signals.
9432 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
9433 input/output list as another module, but no internals. Specifically, it
9434 finds all inputs and inouts in the module, and if that input is not otherwise
9435 used, adds it to a comma separated list.
9437 The comma separated list is intended to be used to create a _unused_ok
9438 signal. Using the exact name \"_unused_ok\" for name of the temporary
9439 signal is recommended as it will insure maximum forward compatibility, it
9440 also makes lint warnings easy to understand; ignore any unused warnings
9441 with \"unused\" in the signal name.
9443 To reduce simulation time, the _unused_ok signal should be forced to a
9444 constant to prevent wiggling. The easiest thing to do is use a
9445 reduction-and with 1'b0 as shown.
9447 This way all unused signals are in one place, making it convenient to add
9448 your tool's specific pragmas around the assignment to disable any unused
9449 warnings.
9451 You can add signals you do not want included in AUTOUNUSED with
9452 `verilog-auto-unused-ignore-regexp'.
9454 An example of making a stub for another module:
9456 module ExampStub (/*AUTOINST*/);
9457 /*AUTOINOUTMODULE(\"Examp\")*/
9458 /*AUTOTIEOFF*/
9459 // verilator lint_off UNUSED
9460 wire _unused_ok = &{1'b0,
9461 /*AUTOUNUSED*/
9462 1'b0};
9463 // verilator lint_on UNUSED
9464 endmodule
9466 Typing \\[verilog-auto] will make this into:
9469 // verilator lint_off UNUSED
9470 wire _unused_ok = &{1'b0,
9471 /*AUTOUNUSED*/
9472 // Beginning of automatics
9473 unused_input_a,
9474 unused_input_b,
9475 unused_input_c,
9476 // End of automatics
9477 1'b0};
9478 // verilator lint_on UNUSED
9479 endmodule"
9480 (interactive)
9481 (save-excursion
9482 ;; Find beginning
9483 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
9484 (modi (verilog-modi-current))
9485 (moddecls (verilog-modi-get-decls modi))
9486 (modsubdecls (verilog-modi-get-sub-decls modi))
9487 (sig-list (verilog-signals-not-in
9488 (append (verilog-decls-get-inputs moddecls)
9489 (verilog-decls-get-inouts moddecls))
9490 (append (verilog-subdecls-get-inputs modsubdecls)
9491 (verilog-subdecls-get-inouts modsubdecls)))))
9492 (setq sig-list (verilog-signals-not-matching-regexp
9493 sig-list verilog-auto-unused-ignore-regexp))
9494 (when sig-list
9495 (forward-line 1)
9496 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
9497 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
9498 (while sig-list
9499 (let ((sig (car sig-list)))
9500 (indent-to indent-pt)
9501 (insert (verilog-sig-name sig) ",\n")
9502 (setq sig-list (cdr sig-list))))
9503 (verilog-insert-indent "// End of automatics\n")))))
9505 (defun verilog-enum-ascii (signm elim-regexp)
9506 "Convert an enum name SIGNM to an ascii string for insertion.
9507 Remove user provided prefix ELIM-REGEXP."
9508 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
9509 (let ((case-fold-search t))
9510 ;; All upper becomes all lower for readability
9511 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
9513 (defun verilog-auto-ascii-enum ()
9514 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
9515 Create a register to contain the ASCII decode of a enumerated signal type.
9516 This will allow trace viewers to show the ASCII name of states.
9518 First, parameters are built into a enumeration using the synopsys enum
9519 comment. The comment must be between the keyword and the symbol.
9520 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
9522 Next, registers which that enum applies to are also tagged with the same
9523 enum. Synopsys also suggests labeling state vectors, but `verilog-mode'
9524 doesn't care.
9526 Finally, a AUTOASCIIENUM command is used.
9528 The first parameter is the name of the signal to be decoded.
9530 The second parameter is the name to store the ASCII code into. For the
9531 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
9532 a signal that is just for simulation, and the magic characters _ascii
9533 tell viewers like Dinotrace to display in ASCII format.
9535 The final optional parameter is a string which will be removed from the
9536 state names.
9538 An example:
9540 //== State enumeration
9541 parameter [2:0] // synopsys enum state_info
9542 SM_IDLE = 3'b000,
9543 SM_SEND = 3'b001,
9544 SM_WAIT1 = 3'b010;
9545 //== State variables
9546 reg [2:0] /* synopsys enum state_info */
9547 state_r; /* synopsys state_vector state_r */
9548 reg [2:0] /* synopsys enum state_info */
9549 state_e1;
9551 //== ASCII state decoding
9553 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
9555 Typing \\[verilog-auto] will make this into:
9557 ... same front matter ...
9559 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
9560 // Beginning of automatic ASCII enum decoding
9561 reg [39:0] state_ascii_r; // Decode of state_r
9562 always @(state_r) begin
9563 case ({state_r})
9564 SM_IDLE: state_ascii_r = \"idle \";
9565 SM_SEND: state_ascii_r = \"send \";
9566 SM_WAIT1: state_ascii_r = \"wait1\";
9567 default: state_ascii_r = \"%Erro\";
9568 endcase
9570 // End of automatics"
9571 (save-excursion
9572 (let* ((params (verilog-read-auto-params 2 3))
9573 (undecode-name (nth 0 params))
9574 (ascii-name (nth 1 params))
9575 (elim-regexp (nth 2 params))
9577 (indent-pt (current-indentation))
9578 (modi (verilog-modi-current))
9579 (moddecls (verilog-modi-get-decls modi))
9581 (sig-list-consts (append (verilog-decls-get-consts moddecls)
9582 (verilog-decls-get-gparams moddecls)))
9583 (sig-list-all (append (verilog-decls-get-regs moddecls)
9584 (verilog-decls-get-outputs moddecls)
9585 (verilog-decls-get-inouts moddecls)
9586 (verilog-decls-get-inputs moddecls)
9587 (verilog-decls-get-wires moddecls)))
9589 (undecode-sig (or (assoc undecode-name sig-list-all)
9590 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
9591 (undecode-enum (or (verilog-sig-enum undecode-sig)
9592 (error "%s: Signal %s does not have a enum tag" (verilog-point-text) undecode-name)))
9594 (enum-sigs (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
9595 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum)))
9597 (enum-chars 0)
9598 (ascii-chars 0))
9600 ;; Find number of ascii chars needed
9601 (let ((tmp-sigs enum-sigs))
9602 (while tmp-sigs
9603 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
9604 ascii-chars (max ascii-chars (length (verilog-enum-ascii
9605 (verilog-sig-name (car tmp-sigs))
9606 elim-regexp)))
9607 tmp-sigs (cdr tmp-sigs))))
9609 (forward-line 1)
9610 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
9611 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
9612 (concat "Decode of " undecode-name) nil nil))))
9613 (verilog-insert-definition decode-sig-list "reg" indent-pt nil)
9614 (verilog-modi-cache-add-regs modi decode-sig-list))
9616 (verilog-insert-indent "always @(" undecode-name ") begin\n")
9617 (setq indent-pt (+ indent-pt verilog-indent-level))
9618 (indent-to indent-pt)
9619 (insert "case ({" undecode-name "})\n")
9620 (setq indent-pt (+ indent-pt verilog-case-indent))
9622 (let ((tmp-sigs enum-sigs)
9623 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n" (1+ (max 8 enum-chars))
9624 ascii-name ascii-chars))
9625 (errname (substring "%Error" 0 (min 6 ascii-chars))))
9626 (while tmp-sigs
9627 (verilog-insert-indent
9628 (format chrfmt (concat (verilog-sig-name (car tmp-sigs)) ":")
9629 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
9630 elim-regexp)))
9631 (setq tmp-sigs (cdr tmp-sigs)))
9632 (verilog-insert-indent (format chrfmt "default:" errname)))
9634 (setq indent-pt (- indent-pt verilog-case-indent))
9635 (verilog-insert-indent "endcase\n")
9636 (setq indent-pt (- indent-pt verilog-indent-level))
9637 (verilog-insert-indent "end\n"
9638 "// End of automatics\n"))))
9640 (defun verilog-auto-templated-rel ()
9641 "Replace Templated relative line numbers with absolute line numbers.
9642 Internal use only. This hacks around the line numbers in AUTOINST Templates
9643 being different from the final output's line numbering."
9644 (let ((templateno 0) (template-line (list 0)))
9645 ;; Find line number each template is on
9646 (goto-char (point-min))
9647 (while (search-forward "AUTO_TEMPLATE" nil t)
9648 (setq templateno (1+ templateno))
9649 (setq template-line
9650 (cons (count-lines (point-min) (point)) template-line)))
9651 (setq template-line (nreverse template-line))
9652 ;; Replace T# L# with absolute line number
9653 (goto-char (point-min))
9654 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
9655 (replace-match
9656 (concat " Templated "
9657 (int-to-string (+ (nth (string-to-number (match-string 1))
9658 template-line)
9659 (string-to-number (match-string 2)))))
9660 t t))))
9664 ;; Auto top level
9667 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing a arg
9668 "Expand AUTO statements.
9669 Look for any /*AUTO...*/ commands in the code, as used in
9670 instantiations or argument headers. Update the list of signals
9671 following the /*AUTO...*/ command.
9673 Use \\[verilog-delete-auto] to remove the AUTOs.
9675 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
9677 Use \\[verilog-faq] for a pointer to frequently asked questions.
9679 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
9680 called before and after this function, respectively.
9682 For example:
9683 module ModuleName (/*AUTOARG*/)
9684 /*AUTOINPUT*/
9685 /*AUTOOUTPUT*/
9686 /*AUTOWIRE*/
9687 /*AUTOREG*/
9688 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
9690 You can also update the AUTOs from the shell using:
9691 emacs --batch <filenames.v> -f verilog-batch-auto
9692 Or fix indentation with:
9693 emacs --batch <filenames.v> -f verilog-batch-indent
9694 Likewise, you can delete or inject AUTOs with:
9695 emacs --batch <filenames.v> -f verilog-batch-delete-auto
9696 emacs --batch <filenames.v> -f verilog-batch-inject-auto
9698 Using \\[describe-function], see also:
9699 `verilog-auto-arg' for AUTOARG module instantiations
9700 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
9701 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
9702 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
9703 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
9704 `verilog-auto-inst' for AUTOINST instantiation pins
9705 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
9706 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
9707 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
9708 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
9709 `verilog-auto-reg' for AUTOREG registers
9710 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
9711 `verilog-auto-reset' for AUTORESET flop resets
9712 `verilog-auto-sense' for AUTOSENSE always sensitivity lists
9713 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
9714 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
9715 `verilog-auto-wire' for AUTOWIRE instantiation wires
9717 `verilog-read-defines' for reading `define values
9718 `verilog-read-includes' for reading `includes
9720 If you have bugs with these autos, try contacting the AUTOAUTHOR
9721 Wilson Snyder (wsnyder@wsnyder.org), and/or see http://www.veripool.com."
9722 (interactive)
9723 (unless noninteractive (message "Updating AUTOs..."))
9724 (if (fboundp 'dinotrace-unannotate-all)
9725 (dinotrace-unannotate-all))
9726 (let ((oldbuf (if (not (buffer-modified-p))
9727 (buffer-string)))
9728 ;; Before version 20, match-string with font-lock returns a
9729 ;; vector that is not equal to the string. IE if on "input"
9730 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
9731 (fontlocked (when (and (boundp 'font-lock-mode)
9732 font-lock-mode)
9733 (font-lock-mode nil)
9735 ;; Cache directories; we don't write new files, so can't change
9736 (verilog-dir-cache-preserving t))
9737 (unwind-protect
9738 (save-excursion
9739 ;; If we're not in verilog-mode, change syntax table so parsing works right
9740 (unless (eq major-mode `verilog-mode) (verilog-mode))
9741 ;; Allow user to customize
9742 (run-hooks 'verilog-before-auto-hook)
9743 ;; Try to save the user from needing to revert-file to reread file local-variables
9744 (verilog-auto-reeval-locals)
9745 (verilog-read-auto-lisp (point-min) (point-max))
9746 (verilog-getopt-flags)
9747 ;; From here on out, we can cache anything we read from disk
9748 (verilog-preserve-dir-cache
9749 ;; These two may seem obvious to do always, but on large includes it can be way too slow
9750 (when verilog-auto-read-includes
9751 (verilog-read-includes)
9752 (verilog-read-defines nil nil t))
9753 ;; This particular ordering is important
9754 ;; INST: Lower modules correct, no internal dependencies, FIRST
9755 (verilog-preserve-modi-cache
9756 ;; Clear existing autos else we'll be screwed by existing ones
9757 (verilog-delete-auto)
9758 ;; Injection if appropriate
9759 (when inject
9760 (verilog-inject-inst)
9761 (verilog-inject-sense)
9762 (verilog-inject-arg))
9764 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
9765 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
9766 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
9767 ;; Doesn't matter when done, but combine it with a common changer
9768 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
9769 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
9770 ;; Must be done before autoin/out as creates a reg
9771 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM([^)]*)\\*/" 'verilog-auto-ascii-enum)
9773 ;; first in/outs from other files
9774 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE([^)]*)\\*/" 'verilog-auto-inout-module)
9775 ;; next in/outs which need previous sucked inputs first
9776 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((\"[^\"]*\")\\)\\*/"
9777 '(lambda () (verilog-auto-output t)))
9778 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\*/" 'verilog-auto-output)
9779 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((\"[^\"]*\")\\)\\*/"
9780 '(lambda () (verilog-auto-input t)))
9781 (verilog-auto-re-search-do "/\\*AUTOINPUT\\*/" 'verilog-auto-input)
9782 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((\"[^\"]*\")\\)\\*/"
9783 '(lambda () (verilog-auto-inout t)))
9784 (verilog-auto-re-search-do "/\\*AUTOINOUT\\*/" 'verilog-auto-inout)
9785 ;; Then tie off those in/outs
9786 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
9787 ;; Wires/regs must be after inputs/outputs
9788 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
9789 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
9790 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
9791 ;; outputevery needs AUTOOUTPUTs done first
9792 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\*/" 'verilog-auto-output-every)
9793 ;; After we've created all new variables
9794 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
9795 ;; Must be after all inputs outputs are generated
9796 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
9797 ;; Fix line numbers (comments only)
9798 (verilog-auto-templated-rel)))
9800 (run-hooks 'verilog-auto-hook)
9802 (set (make-local-variable 'verilog-auto-update-tick) (buffer-modified-tick))
9804 ;; If end result is same as when started, clear modified flag
9805 (cond ((and oldbuf (equal oldbuf (buffer-string)))
9806 (set-buffer-modified-p nil)
9807 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
9808 (t (unless noninteractive (message "Updating AUTOs...done"))))))
9809 ;; Unwind forms
9810 (progn
9811 ;; Restore font-lock
9812 (when fontlocked (font-lock-mode t)))))
9816 ;; Skeleton based code insertion
9818 (defvar verilog-template-map
9819 (let ((map (make-sparse-keymap)))
9820 (define-key map "a" 'verilog-sk-always)
9821 (define-key map "b" 'verilog-sk-begin)
9822 (define-key map "c" 'verilog-sk-case)
9823 (define-key map "f" 'verilog-sk-for)
9824 (define-key map "g" 'verilog-sk-generate)
9825 (define-key map "h" 'verilog-sk-header)
9826 (define-key map "i" 'verilog-sk-initial)
9827 (define-key map "j" 'verilog-sk-fork)
9828 (define-key map "m" 'verilog-sk-module)
9829 (define-key map "p" 'verilog-sk-primitive)
9830 (define-key map "r" 'verilog-sk-repeat)
9831 (define-key map "s" 'verilog-sk-specify)
9832 (define-key map "t" 'verilog-sk-task)
9833 (define-key map "w" 'verilog-sk-while)
9834 (define-key map "x" 'verilog-sk-casex)
9835 (define-key map "z" 'verilog-sk-casez)
9836 (define-key map "?" 'verilog-sk-if)
9837 (define-key map ":" 'verilog-sk-else-if)
9838 (define-key map "/" 'verilog-sk-comment)
9839 (define-key map "A" 'verilog-sk-assign)
9840 (define-key map "F" 'verilog-sk-function)
9841 (define-key map "I" 'verilog-sk-input)
9842 (define-key map "O" 'verilog-sk-output)
9843 (define-key map "S" 'verilog-sk-state-machine)
9844 (define-key map "=" 'verilog-sk-inout)
9845 (define-key map "W" 'verilog-sk-wire)
9846 (define-key map "R" 'verilog-sk-reg)
9847 (define-key map "D" 'verilog-sk-define-signal)
9848 map)
9849 "Keymap used in Verilog mode for smart template operations.")
9853 ;; Place the templates into Verilog Mode. They may be inserted under any key.
9854 ;; C-c C-t will be the default. If you use templates a lot, you
9855 ;; may want to consider moving the binding to another key in your .emacs
9856 ;; file.
9858 ;(define-key verilog-mode-map "\C-ct" verilog-template-map)
9859 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
9861 ;;; ---- statement skeletons ------------------------------------------
9863 (define-skeleton verilog-sk-prompt-condition
9864 "Prompt for the loop condition."
9865 "[condition]: " str )
9867 (define-skeleton verilog-sk-prompt-init
9868 "Prompt for the loop init statement."
9869 "[initial statement]: " str )
9871 (define-skeleton verilog-sk-prompt-inc
9872 "Prompt for the loop increment statement."
9873 "[increment statement]: " str )
9875 (define-skeleton verilog-sk-prompt-name
9876 "Prompt for the name of something."
9877 "[name]: " str)
9879 (define-skeleton verilog-sk-prompt-clock
9880 "Prompt for the name of something."
9881 "name and edge of clock(s): " str)
9883 (defvar verilog-sk-reset nil)
9884 (defun verilog-sk-prompt-reset ()
9885 "Prompt for the name of a state machine reset."
9886 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
9889 (define-skeleton verilog-sk-prompt-state-selector
9890 "Prompt for the name of a state machine selector."
9891 "name of selector (eg {a,b,c,d}): " str )
9893 (define-skeleton verilog-sk-prompt-output
9894 "Prompt for the name of something."
9895 "output: " str)
9897 (define-skeleton verilog-sk-prompt-msb
9898 "Prompt for least significant bit specification."
9899 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
9901 (define-skeleton verilog-sk-prompt-lsb
9902 "Prompt for least significant bit specification."
9903 "lsb:" str )
9905 (defvar verilog-sk-p nil)
9906 (define-skeleton verilog-sk-prompt-width
9907 "Prompt for a width specification."
9909 (progn
9910 (setq verilog-sk-p (point))
9911 (verilog-sk-prompt-msb)
9912 (if (> (point) verilog-sk-p) "] " " ")))
9914 (defun verilog-sk-header ()
9915 "Insert a descriptive header at the top of the file."
9916 (interactive "*")
9917 (save-excursion
9918 (goto-char (point-min))
9919 (verilog-sk-header-tmpl)))
9921 (define-skeleton verilog-sk-header-tmpl
9922 "Insert a comment block containing the module title, author, etc."
9923 "[Description]: "
9924 "// -*- Mode: Verilog -*-"
9925 "\n// Filename : " (buffer-name)
9926 "\n// Description : " str
9927 "\n// Author : " (user-full-name)
9928 "\n// Created On : " (current-time-string)
9929 "\n// Last Modified By: ."
9930 "\n// Last Modified On: ."
9931 "\n// Update Count : 0"
9932 "\n// Status : Unknown, Use with caution!"
9933 "\n")
9935 (define-skeleton verilog-sk-module
9936 "Insert a module definition."
9938 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
9939 > _ \n
9940 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
9942 (define-skeleton verilog-sk-primitive
9943 "Insert a task definition."
9945 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
9946 > _ \n
9947 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
9949 (define-skeleton verilog-sk-task
9950 "Insert a task definition."
9952 > "task " '(verilog-sk-prompt-name) & ?; \n
9953 > _ \n
9954 > "begin" \n
9955 > \n
9956 > (- verilog-indent-level-behavioral) "end" \n
9957 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
9959 (define-skeleton verilog-sk-function
9960 "Insert a function definition."
9962 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
9963 > _ \n
9964 > "begin" \n
9965 > \n
9966 > (- verilog-indent-level-behavioral) "end" \n
9967 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
9969 (define-skeleton verilog-sk-always
9970 "Insert always block. Uses the minibuffer to prompt
9971 for sensitivity list."
9973 > "always @ ( /*AUTOSENSE*/ ) begin\n"
9974 > _ \n
9975 > (- verilog-indent-level-behavioral) "end" \n >
9978 (define-skeleton verilog-sk-initial
9979 "Insert an initial block."
9981 > "initial begin\n"
9982 > _ \n
9983 > (- verilog-indent-level-behavioral) "end" \n > )
9985 (define-skeleton verilog-sk-specify
9986 "Insert specify block. "
9988 > "specify\n"
9989 > _ \n
9990 > (- verilog-indent-level-behavioral) "endspecify" \n > )
9992 (define-skeleton verilog-sk-generate
9993 "Insert generate block. "
9995 > "generate\n"
9996 > _ \n
9997 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
9999 (define-skeleton verilog-sk-begin
10000 "Insert begin end block. Uses the minibuffer to prompt for name."
10002 > "begin" '(verilog-sk-prompt-name) \n
10003 > _ \n
10004 > (- verilog-indent-level-behavioral) "end"
10007 (define-skeleton verilog-sk-fork
10008 "Insert a fork join block."
10010 > "fork\n"
10011 > "begin" \n
10012 > _ \n
10013 > (- verilog-indent-level-behavioral) "end" \n
10014 > "begin" \n
10015 > \n
10016 > (- verilog-indent-level-behavioral) "end" \n
10017 > (- verilog-indent-level-behavioral) "join" \n
10021 (define-skeleton verilog-sk-case
10022 "Build skeleton case statement, prompting for the selector expression,
10023 and the case items."
10024 "[selector expression]: "
10025 > "case (" str ") " \n
10026 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n )
10027 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
10029 (define-skeleton verilog-sk-casex
10030 "Build skeleton casex statement, prompting for the selector expression,
10031 and the case items."
10032 "[selector expression]: "
10033 > "casex (" str ") " \n
10034 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n )
10035 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
10037 (define-skeleton verilog-sk-casez
10038 "Build skeleton casez statement, prompting for the selector expression,
10039 and the case items."
10040 "[selector expression]: "
10041 > "casez (" str ") " \n
10042 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n )
10043 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
10045 (define-skeleton verilog-sk-if
10046 "Insert a skeleton if statement."
10047 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
10048 > _ \n
10049 > (- verilog-indent-level-behavioral) "end " \n )
10051 (define-skeleton verilog-sk-else-if
10052 "Insert a skeleton else if statement."
10053 > (verilog-indent-line) "else if ("
10054 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
10055 > _ \n
10056 > "end" (progn (electric-verilog-terminate-line) nil))
10058 (define-skeleton verilog-sk-datadef
10059 "Common routine to get data definition."
10061 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
10063 (define-skeleton verilog-sk-input
10064 "Insert an input definition."
10066 > "input [" '(verilog-sk-datadef))
10068 (define-skeleton verilog-sk-output
10069 "Insert an output definition."
10071 > "output [" '(verilog-sk-datadef))
10073 (define-skeleton verilog-sk-inout
10074 "Insert an inout definition."
10076 > "inout [" '(verilog-sk-datadef))
10078 (defvar verilog-sk-signal nil)
10079 (define-skeleton verilog-sk-def-reg
10080 "Insert a reg definition."
10082 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations) )
10084 (defun verilog-sk-define-signal ()
10085 "Insert a definition of signal under point at top of module."
10086 (interactive "*")
10087 (let* ((sig-re "[a-zA-Z0-9_]*")
10088 (v1 (buffer-substring
10089 (save-excursion
10090 (skip-chars-backward sig-re)
10091 (point))
10092 (save-excursion
10093 (skip-chars-forward sig-re)
10094 (point)))))
10095 (if (not (member v1 verilog-keywords))
10096 (save-excursion
10097 (setq verilog-sk-signal v1)
10098 (verilog-beg-of-defun)
10099 (verilog-end-of-statement)
10100 (verilog-forward-syntactic-ws)
10101 (verilog-sk-def-reg)
10102 (message "signal at point is %s" v1))
10103 (message "object at point (%s) is a keyword" v1))))
10105 (define-skeleton verilog-sk-wire
10106 "Insert a wire definition."
10108 > "wire [" '(verilog-sk-datadef))
10110 (define-skeleton verilog-sk-reg
10111 "Insert a reg definition."
10113 > "reg [" '(verilog-sk-datadef))
10115 (define-skeleton verilog-sk-assign
10116 "Insert a skeleton assign statement."
10118 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
10120 (define-skeleton verilog-sk-while
10121 "Insert a skeleton while loop statement."
10123 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
10124 > _ \n
10125 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
10127 (define-skeleton verilog-sk-repeat
10128 "Insert a skeleton repeat loop statement."
10130 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
10131 > _ \n
10132 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
10134 (define-skeleton verilog-sk-for
10135 "Insert a skeleton while loop statement."
10137 > "for ("
10138 '(verilog-sk-prompt-init) "; "
10139 '(verilog-sk-prompt-condition) "; "
10140 '(verilog-sk-prompt-inc)
10141 ") begin" \n
10142 > _ \n
10143 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
10145 (define-skeleton verilog-sk-comment
10146 "Inserts three comment lines, making a display comment."
10148 > "/*\n"
10149 > "* " _ \n
10150 > "*/")
10152 (define-skeleton verilog-sk-state-machine
10153 "Insert a state machine definition."
10154 "Name of state variable: "
10155 '(setq input "state")
10156 > "// State registers for " str | -23 \n
10157 '(setq verilog-sk-state str)
10158 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
10159 '(setq input nil)
10160 > \n
10161 > "// State FF for " verilog-sk-state \n
10162 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
10163 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
10164 > verilog-sk-state " = next_" verilog-sk-state ?; \n
10165 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
10166 > \n
10167 > "// Next State Logic for " verilog-sk-state \n
10168 > "always @ ( /*AUTOSENSE*/ ) begin\n"
10169 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
10170 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
10171 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
10172 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
10176 ;; Include file loading with mouse/return event
10178 ;; idea & first impl.: M. Rouat (eldo-mode.el)
10179 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
10181 (if (featurep 'xemacs)
10182 (require 'overlay)
10183 (require 'lucid)) ;; what else can we do ??
10185 (defconst verilog-include-file-regexp
10186 "^`include\\s-+\"\\([^\n\"]*\\)\""
10187 "Regexp that matches the include file.")
10189 (defvar verilog-mode-mouse-map
10190 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
10191 (set-keymap-parent map verilog-mode-map)
10192 ;; mouse button bindings
10193 (define-key map "\r" 'verilog-load-file-at-point)
10194 (if (featurep 'xemacs)
10195 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
10196 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
10197 (if (featurep 'xemacs)
10198 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
10199 (define-key map [S-mouse-2] 'mouse-yank-at-click))
10200 map)
10201 "Map containing mouse bindings for `verilog-mode'.")
10204 (defun verilog-colorize-include-files (beg end old-len)
10205 "This function colorizes included files when the mouse passes over them.
10206 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
10207 (save-excursion
10208 (save-match-data
10209 (let (end-point)
10210 (goto-char end)
10211 (setq end-point (verilog-get-end-of-line))
10212 (goto-char beg)
10213 (beginning-of-line) ; scan entire line !
10214 ;; delete overlays existing on this line
10215 (let ((overlays (overlays-in (point) end-point)))
10216 (while overlays
10217 (if (and
10218 (overlay-get (car overlays) 'detachable)
10219 (overlay-get (car overlays) 'verilog-include-file))
10220 (delete-overlay (car overlays)))
10221 (setq overlays (cdr overlays)))) ; let
10222 ;; make new ones, could reuse deleted one ?
10223 (while (search-forward-regexp verilog-include-file-regexp end-point t)
10224 (let (ov)
10225 (goto-char (match-beginning 1))
10226 (setq ov (make-overlay (match-beginning 1) (match-end 1)))
10227 (overlay-put ov 'start-closed 't)
10228 (overlay-put ov 'end-closed 't)
10229 (overlay-put ov 'evaporate 't)
10230 (overlay-put ov 'verilog-include-file 't)
10231 (overlay-put ov 'mouse-face 'highlight)
10232 (overlay-put ov 'local-map verilog-mode-mouse-map)))))))
10235 (defun verilog-colorize-include-files-buffer ()
10236 "Colorize an include file."
10237 (interactive)
10238 ;; delete overlays
10239 (let ((overlays (overlays-in (point-min) (point-max))))
10240 (while overlays
10241 (if (and
10242 (overlay-get (car overlays) 'detachable)
10243 (overlay-get (car overlays) 'verilog-include-file))
10244 (delete-overlay (car overlays)))
10245 (setq overlays (cdr overlays)))) ; let
10246 ;; remake overlays
10247 (verilog-colorize-include-files (point-min) (point-max) nil))
10249 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
10250 ;; so define this function to do more or less the same as ffap-at-mouse
10251 ;; but first resolve filename...
10252 (defun verilog-load-file-at-mouse (event)
10253 "Load file under button 2 click's EVENT.
10254 Files are checked based on `verilog-library-directories'."
10255 (interactive "@e")
10256 (save-excursion ;; implement a Verilog specific ffap-at-mouse
10257 (mouse-set-point event)
10258 (beginning-of-line)
10259 (if (looking-at verilog-include-file-regexp)
10260 (if (and (car (verilog-library-filenames
10261 (match-string 1) (buffer-file-name)))
10262 (file-readable-p (car (verilog-library-filenames
10263 (match-string 1) (buffer-file-name)))))
10264 (find-file (car (verilog-library-filenames
10265 (match-string 1) (buffer-file-name))))
10266 (progn
10267 (message
10268 "File '%s' isn't readable, use shift-mouse2 to paste in this field"
10269 (match-string 1)))))))
10271 ;; ffap isn't useable for Verilog mode. It uses library paths.
10272 ;; so define this function to do more or less the same as ffap
10273 ;; but first resolve filename...
10274 (defun verilog-load-file-at-point ()
10275 "Load file under point.
10276 Files are checked based on `verilog-library-directories'."
10277 (interactive)
10278 (save-excursion ;; implement a Verilog specific ffap
10279 (beginning-of-line)
10280 (if (looking-at verilog-include-file-regexp)
10281 (if (and
10282 (car (verilog-library-filenames
10283 (match-string 1) (buffer-file-name)))
10284 (file-readable-p (car (verilog-library-filenames
10285 (match-string 1) (buffer-file-name)))))
10286 (find-file (car (verilog-library-filenames
10287 (match-string 1) (buffer-file-name))))))))
10291 ;; Bug reporting
10294 (defun verilog-faq ()
10295 "Tell the user their current version, and where to get the FAQ etc."
10296 (interactive)
10297 (with-output-to-temp-buffer "*verilog-mode help*"
10298 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
10299 (princ "\n")
10300 (princ "For new releases, see http://www.verilog.com\n")
10301 (princ "\n")
10302 (princ "For frequently asked questions, see http://www.veripool.com/verilog-mode-faq.html\n")
10303 (princ "\n")
10304 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
10305 (princ "\n")))
10307 (autoload 'reporter-submit-bug-report "reporter")
10308 (defvar reporter-prompt-for-summary-p)
10310 (defun verilog-submit-bug-report ()
10311 "Submit via mail a bug report on verilog-mode.el."
10312 (interactive)
10313 (let ((reporter-prompt-for-summary-p t))
10314 (reporter-submit-bug-report
10315 "mac@verilog.com"
10316 (concat "verilog-mode v" verilog-mode-version)
10318 verilog-align-ifelse
10319 verilog-auto-endcomments
10320 verilog-auto-hook
10321 verilog-auto-indent-on-newline
10322 verilog-auto-inst-vector
10323 verilog-auto-inst-template-numbers
10324 verilog-auto-lineup
10325 verilog-auto-newline
10326 verilog-auto-save-policy
10327 verilog-auto-sense-defines-constant
10328 verilog-auto-sense-include-inputs
10329 verilog-before-auto-hook
10330 verilog-case-indent
10331 verilog-cexp-indent
10332 verilog-compiler
10333 verilog-coverage
10334 verilog-highlight-translate-off
10335 verilog-indent-begin-after-if
10336 verilog-indent-declaration-macros
10337 verilog-indent-level
10338 verilog-indent-level-behavioral
10339 verilog-indent-level-declaration
10340 verilog-indent-level-directive
10341 verilog-indent-level-module
10342 verilog-indent-lists
10343 verilog-library-flags
10344 verilog-library-directories
10345 verilog-library-extensions
10346 verilog-library-files
10347 verilog-linter
10348 verilog-minimum-comment-distance
10349 verilog-mode-hook
10350 verilog-simulator
10351 verilog-tab-always-indent
10352 verilog-tab-to-comment
10354 nil nil
10355 (concat "Hi Mac,
10357 I want to report a bug. I've read the `Bugs' section of `Info' on
10358 Emacs, so I know how to make a clear and unambiguous report. To get
10359 to that Info section, I typed
10361 M-x info RET m " invocation-name " RET m bugs RET
10363 Before I go further, I want to say that Verilog mode has changed my life.
10364 I save so much time, my files are colored nicely, my co workers respect
10365 my coding ability... until now. I'd really appreciate anything you
10366 could do to help me out with this minor deficiency in the product.
10368 If you have bugs with the AUTO functions, please CC the AUTOAUTHOR Wilson
10369 Snyder (wsnyder@wsnyder.org) and/or see http://www.veripool.com.
10370 You may also want to look at the Verilog-Mode FAQ, see
10371 http://www.veripool.com/verilog-mode-faq.html.
10373 To reproduce the bug, start a fresh Emacs via " invocation-name "
10374 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
10375 the code included below.
10377 Given those lines, I expected [[Fill in here]] to happen;
10378 but instead, [[Fill in here]] happens!.
10380 == The code: =="))))
10382 (provide 'verilog-mode)
10384 ;; Local Variables:
10385 ;; checkdoc-permit-comma-termination-flag:t
10386 ;; checkdoc-force-docstrings-flag:nil
10387 ;; End:
10389 ;; arch-tag: 87923725-57b3-41b5-9494-be21118c6a6f
10390 ;;; verilog-mode.el ends here