Replace Lisp calls to delete-backward-char by delete-char.
[bpt/emacs.git] / lisp / progmodes / verilog-mode.el
1 ;; verilog-mode.el --- major mode for editing verilog source in Emacs
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
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.org
12 ;; Keywords: languages
13
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.
18
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!
22 ;;
23 ;;
24
25 ;; This file is part of GNU Emacs.
26
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.
31
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.
36
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/>.
39
40 ;;; Commentary:
41
42 ;; This mode borrows heavily from the Pascal-mode and the cc-mode of Emacs
43
44 ;; USAGE
45 ;; =====
46
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>
51
52 ;; KNOWN BUGS / BUG REPORTS
53 ;; =======================
54
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 issues to the issue tracker at
58 ;; http://www.veripool.org/verilog-mode
59 ;; Please use verilog-submit-bug-report to submit a report; type C-c
60 ;; C-b to invoke this and as a result I will have a much easier time
61 ;; of reproducing the bug you find, and hence fixing it.
62
63 ;; INSTALLING THE MODE
64 ;; ===================
65
66 ;; An older version of this mode may be already installed as a part of
67 ;; your environment, and one method of updating would be to update
68 ;; your Emacs environment. Sometimes this is difficult for local
69 ;; political/control reasons, and hence you can always install a
70 ;; private copy (or even a shared copy) which overrides the system
71 ;; default.
72
73 ;; You can get step by step help in installing this file by going to
74 ;; <http://www.verilog.com/emacs_install.html>
75
76 ;; The short list of installation instructions are: To set up
77 ;; automatic Verilog mode, put this file in your load path, and put
78 ;; the following in code (please un comment it first!) in your
79 ;; .emacs, or in your site's site-load.el
80
81 ; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
82 ; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
83
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.
89 ;;
90 ;; Be sure to examine at the help for verilog-auto, and the other
91 ;; verilog-auto-* functions for some major coding time savers.
92 ;;
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 'declarations
107 ; verilog-highlight-p1800-keywords nil
108 ; verilog-linter "my_lint_shell_command"
109 ; )
110
111 ;; \f
112
113 ;;; History:
114 ;;
115 ;; See commit history at http://www.veripool.org/verilog-mode.html
116 ;; (This section is required to appease checkdoc.)
117
118 ;;; Code:
119
120 ;; This variable will always hold the version number of the mode
121 (defconst verilog-mode-version "565"
122 "Version of this Verilog mode.")
123 (defconst verilog-mode-release-date "2010-03-01-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.")
127
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))
132
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 ;; Provide stuff if we are XEmacs
137 (when (featurep 'xemacs)
138 (condition-case nil
139 (require 'easymenu)
140 (error nil))
141 (condition-case nil
142 (require 'regexp-opt)
143 (error nil))
144 ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
145 (condition-case nil
146 (load "skeleton")
147 (error nil))
148 (condition-case nil
149 (if (fboundp 'when)
150 nil ;; fab
151 (defmacro when (cond &rest body)
152 (list 'if cond (cons 'progn body))))
153 (error nil))
154 (condition-case nil
155 (if (fboundp 'unless)
156 nil ;; fab
157 (defmacro unless (cond &rest body)
158 (cons 'if (cons cond (cons nil body)))))
159 (error nil))
160 (condition-case nil
161 (if (fboundp 'store-match-data)
162 nil ;; fab
163 (defmacro store-match-data (&rest args) nil))
164 (error nil))
165 (condition-case nil
166 (if (fboundp 'char-before)
167 nil ;; great
168 (defmacro char-before (&rest body)
169 (char-after (1- (point)))))
170 (error nil))
171 (condition-case nil
172 (require 'custom)
173 (error nil))
174 (condition-case nil
175 (if (fboundp 'match-string-no-properties)
176 nil ;; great
177 (defsubst match-string-no-properties (num &optional string)
178 "Return string of text matched by last search, without text properties.
179 NUM specifies which parenthesized expression in the last regexp.
180 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
181 Zero means the entire text matched by the whole regexp or whole string.
182 STRING should be given if the last search was by `string-match' on STRING."
183 (if (match-beginning num)
184 (if string
185 (let ((result
186 (substring string
187 (match-beginning num) (match-end num))))
188 (set-text-properties 0 (length result) nil result)
189 result)
190 (buffer-substring-no-properties (match-beginning num)
191 (match-end num)
192 (current-buffer)))))
193 )
194 (error nil))
195 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
196 nil ;; We've got what we needed
197 ;; We have the old custom-library, hack around it!
198 (defmacro defgroup (&rest args) nil)
199 (defmacro customize (&rest args)
200 (message
201 "Sorry, Customize is not available with this version of Emacs"))
202 (defmacro defcustom (var value doc &rest args)
203 `(defvar ,var ,value ,doc))
204 )
205 (if (fboundp 'defface)
206 nil ; great!
207 (defmacro defface (var values doc &rest args)
208 `(make-face ,var))
209 )
210
211 (if (and (featurep 'custom) (fboundp 'customize-group))
212 nil ;; We've got what we needed
213 ;; We have an intermediate custom-library, hack around it!
214 (defmacro customize-group (var &rest args)
215 `(customize ,var))
216 ))
217 ;; OK, do this stuff if we are NOT XEmacs:
218 (unless (featurep 'xemacs)
219 (unless (fboundp 'region-active-p)
220 (defmacro region-active-p ()
221 `(and transient-mark-mode mark-active))))
222 )
223
224 ;; Provide a regular expression optimization routine, using regexp-opt
225 ;; if provided by the user's elisp libraries
226 (eval-and-compile
227 ;; The below were disabled when GNU Emacs 22 was released;
228 ;; perhaps some still need to be there to support Emacs 21.
229 (if (featurep 'xemacs)
230 (if (fboundp 'regexp-opt)
231 ;; regexp-opt is defined, does it take 3 or 2 arguments?
232 (if (fboundp 'function-max-args)
233 (let ((args (function-max-args `regexp-opt)))
234 (cond
235 ((eq args 3) ;; It takes 3
236 (condition-case nil ; Hide this defun from emacses
237 ;with just a two input regexp
238 (defun verilog-regexp-opt (a b)
239 "Deal with differing number of required arguments for `regexp-opt'.
240 Call 'regexp-opt' on A and B."
241 (regexp-opt a b 't))
242 (error nil))
243 )
244 ((eq args 2) ;; It takes 2
245 (defun verilog-regexp-opt (a b)
246 "Call 'regexp-opt' on A and B."
247 (regexp-opt a b))
248 )
249 (t nil)))
250 ;; We can't tell; assume it takes 2
251 (defun verilog-regexp-opt (a b)
252 "Call 'regexp-opt' on A and B."
253 (regexp-opt a b))
254 )
255 ;; There is no regexp-opt, provide our own
256 (defun verilog-regexp-opt (strings &optional paren shy)
257 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
258 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
259 )
260 ;; Emacs.
261 (defalias 'verilog-regexp-opt 'regexp-opt)))
262
263 (eval-when-compile
264 (defun verilog-regexp-words (a)
265 "Call 'regexp-opt' with word delimiters for the words A."
266 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
267
268 (defun verilog-easy-menu-filter (menu)
269 "Filter `easy-menu-define' MENU to support new features."
270 (cond ((not (featurep 'xemacs))
271 menu) ;; GNU Emacs - passthru
272 ;; Xemacs doesn't support :help. Strip it.
273 ;; Recursively filter the a submenu
274 ((listp menu)
275 (mapcar 'verilog-easy-menu-filter menu))
276 ;; Look for [:help "blah"] and remove
277 ((vectorp menu)
278 (let ((i 0) (out []))
279 (while (< i (length menu))
280 (if (equal `:help (aref menu i))
281 (setq i (+ 2 i))
282 (setq out (vconcat out (vector (aref menu i)))
283 i (1+ i))))
284 out))
285 (t menu))) ;; Default - ok
286 ;;(verilog-easy-menu-filter
287 ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
288 ;; "----" ["MB" nil :help "Help MB"]))
289
290 (defun verilog-customize ()
291 "Customize variables and other settings used by Verilog-Mode."
292 (interactive)
293 (customize-group 'verilog-mode))
294
295 (defun verilog-font-customize ()
296 "Customize fonts used by Verilog-Mode."
297 (interactive)
298 (if (fboundp 'customize-apropos)
299 (customize-apropos "font-lock-*" 'faces)))
300
301 (defun verilog-booleanp (value)
302 "Return t if VALUE is boolean.
303 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
304 This function may be removed when Emacs 21 is no longer supported."
305 (or (equal value t) (equal value nil)))
306
307 (defun verilog-insert-last-command-event ()
308 "Insert the `last-command-event'."
309 (insert (if (featurep 'xemacs)
310 ;; XEmacs 21.5 doesn't like last-command-event
311 last-command-char
312 ;; And GNU Emacs 22 has obsoleted last-command-char
313 last-command-event)))
314
315 (defalias 'verilog-syntax-ppss
316 (if (fboundp 'syntax-ppss) 'syntax-ppss
317 (lambda (&optional pos) (parse-partial-sexp (point-min) (or pos (point))))))
318
319 (defgroup verilog-mode nil
320 "Facilitates easy editing of Verilog source text."
321 :version "22.2"
322 :group 'languages)
323
324 ; (defgroup verilog-mode-fonts nil
325 ; "Facilitates easy customization fonts used in Verilog source text"
326 ; :link '(customize-apropos "font-lock-*" 'faces)
327 ; :group 'verilog-mode)
328
329 (defgroup verilog-mode-indent nil
330 "Customize indentation and highlighting of Verilog source text."
331 :group 'verilog-mode)
332
333 (defgroup verilog-mode-actions nil
334 "Customize actions on Verilog source text."
335 :group 'verilog-mode)
336
337 (defgroup verilog-mode-auto nil
338 "Customize AUTO actions when expanding Verilog source text."
339 :group 'verilog-mode)
340
341 (defcustom verilog-linter
342 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
343 "*Unix program and arguments to call to run a lint checker on Verilog source.
344 Depending on the `verilog-set-compile-command', this may be invoked when
345 you type \\[compile]. When the compile completes, \\[next-error] will take
346 you to the next lint error."
347 :type 'string
348 :group 'verilog-mode-actions)
349 ;; We don't mark it safe, as it's used as a shell command
350
351 (defcustom verilog-coverage
352 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
353 "*Program and arguments to use to annotate for coverage Verilog source.
354 Depending on the `verilog-set-compile-command', this may be invoked when
355 you type \\[compile]. When the compile completes, \\[next-error] will take
356 you to the next lint error."
357 :type 'string
358 :group 'verilog-mode-actions)
359 ;; We don't mark it safe, as it's used as a shell command
360
361 (defcustom verilog-simulator
362 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
363 "*Program and arguments to use to interpret Verilog source.
364 Depending on the `verilog-set-compile-command', this may be invoked when
365 you type \\[compile]. When the compile completes, \\[next-error] will take
366 you to the next lint error."
367 :type 'string
368 :group 'verilog-mode-actions)
369 ;; We don't mark it safe, as it's used as a shell command
370
371 (defcustom verilog-compiler
372 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
373 "*Program and arguments to use to compile Verilog source.
374 Depending on the `verilog-set-compile-command', this may be invoked when
375 you type \\[compile]. When the compile completes, \\[next-error] will take
376 you to the next lint error."
377 :type 'string
378 :group 'verilog-mode-actions)
379 ;; We don't mark it safe, as it's used as a shell command
380
381 (defvar verilog-tool 'verilog-linter
382 "Which tool to use for building compiler-command.
383 Either nil, `verilog-linter, `verilog-coverage, `verilog-simulator, or
384 `verilog-compiler. Alternatively use the \"Choose Compilation Action\"
385 menu. See `verilog-set-compile-command' for more information.")
386
387 (defcustom verilog-highlight-translate-off nil
388 "*Non-nil means background-highlight code excluded from translation.
389 That is, all code between \"// synopsys translate_off\" and
390 \"// synopsys translate_on\" is highlighted using a different background color
391 \(face `verilog-font-lock-translate-off-face').
392
393 Note: This will slow down on-the-fly fontification (and thus editing).
394
395 Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
396 entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
397 :type 'boolean
398 :group 'verilog-mode-indent)
399 ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
400 (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
401
402 (defcustom verilog-auto-lineup 'declarations
403 "*Type of statements to lineup across multiple lines.
404 If 'all' is selected, then all line ups described below are done.
405
406 If 'declaration', then just declarations are lined up with any
407 preceding declarations, taking into account widths and the like,
408 so or example the code:
409 reg [31:0] a;
410 reg b;
411 would become
412 reg [31:0] a;
413 reg b;
414
415 If 'assignment', then assignments are lined up with any preceding
416 assignments, so for example the code
417 a_long_variable <= b + c;
418 d = e + f;
419 would become
420 a_long_variable <= b + c;
421 d = e + f;
422
423 In order to speed up editing, large blocks of statements are lined up
424 only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
425 are lineup only when \\[verilog-pretty-declarations] is typed."
426
427 :type '(radio (const :tag "Line up Assignments and Declarations" all)
428 (const :tag "Line up Assignment statements" assignments )
429 (const :tag "Line up Declarations" declarations)
430 (function :tag "Other"))
431 :group 'verilog-mode-indent )
432
433 (defcustom verilog-indent-level 3
434 "*Indentation of Verilog statements with respect to containing block."
435 :group 'verilog-mode-indent
436 :type 'integer)
437 (put 'verilog-indent-level 'safe-local-variable 'integerp)
438
439 (defcustom verilog-indent-level-module 3
440 "*Indentation of Module level Verilog statements (eg always, initial).
441 Set to 0 to get initial and always statements lined up on the left side of
442 your screen."
443 :group 'verilog-mode-indent
444 :type 'integer)
445 (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
446
447 (defcustom verilog-indent-level-declaration 3
448 "*Indentation of declarations with respect to containing block.
449 Set to 0 to get them list right under containing block."
450 :group 'verilog-mode-indent
451 :type 'integer)
452 (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
453
454 (defcustom verilog-indent-declaration-macros nil
455 "*How to treat macro expansions in a declaration.
456 If nil, indent as:
457 input [31:0] a;
458 input `CP;
459 output c;
460 If non nil, treat as:
461 input [31:0] a;
462 input `CP ;
463 output c;"
464 :group 'verilog-mode-indent
465 :type 'boolean)
466 (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
467
468 (defcustom verilog-indent-lists t
469 "*How to treat indenting items in a list.
470 If t (the default), indent as:
471 always @( posedge a or
472 reset ) begin
473
474 If nil, treat as:
475 always @( posedge a or
476 reset ) begin"
477 :group 'verilog-mode-indent
478 :type 'boolean)
479 (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
480
481 (defcustom verilog-indent-level-behavioral 3
482 "*Absolute indentation of first begin in a task or function block.
483 Set to 0 to get such code to start at the left side of the screen."
484 :group 'verilog-mode-indent
485 :type 'integer)
486 (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
487
488 (defcustom verilog-indent-level-directive 1
489 "*Indentation to add to each level of `ifdef declarations.
490 Set to 0 to have all directives start at the left side of the screen."
491 :group 'verilog-mode-indent
492 :type 'integer)
493 (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
494
495 (defcustom verilog-cexp-indent 2
496 "*Indentation of Verilog statements split across lines."
497 :group 'verilog-mode-indent
498 :type 'integer)
499 (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
500
501 (defcustom verilog-case-indent 2
502 "*Indentation for case statements."
503 :group 'verilog-mode-indent
504 :type 'integer)
505 (put 'verilog-case-indent 'safe-local-variable 'integerp)
506
507 (defcustom verilog-auto-newline t
508 "*True means automatically newline after semicolons."
509 :group 'verilog-mode-indent
510 :type 'boolean)
511 (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
512
513 (defcustom verilog-auto-indent-on-newline t
514 "*True means automatically indent line after newline."
515 :group 'verilog-mode-indent
516 :type 'boolean)
517 (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
518
519 (defcustom verilog-tab-always-indent t
520 "*True means TAB should always re-indent the current line.
521 A nil value means TAB will only reindent when at the beginning of the line."
522 :group 'verilog-mode-indent
523 :type 'boolean)
524 (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
525
526 (defcustom verilog-tab-to-comment nil
527 "*True means TAB moves to the right hand column in preparation for a comment."
528 :group 'verilog-mode-actions
529 :type 'boolean)
530 (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
531
532 (defcustom verilog-indent-begin-after-if t
533 "*If true, indent begin statements following if, else, while, for and repeat.
534 Otherwise, line them up."
535 :group 'verilog-mode-indent
536 :type 'boolean)
537 (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
538
539
540 (defcustom verilog-align-ifelse nil
541 "*If true, align `else' under matching `if'.
542 Otherwise else is lined up with first character on line holding matching if."
543 :group 'verilog-mode-indent
544 :type 'boolean)
545 (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
546
547 (defcustom verilog-minimum-comment-distance 10
548 "*Minimum distance (in lines) between begin and end required before a comment.
549 Setting this variable to zero results in every end acquiring a comment; the
550 default avoids too many redundant comments in tight quarters."
551 :group 'verilog-mode-indent
552 :type 'integer)
553 (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
554
555 (defcustom verilog-highlight-p1800-keywords nil
556 "*True means highlight words newly reserved by IEEE-1800.
557 These will appear in `verilog-font-lock-p1800-face' in order to gently
558 suggest changing where these words are used as variables to something else.
559 A nil value means highlight these words as appropriate for the SystemVerilog
560 IEEE-1800 standard. Note that changing this will require restarting Emacs
561 to see the effect as font color choices are cached by Emacs."
562 :group 'verilog-mode-indent
563 :type 'boolean)
564 (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
565
566 (defcustom verilog-highlight-grouping-keywords nil
567 "*True means highlight grouping keywords 'begin' and 'end' more dramatically.
568 If false, these words are in the `font-lock-type-face'; if True then they are in
569 `verilog-font-lock-ams-face'. Some find that special highlighting on these
570 grouping constructs allow the structure of the code to be understood at a glance."
571 :group 'verilog-mode-indent
572 :type 'boolean)
573 (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
574
575 (defcustom verilog-auto-endcomments t
576 "*True means insert a comment /* ... */ after 'end's.
577 The name of the function or case will be set between the braces."
578 :group 'verilog-mode-actions
579 :type 'boolean)
580 (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
581
582 (defcustom verilog-auto-ignore-concat nil
583 "*True means ignore signals in {...} concatenations for AUTOWIRE etc.
584 This will exclude signals referenced as pin connections in {...}
585 from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
586 for backward compatibility only and not set in new designs; it
587 may be removed in future versions."
588 :group 'verilog-mode-actions
589 :type 'boolean)
590 (put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
591
592 (defcustom verilog-auto-read-includes nil
593 "*True means to automatically read includes before AUTOs.
594 This will do a `verilog-read-defines' and `verilog-read-includes' before
595 each AUTO expansion. This makes it easier to embed defines and includes,
596 but can result in very slow reading times if there are many or large
597 include files."
598 :group 'verilog-mode-actions
599 :type 'boolean)
600 (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
601
602 (defcustom verilog-auto-save-policy nil
603 "*Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
604 A value of `force' will always do a \\[verilog-auto] automatically if
605 needed on every save. A value of `detect' will do \\[verilog-auto]
606 automatically when it thinks necessary. A value of `ask' will query the
607 user when it thinks updating is needed.
608
609 You should not rely on the 'ask or 'detect policies, they are safeguards
610 only. They do not detect when AUTOINSTs need to be updated because a
611 sub-module's port list has changed."
612 :group 'verilog-mode-actions
613 :type '(choice (const nil) (const ask) (const detect) (const force)))
614
615 (defcustom verilog-auto-star-expand t
616 "*Non-nil indicates to expand a SystemVerilog .* instance ports.
617 They will be expanded in the same way as if there was a AUTOINST in the
618 instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
619 :group 'verilog-mode-actions
620 :type 'boolean)
621 (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
622
623 (defcustom verilog-auto-star-save nil
624 "*Non-nil indicates to save to disk SystemVerilog .* instance expansions.
625 A nil value indicates direct connections will be removed before saving.
626 Only meaningful to those created due to `verilog-auto-star-expand' being set.
627
628 Instead of setting this, you may want to use /*AUTOINST*/, which will
629 always be saved."
630 :group 'verilog-mode-actions
631 :type 'boolean)
632 (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
633
634 (defvar verilog-auto-update-tick nil
635 "Modification tick at which autos were last performed.")
636
637 (defvar verilog-auto-last-file-locals nil
638 "Text from file-local-variables during last evaluation.")
639
640 ;;; Compile support
641 (require 'compile)
642 (defvar verilog-error-regexp-added nil)
643 ; List of regexps for Verilog compilers, like verilint. See compilation-error-regexp-alist
644 ; for the formatting.
645 ; Here is the version for Emacs 22:
646 (defvar verilog-error-regexp-emacs-alist
647 '(
648 (verilog-xl-1
649 "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
650 (verilog-xl-2
651 "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
652 (verilog-IES
653 ".*\\*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)" 1 2)
654 (verilog-surefire-1
655 "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
656 (verilog-surefire-2
657 "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
658 (verilog-verbose
659 "\
660 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
661 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
662 (verilog-xsim
663 "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
664 (verilog-vcs-1
665 "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
666 (verilog-vcs-2
667 "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
668 (verilog-vcs-3
669 "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
670 (verilog-vcs-4
671 "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
672 (verilog-verilator
673 "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
674 (verilog-leda
675 "In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):
676 .*
677 .*
678 .*
679 \\(Warning\\|Error\\|Failure\\)" 1 2)
680 ))
681 ;; And the version for XEmacs:
682 (defvar verilog-error-regexp-xemacs-alist
683 '(verilog
684 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
685 ("\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
686 ("\
687 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
688 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
689 ; xsim
690 ; Error! in file /homes/mac/Axis/Xsim/test.v at line 13 [OBJ_NOT_DECLARED]
691 ("\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
692 ; vcs
693 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
694 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
695 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
696 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
697 ; Verilator
698 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
699 ; verilog-xl
700 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
701 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\([0-9]+\\):.*$" 1 2) ; vxl
702 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+line[ \t]+\\([0-9]+\\):.*$" 1 2)
703 ; nc-verilog
704 (".*\\*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)|" 1 2)
705 ; Leda
706 ("In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\[\\(Warning\\|Error\\|Failure\\)\\][^\n]*" 1 2)
707 )
708 )
709
710 (defvar verilog-error-font-lock-keywords
711 '(
712 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
713 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
714
715 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
716 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
717
718 ("\
719 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
720 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
721 ("\
722 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
723 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
724
725 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
726 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
727
728 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
729 ("%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
730
731 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
732 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
733
734 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
735 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
736
737 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
738 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
739 ; vxl
740 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
741 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
742
743 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\([0-9]+\\):.*$" 1 bold t)
744 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\([0-9]+\\):.*$" 2 bold t)
745
746 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+line[ \t]+\\([0-9]+\\):.*$" 1 bold t)
747 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+line[ \t]+\\([0-9]+\\):.*$" 2 bold t)
748 ; nc-verilog
749 (".*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)|" 1 bold t)
750 (".*[WE],[0-9A-Z]+ (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
751 ; Leda
752 ("In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\[\\(Warning\\|Error\\|Failure\\)\\][^\n]*" 1 bold t)
753 ("In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\[\\(Warning\\|Error\\|Failure\\)\\][^\n]*" 2 bold t)
754 )
755 "*Keywords to also highlight in Verilog *compilation* buffers.")
756
757 (defcustom verilog-library-flags '("")
758 "*List of standard Verilog arguments to use for /*AUTOINST*/.
759 These arguments are used to find files for `verilog-auto', and match
760 the flags accepted by a standard Verilog-XL simulator.
761
762 -f filename Reads more `verilog-library-flags' from the filename.
763 +incdir+dir Adds the directory to `verilog-library-directories'.
764 -Idir Adds the directory to `verilog-library-directories'.
765 -y dir Adds the directory to `verilog-library-directories'.
766 +libext+.v Adds the extensions to `verilog-library-extensions'.
767 -v filename Adds the filename to `verilog-library-files'.
768
769 filename Adds the filename to `verilog-library-files'.
770 This is not recommended, -v is a better choice.
771
772 You might want these defined in each file; put at the *END* of your file
773 something like:
774
775 // Local Variables:
776 // verilog-library-flags:(\"-y dir -y otherdir\")
777 // End:
778
779 Verilog-mode attempts to detect changes to this local variable, but they
780 are only insured to be correct when the file is first visited. Thus if you
781 have problems, use \\[find-alternate-file] RET to have these take effect.
782
783 See also the variables mentioned above."
784 :group 'verilog-mode-auto
785 :type '(repeat string))
786 (put 'verilog-library-flags 'safe-local-variable 'listp)
787
788 (defcustom verilog-library-directories '(".")
789 "*List of directories when looking for files for /*AUTOINST*/.
790 The directory may be relative to the current file, or absolute.
791 Environment variables are also expanded in the directory names.
792 Having at least the current directory is a good idea.
793
794 You might want these defined in each file; put at the *END* of your file
795 something like:
796
797 // Local Variables:
798 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
799 // End:
800
801 Verilog-mode attempts to detect changes to this local variable, but they
802 are only insured to be correct when the file is first visited. Thus if you
803 have problems, use \\[find-alternate-file] RET to have these take effect.
804
805 See also `verilog-library-flags', `verilog-library-files'
806 and `verilog-library-extensions'."
807 :group 'verilog-mode-auto
808 :type '(repeat file))
809 (put 'verilog-library-directories 'safe-local-variable 'listp)
810
811 (defcustom verilog-library-files '()
812 "*List of files to search for modules.
813 AUTOINST will use this when it needs to resolve a module name.
814 This is a complete path, usually to a technology file with many standard
815 cells defined in it.
816
817 You might want these defined in each file; put at the *END* of your file
818 something like:
819
820 // Local Variables:
821 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
822 // End:
823
824 Verilog-mode attempts to detect changes to this local variable, but they
825 are only insured to be correct when the file is first visited. Thus if you
826 have problems, use \\[find-alternate-file] RET to have these take effect.
827
828 See also `verilog-library-flags', `verilog-library-directories'."
829 :group 'verilog-mode-auto
830 :type '(repeat directory))
831 (put 'verilog-library-files 'safe-local-variable 'listp)
832
833 (defcustom verilog-library-extensions '(".v" ".sv")
834 "*List of extensions to use when looking for files for /*AUTOINST*/.
835 See also `verilog-library-flags', `verilog-library-directories'."
836 :type '(repeat string)
837 :group 'verilog-mode-auto)
838 (put 'verilog-library-extensions 'safe-local-variable 'listp)
839
840 (defcustom verilog-active-low-regexp nil
841 "*If set, treat signals matching this regexp as active low.
842 This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
843 you will probably also need `verilog-auto-reset-widths' set."
844 :group 'verilog-mode-auto
845 :type 'string)
846 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
847
848 (defcustom verilog-auto-sense-include-inputs nil
849 "*If true, AUTOSENSE should include all inputs.
850 If nil, only inputs that are NOT output signals in the same block are
851 included."
852 :group 'verilog-mode-auto
853 :type 'boolean)
854 (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
855
856 (defcustom verilog-auto-sense-defines-constant nil
857 "*If true, AUTOSENSE should assume all defines represent constants.
858 When true, the defines will not be included in sensitivity lists. To
859 maintain compatibility with other sites, this should be set at the bottom
860 of each Verilog file that requires it, rather than being set globally."
861 :group 'verilog-mode-auto
862 :type 'boolean)
863 (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
864
865 (defcustom verilog-auto-reset-widths t
866 "*If true, AUTORESET should determine the width of signals.
867 This is then used to set the width of the zero (32'h0 for example). This
868 is required by some lint tools that aren't smart enough to ignore widths of
869 the constant zero. This may result in ugly code when parameters determine
870 the MSB or LSB of a signal inside an AUTORESET."
871 :type 'boolean
872 :group 'verilog-mode-auto)
873 (put 'verilog-auto-reset-widths 'safe-local-variable 'verilog-booleanp)
874
875 (defcustom verilog-assignment-delay ""
876 "*Text used for delays in delayed assignments. Add a trailing space if set."
877 :group 'verilog-mode-auto
878 :type 'string)
879 (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
880
881 (defcustom verilog-auto-arg-sort nil
882 "*If set, AUTOARG signal names will be sorted, not in delaration order.
883 Declaration order is advantageous with order based instantiations
884 and is the default for backward compatibility. Sorted order
885 reduces changes when declarations are moved around in a file, and
886 it's bad practice to rely on order based instantiations anyhow."
887 :group 'verilog-mode-auto
888 :type 'boolean)
889 (put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
890
891 (defcustom verilog-auto-inst-param-value nil
892 "*If set, AUTOINST will replace parameters with the parameter value.
893 If nil, leave parameters as symbolic names.
894
895 Parameters must be in Verilog 2001 format #(...), and if a parameter is not
896 listed as such there (as when the default value is acceptable), it will not
897 be replaced, and will remain symbolic.
898
899 For example, imagine a submodule uses parameters to declare the size of its
900 inputs. This is then used by a upper module:
901
902 module InstModule (o,i)
903 parameter WIDTH;
904 input [WIDTH-1:0] i;
905 endmodule
906
907 module ExampInst;
908 InstModule
909 #(PARAM(10))
910 instName
911 (/*AUTOINST*/
912 .i (i[PARAM-1:0]));
913
914 Note even though PARAM=10, the AUTOINST has left the parameter as a
915 symbolic name. If `verilog-auto-inst-param-value' is set, this will
916 instead expand to:
917
918 module ExampInst;
919 InstModule
920 #(PARAM(10))
921 instName
922 (/*AUTOINST*/
923 .i (i[9:0]));"
924 :group 'verilog-mode-auto
925 :type 'boolean)
926 (put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
927
928 (defcustom verilog-auto-inst-vector t
929 "*If true, when creating default ports with AUTOINST, use bus subscripts.
930 If nil, skip the subscript when it matches the entire bus as declared in
931 the module (AUTOWIRE signals always are subscripted, you must manually
932 declare the wire to have the subscripts removed.) Setting this to nil may
933 speed up some simulators, but is less general and harder to read, so avoid."
934 :group 'verilog-mode-auto
935 :type 'boolean)
936 (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
937
938 (defcustom verilog-auto-inst-template-numbers nil
939 "*If true, when creating templated ports with AUTOINST, add a comment.
940 The comment will add the line number of the template that was used for that
941 port declaration. Setting this aids in debugging, but nil is suggested for
942 regular use to prevent large numbers of merge conflicts."
943 :group 'verilog-mode-auto
944 :type 'boolean)
945 (put 'verilog-auto-inst-template-numbers 'safe-local-variable 'verilog-booleanp)
946
947 (defcustom verilog-auto-inst-column 40
948 "*Indent-to column number for net name part of AUTOINST created pin."
949 :group 'verilog-mode-indent
950 :type 'integer)
951 (put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
952
953 (defcustom verilog-auto-input-ignore-regexp nil
954 "*If set, when creating AUTOINPUT list, ignore signals matching this regexp.
955 See the \\[verilog-faq] for examples on using this."
956 :group 'verilog-mode-auto
957 :type 'string)
958 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
959
960 (defcustom verilog-auto-inout-ignore-regexp nil
961 "*If set, when creating AUTOINOUT list, ignore signals matching this regexp.
962 See the \\[verilog-faq] for examples on using this."
963 :group 'verilog-mode-auto
964 :type 'string)
965 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
966
967 (defcustom verilog-auto-output-ignore-regexp nil
968 "*If set, when creating AUTOOUTPUT list, ignore signals matching this regexp.
969 See the \\[verilog-faq] for examples on using this."
970 :group 'verilog-mode-auto
971 :type 'string)
972 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
973
974 (defcustom verilog-auto-unused-ignore-regexp nil
975 "*If set, when creating AUTOUNUSED list, ignore signals matching this regexp.
976 See the \\[verilog-faq] for examples on using this."
977 :group 'verilog-mode-auto
978 :type 'string)
979 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
980
981 (defcustom verilog-typedef-regexp nil
982 "*If non-nil, regular expression that matches Verilog-2001 typedef names.
983 For example, \"_t$\" matches typedefs named with _t, as in the C language."
984 :group 'verilog-mode-auto
985 :type 'string)
986 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
987
988 (defcustom verilog-mode-hook 'verilog-set-compile-command
989 "*Hook run after Verilog mode is loaded."
990 :type 'hook
991 :group 'verilog-mode)
992
993 (defcustom verilog-auto-hook nil
994 "*Hook run after `verilog-mode' updates AUTOs."
995 :group 'verilog-mode-auto
996 :type 'hook)
997
998 (defcustom verilog-before-auto-hook nil
999 "*Hook run before `verilog-mode' updates AUTOs."
1000 :group 'verilog-mode-auto
1001 :type 'hook)
1002
1003 (defcustom verilog-delete-auto-hook nil
1004 "*Hook run after `verilog-mode' deletes AUTOs."
1005 :group 'verilog-mode-auto
1006 :type 'hook)
1007
1008 (defcustom verilog-before-delete-auto-hook nil
1009 "*Hook run before `verilog-mode' deletes AUTOs."
1010 :group 'verilog-mode-auto
1011 :type 'hook)
1012
1013 (defcustom verilog-getopt-flags-hook nil
1014 "*Hook run after `verilog-getopt-flags' determines the Verilog option lists."
1015 :group 'verilog-mode-auto
1016 :type 'hook)
1017
1018 (defcustom verilog-before-getopt-flags-hook nil
1019 "*Hook run before `verilog-getopt-flags' determines the Verilog option lists."
1020 :group 'verilog-mode-auto
1021 :type 'hook)
1022
1023 (defvar verilog-imenu-generic-expression
1024 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
1025 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
1026 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
1027
1028 ;;
1029 ;; provide a verilog-header function.
1030 ;; Customization variables:
1031 ;;
1032 (defvar verilog-date-scientific-format nil
1033 "*If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1034 If nil, in European format (e.g. 17.09.1997). The brain-dead American
1035 format (e.g. 09/17/1997) is not supported.")
1036
1037 (defvar verilog-company nil
1038 "*Default name of Company for Verilog header.
1039 If set will become buffer local.")
1040 (make-variable-buffer-local 'verilog-company)
1041
1042 (defvar verilog-project nil
1043 "*Default name of Project for Verilog header.
1044 If set will become buffer local.")
1045 (make-variable-buffer-local 'verilog-project)
1046
1047 (defvar verilog-mode-map
1048 (let ((map (make-sparse-keymap)))
1049 (define-key map ";" 'electric-verilog-semi)
1050 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1051 (define-key map ":" 'electric-verilog-colon)
1052 ;;(define-key map "=" 'electric-verilog-equal)
1053 (define-key map "\`" 'electric-verilog-tick)
1054 (define-key map "\t" 'electric-verilog-tab)
1055 (define-key map "\r" 'electric-verilog-terminate-line)
1056 ;; backspace/delete key bindings
1057 (define-key map [backspace] 'backward-delete-char-untabify)
1058 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1059 (define-key map [delete] 'delete-char)
1060 (define-key map [(meta delete)] 'kill-word))
1061 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1062 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1063 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1064 (define-key map "\M-\t" 'verilog-complete-word)
1065 (define-key map "\M-?" 'verilog-show-completions)
1066 (define-key map "\C-c\`" 'verilog-lint-off)
1067 (define-key map "\C-c\*" 'verilog-delete-auto-star-implicit)
1068 (define-key map "\C-c\C-r" 'verilog-label-be)
1069 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1070 (define-key map "\C-c=" 'verilog-pretty-expr)
1071 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1072 (define-key map "\M-*" 'verilog-star-comment)
1073 (define-key map "\C-c\C-c" 'verilog-comment-region)
1074 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
1075 (when (featurep 'xemacs)
1076 (define-key map [(meta control h)] 'verilog-mark-defun)
1077 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1078 (define-key map "\M-\C-e" 'verilog-end-of-defun))
1079 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1080 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1081 (define-key map "\C-c\C-a" 'verilog-auto)
1082 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
1083 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1084 (define-key map "\C-c\C-e" 'verilog-expand-vector)
1085 (define-key map "\C-c\C-h" 'verilog-header)
1086 map)
1087 "Keymap used in Verilog mode.")
1088
1089 ;; menus
1090 (easy-menu-define
1091 verilog-menu verilog-mode-map "Menu for Verilog mode"
1092 (verilog-easy-menu-filter
1093 '("Verilog"
1094 ("Choose Compilation Action"
1095 ["None"
1096 (progn
1097 (setq verilog-tool nil)
1098 (verilog-set-compile-command))
1099 :style radio
1100 :selected (equal verilog-tool nil)
1101 :help "When invoking compilation, use compile-command"]
1102 ["Lint"
1103 (progn
1104 (setq verilog-tool 'verilog-linter)
1105 (verilog-set-compile-command))
1106 :style radio
1107 :selected (equal verilog-tool `verilog-linter)
1108 :help "When invoking compilation, use lint checker"]
1109 ["Coverage"
1110 (progn
1111 (setq verilog-tool 'verilog-coverage)
1112 (verilog-set-compile-command))
1113 :style radio
1114 :selected (equal verilog-tool `verilog-coverage)
1115 :help "When invoking compilation, annotate for coverage"]
1116 ["Simulator"
1117 (progn
1118 (setq verilog-tool 'verilog-simulator)
1119 (verilog-set-compile-command))
1120 :style radio
1121 :selected (equal verilog-tool `verilog-simulator)
1122 :help "When invoking compilation, interpret Verilog source"]
1123 ["Compiler"
1124 (progn
1125 (setq verilog-tool 'verilog-compiler)
1126 (verilog-set-compile-command))
1127 :style radio
1128 :selected (equal verilog-tool `verilog-compiler)
1129 :help "When invoking compilation, compile Verilog source"]
1130 )
1131 ("Move"
1132 ["Beginning of function" verilog-beg-of-defun
1133 :keys "C-M-a"
1134 :help "Move backward to the beginning of the current function or procedure"]
1135 ["End of function" verilog-end-of-defun
1136 :keys "C-M-e"
1137 :help "Move forward to the end of the current function or procedure"]
1138 ["Mark function" verilog-mark-defun
1139 :keys "C-M-h"
1140 :help "Mark the current Verilog function or procedure"]
1141 ["Goto function/module" verilog-goto-defun
1142 :help "Move to specified Verilog module/task/function"]
1143 ["Move to beginning of block" electric-verilog-backward-sexp
1144 :help "Move backward over one balanced expression"]
1145 ["Move to end of block" electric-verilog-forward-sexp
1146 :help "Move forward over one balanced expression"]
1147 )
1148 ("Comments"
1149 ["Comment Region" verilog-comment-region
1150 :help "Put marked area into a comment"]
1151 ["UnComment Region" verilog-uncomment-region
1152 :help "Uncomment an area commented with Comment Region"]
1153 ["Multi-line comment insert" verilog-star-comment
1154 :help "Insert Verilog /* */ comment at point"]
1155 ["Lint error to comment" verilog-lint-off
1156 :help "Convert a Verilog linter warning line into a disable statement"]
1157 )
1158 "----"
1159 ["Compile" compile
1160 :help "Perform compilation-action (above) on the current buffer"]
1161 ["AUTO, Save, Compile" verilog-auto-save-compile
1162 :help "Recompute AUTOs, save buffer, and compile"]
1163 ["Next Compile Error" next-error
1164 :help "Visit next compilation error message and corresponding source code"]
1165 ["Ignore Lint Warning at point" verilog-lint-off
1166 :help "Convert a Verilog linter warning line into a disable statement"]
1167 "----"
1168 ["Line up declarations around point" verilog-pretty-declarations
1169 :help "Line up declarations around point"]
1170 ["Line up equations around point" verilog-pretty-expr
1171 :help "Line up expressions around point"]
1172 ["Redo/insert comments on every end" verilog-label-be
1173 :help "Label matching begin ... end statements"]
1174 ["Expand [x:y] vector line" verilog-expand-vector
1175 :help "Take a signal vector on the current line and expand it to multiple lines"]
1176 ["Insert begin-end block" verilog-insert-block
1177 :help "Insert begin ... end"]
1178 ["Complete word" verilog-complete-word
1179 :help "Complete word at point"]
1180 "----"
1181 ["Recompute AUTOs" verilog-auto
1182 :help "Expand AUTO meta-comment statements"]
1183 ["Kill AUTOs" verilog-delete-auto
1184 :help "Remove AUTO expansions"]
1185 ["Inject AUTOs" verilog-inject-auto
1186 :help "Inject AUTOs into legacy non-AUTO buffer"]
1187 ("AUTO Help..."
1188 ["AUTO General" (describe-function 'verilog-auto)
1189 :help "Help introduction on AUTOs"]
1190 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1191 :help "Help on verilog-library-flags"]
1192 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1193 :help "Help on verilog-library-directories"]
1194 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1195 :help "Help on verilog-library-files"]
1196 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1197 :help "Help on verilog-library-extensions"]
1198 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1199 :help "Help on reading `defines"]
1200 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1201 :help "Help on parsing `includes"]
1202 ["AUTOARG" (describe-function 'verilog-auto-arg)
1203 :help "Help on AUTOARG - declaring module port list"]
1204 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1205 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1206 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
1207 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
1208 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1209 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1210 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1211 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
1212 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1213 :help "Help on AUTOINOUT - adding inouts from cells"]
1214 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1215 :help "Help on AUTOINPUT - adding inputs from cells"]
1216 ["AUTOINST" (describe-function 'verilog-auto-inst)
1217 :help "Help on AUTOINST - adding pins for cells"]
1218 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1219 :help "Help on expanding Verilog-2001 .* pins"]
1220 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1221 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1222 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1223 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1224 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1225 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1226 ["AUTOREG" (describe-function 'verilog-auto-reg)
1227 :help "Help on AUTOREG - declaring registers for non-wires"]
1228 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1229 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1230 ["AUTORESET" (describe-function 'verilog-auto-reset)
1231 :help "Help on AUTORESET - resetting always blocks"]
1232 ["AUTOSENSE" (describe-function 'verilog-auto-sense)
1233 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1234 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1235 :help "Help on AUTOTIEOFF - tieing off unused outputs"]
1236 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1237 :help "Help on AUTOUNUSED - terminating unused inputs"]
1238 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1239 :help "Help on AUTOWIRE - declaring wires for cells"]
1240 )
1241 "----"
1242 ["Submit bug report" verilog-submit-bug-report
1243 :help "Submit via mail a bug report on verilog-mode.el"]
1244 ["Version and FAQ" verilog-faq
1245 :help "Show the current version, and where to get the FAQ etc"]
1246 ["Customize Verilog Mode..." verilog-customize
1247 :help "Customize variables and other settings used by Verilog-Mode"]
1248 ["Customize Verilog Fonts & Colors" verilog-font-customize
1249 :help "Customize fonts used by Verilog-Mode."])))
1250
1251 (easy-menu-define
1252 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1253 (verilog-easy-menu-filter
1254 '("Statements"
1255 ["Header" verilog-sk-header
1256 :help "Insert a header block at the top of file"]
1257 ["Comment" verilog-sk-comment
1258 :help "Insert a comment block"]
1259 "----"
1260 ["Module" verilog-sk-module
1261 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1262 ["Primitive" verilog-sk-primitive
1263 :help "Insert a primitive .. (.. );.. endprimitive block"]
1264 "----"
1265 ["Input" verilog-sk-input
1266 :help "Insert an input declaration"]
1267 ["Output" verilog-sk-output
1268 :help "Insert an output declaration"]
1269 ["Inout" verilog-sk-inout
1270 :help "Insert an inout declaration"]
1271 ["Wire" verilog-sk-wire
1272 :help "Insert a wire declaration"]
1273 ["Reg" verilog-sk-reg
1274 :help "Insert a register declaration"]
1275 ["Define thing under point as a register" verilog-sk-define-signal
1276 :help "Define signal under point as a register at the top of the module"]
1277 "----"
1278 ["Initial" verilog-sk-initial
1279 :help "Insert an initial begin .. end block"]
1280 ["Always" verilog-sk-always
1281 :help "Insert an always @(AS) begin .. end block"]
1282 ["Function" verilog-sk-function
1283 :help "Insert a function .. begin .. end endfunction block"]
1284 ["Task" verilog-sk-task
1285 :help "Insert a task .. begin .. end endtask block"]
1286 ["Specify" verilog-sk-specify
1287 :help "Insert a specify .. endspecify block"]
1288 ["Generate" verilog-sk-generate
1289 :help "Insert a generate .. endgenerate block"]
1290 "----"
1291 ["Begin" verilog-sk-begin
1292 :help "Insert a begin .. end block"]
1293 ["If" verilog-sk-if
1294 :help "Insert an if (..) begin .. end block"]
1295 ["(if) else" verilog-sk-else-if
1296 :help "Insert an else if (..) begin .. end block"]
1297 ["For" verilog-sk-for
1298 :help "Insert a for (...) begin .. end block"]
1299 ["While" verilog-sk-while
1300 :help "Insert a while (...) begin .. end block"]
1301 ["Fork" verilog-sk-fork
1302 :help "Insert a fork begin .. end .. join block"]
1303 ["Repeat" verilog-sk-repeat
1304 :help "Insert a repeat (..) begin .. end block"]
1305 ["Case" verilog-sk-case
1306 :help "Insert a case block, prompting for details"]
1307 ["Casex" verilog-sk-casex
1308 :help "Insert a casex (...) item: begin.. end endcase block"]
1309 ["Casez" verilog-sk-casez
1310 :help "Insert a casez (...) item: begin.. end endcase block"])))
1311
1312 (defvar verilog-mode-abbrev-table nil
1313 "Abbrev table in use in Verilog-mode buffers.")
1314
1315 (define-abbrev-table 'verilog-mode-abbrev-table ())
1316
1317 ;;
1318 ;; Macros
1319 ;;
1320
1321 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1322 "Replace occurrences of FROM-STRING with TO-STRING.
1323 FIXEDCASE and LITERAL as in `replace-match`. STRING is what to replace.
1324 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1325 will break, as the o's continuously replace. xa -> x works ok though."
1326 ;; Hopefully soon to a emacs built-in
1327 (let ((start 0))
1328 (while (string-match from-string string start)
1329 (setq string (replace-match to-string fixedcase literal string)
1330 start (min (length string) (+ (match-beginning 0) (length to-string)))))
1331 string))
1332
1333 (defsubst verilog-string-remove-spaces (string)
1334 "Remove spaces surrounding STRING."
1335 (save-match-data
1336 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1337 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1338 string))
1339
1340 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1341 ; checkdoc-params: (REGEXP BOUND NOERROR)
1342 "Like `re-search-forward', but skips over match in comments or strings."
1343 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1344 (while (and
1345 (re-search-forward REGEXP BOUND NOERROR)
1346 (setq mdata (match-data))
1347 (and (verilog-skip-forward-comment-or-string)
1348 (progn
1349 (setq mdata '(nil nil))
1350 (if BOUND
1351 (< (point) BOUND)
1352 t)))))
1353 (store-match-data mdata)
1354 (match-end 0)))
1355
1356 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1357 ; checkdoc-params: (REGEXP BOUND NOERROR)
1358 "Like `re-search-backward', but skips over match in comments or strings."
1359 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1360 (while (and
1361 (re-search-backward REGEXP BOUND NOERROR)
1362 (setq mdata (match-data))
1363 (and (verilog-skip-backward-comment-or-string)
1364 (progn
1365 (setq mdata '(nil nil))
1366 (if BOUND
1367 (> (point) BOUND)
1368 t)))))
1369 (store-match-data mdata)
1370 (match-end 0)))
1371
1372 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1373 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1374 but trashes match data and is faster for REGEXP that doesn't match often.
1375 This may at some point use text properties to ignore comments,
1376 so there may be a large up front penalty for the first search."
1377 (let (pt)
1378 (while (and (not pt)
1379 (re-search-forward regexp bound noerror))
1380 (if (not (verilog-inside-comment-p))
1381 (setq pt (match-end 0))))
1382 pt))
1383
1384 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1385 ; checkdoc-params: (REGEXP BOUND NOERROR)
1386 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1387 but trashes match data and is faster for REGEXP that doesn't match often.
1388 This may at some point use text properties to ignore comments,
1389 so there may be a large up front penalty for the first search."
1390 (let (pt)
1391 (while (and (not pt)
1392 (re-search-backward regexp bound noerror))
1393 (if (not (verilog-inside-comment-p))
1394 (setq pt (match-end 0))))
1395 pt))
1396
1397 (defsubst verilog-get-beg-of-line (&optional arg)
1398 (save-excursion
1399 (beginning-of-line arg)
1400 (point)))
1401
1402 (defsubst verilog-get-end-of-line (&optional arg)
1403 (save-excursion
1404 (end-of-line arg)
1405 (point)))
1406
1407 (defsubst verilog-within-string ()
1408 (save-excursion
1409 (nth 3 (parse-partial-sexp (verilog-get-beg-of-line) (point)))))
1410
1411 (defvar compile-command)
1412
1413 ;; compilation program
1414 (defun verilog-set-compile-command ()
1415 "Function to compute shell command to compile Verilog.
1416
1417 This reads `verilog-tool' and sets `compile-command'. This specifies the
1418 program that executes when you type \\[compile] or
1419 \\[verilog-auto-save-compile].
1420
1421 By default `verilog-tool' uses a Makefile if one exists in the current
1422 directory. If not, it is set to the `verilog-linter', `verilog-coverage',
1423 `verilog-simulator', or `verilog-compiler' variables, as selected with the
1424 Verilog -> \"Choose Compilation Action\" menu.
1425
1426 You should set `verilog-tool' or the other variables to the path and
1427 arguments for your Verilog simulator. For example:
1428 \"vcs -p123 -O\"
1429 or a string like:
1430 \"(cd /tmp; surecov %s)\".
1431
1432 In the former case, the path to the current buffer is concat'ed to the
1433 value of `verilog-tool'; in the later, the path to the current buffer is
1434 substituted for the %s.
1435
1436 Where __FILE__ appears in the string, the `buffer-file-name' of the
1437 current buffer, without the directory portion, will be substituted."
1438 (interactive)
1439 (cond
1440 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1441 (file-exists-p "Makefile"))
1442 (make-local-variable 'compile-command)
1443 (setq compile-command "make "))
1444 (t
1445 (make-local-variable 'compile-command)
1446 (setq compile-command
1447 (if verilog-tool
1448 (if (string-match "%s" (eval verilog-tool))
1449 (format (eval verilog-tool) (or buffer-file-name ""))
1450 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1451 ""))))
1452 (verilog-modify-compile-command))
1453
1454 (defun verilog-modify-compile-command ()
1455 "Replace meta-information in `compile-command'.
1456 Where __FILE__ appears in the string, the current buffer's file-name,
1457 without the directory portion, will be substituted."
1458 (when (and
1459 (stringp compile-command)
1460 (string-match "\\b__FILE__\\b" compile-command))
1461 (make-local-variable 'compile-command)
1462 (setq compile-command
1463 (verilog-string-replace-matches
1464 "\\b__FILE__\\b" (file-name-nondirectory (buffer-file-name))
1465 t t compile-command))))
1466
1467 (if (featurep 'xemacs)
1468 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1469 (defun verilog-error-regexp-add-xemacs ()
1470 "Teach XEmacs about verilog errors.
1471 Called by `compilation-mode-hook'. This allows \\[next-error] to
1472 find the errors."
1473 (interactive)
1474 (if (boundp 'compilation-error-regexp-systems-alist)
1475 (if (and
1476 (not (equal compilation-error-regexp-systems-list 'all))
1477 (not (member compilation-error-regexp-systems-list 'verilog)))
1478 (push 'verilog compilation-error-regexp-systems-list)))
1479 (if (boundp 'compilation-error-regexp-alist-alist)
1480 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1481 (setcdr compilation-error-regexp-alist-alist
1482 (cons verilog-error-regexp-xemacs-alist
1483 (cdr compilation-error-regexp-alist-alist)))))
1484 (if (boundp 'compilation-font-lock-keywords)
1485 (progn
1486 (make-local-variable 'compilation-font-lock-keywords)
1487 (setq compilation-font-lock-keywords verilog-error-font-lock-keywords)
1488 (font-lock-set-defaults)))
1489 ;; Need to re-run compilation-error-regexp builder
1490 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1491 (compilation-build-compilation-error-regexp-alist))
1492 ))
1493
1494 ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
1495 (defun verilog-error-regexp-add-emacs ()
1496 "Tell Emacs compile that we are Verilog.
1497 Called by `compilation-mode-hook'. This allows \\[next-error] to
1498 find the errors."
1499 (interactive)
1500 (if (boundp 'compilation-error-regexp-alist-alist)
1501 (progn
1502 (if (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1503 (mapcar
1504 (lambda (item)
1505 (push (car item) compilation-error-regexp-alist)
1506 (push item compilation-error-regexp-alist-alist)
1507 )
1508 verilog-error-regexp-emacs-alist)))))
1509
1510 (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1511 (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
1512
1513 (defconst verilog-directive-re
1514 ;; "`case" "`default" "`define" "`define" "`else" "`endfor" "`endif"
1515 ;; "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1516 ;; "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1517 ;; "`time_scale" "`undef" "`while"
1518 "\\<`\\(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\\)\\>")
1519
1520 (defconst verilog-directive-re-1
1521 (concat "[ \t]*" verilog-directive-re))
1522
1523 (defconst verilog-directive-begin
1524 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1525
1526 (defconst verilog-directive-middle
1527 "\\<`\\(else\\|default\\|case\\)\\>")
1528
1529 (defconst verilog-directive-end
1530 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1531
1532 (defconst verilog-ovm-begin-re
1533 (eval-when-compile
1534 (verilog-regexp-opt
1535 '(
1536 "`ovm_component_utils_begin"
1537 "`ovm_component_param_utils_begin"
1538 "`ovm_field_utils_begin"
1539 "`ovm_object_utils_begin"
1540 "`ovm_object_param_utils_begin"
1541 "`ovm_sequence_utils_begin"
1542 "`ovm_sequencer_utils_begin"
1543 ) nil )))
1544
1545 (defconst verilog-ovm-end-re
1546 (eval-when-compile
1547 (verilog-regexp-opt
1548 '(
1549 "`ovm_component_utils_end"
1550 "`ovm_field_utils_end"
1551 "`ovm_object_utils_end"
1552 "`ovm_sequence_utils_end"
1553 "`ovm_sequencer_utils_end"
1554 ) nil )))
1555
1556 (defconst verilog-vmm-begin-re
1557 (eval-when-compile
1558 (verilog-regexp-opt
1559 '(
1560 "`vmm_data_member_begin"
1561 "`vmm_env_member_begin"
1562 "`vmm_scenario_member_begin"
1563 "`vmm_subenv_member_begin"
1564 "`vmm_xactor_member_begin"
1565 ) nil ) ) )
1566
1567 (defconst verilog-vmm-end-re
1568 (eval-when-compile
1569 (verilog-regexp-opt
1570 '(
1571 "`vmm_data_member_end"
1572 "`vmm_env_member_end"
1573 "`vmm_scenario_member_end"
1574 "`vmm_subenv_member_end"
1575 "`vmm_xactor_member_end"
1576 ) nil ) ) )
1577
1578 (defconst verilog-vmm-statement-re
1579 (eval-when-compile
1580 (verilog-regexp-opt
1581 '(
1582 ;; "`vmm_xactor_member_enum_array"
1583 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
1584 ;; "`vmm_xactor_member_scalar_array"
1585 ;; "`vmm_xactor_member_scalar"
1586 ) nil )))
1587
1588 (defconst verilog-ovm-statement-re
1589 (eval-when-compile
1590 (verilog-regexp-opt
1591 '(
1592 ;; Statements
1593 "`DUT_ERROR"
1594 "`MESSAGE"
1595 "`dut_error"
1596 "`message"
1597 "`ovm_analysis_imp_decl"
1598 "`ovm_blocking_get_imp_decl"
1599 "`ovm_blocking_get_peek_imp_decl"
1600 "`ovm_blocking_master_imp_decl"
1601 "`ovm_blocking_peek_imp_decl"
1602 "`ovm_blocking_put_imp_decl"
1603 "`ovm_blocking_slave_imp_decl"
1604 "`ovm_blocking_transport_imp_decl"
1605 "`ovm_component_registry"
1606 "`ovm_component_registry_param"
1607 "`ovm_component_utils"
1608 "`ovm_create"
1609 "`ovm_create_seq"
1610 "`ovm_declare_sequence_lib"
1611 "`ovm_do"
1612 "`ovm_do_seq"
1613 "`ovm_do_seq_with"
1614 "`ovm_do_with"
1615 "`ovm_error"
1616 "`ovm_fatal"
1617 "`ovm_field_aa_int_byte"
1618 "`ovm_field_aa_int_byte_unsigned"
1619 "`ovm_field_aa_int_int"
1620 "`ovm_field_aa_int_int_unsigned"
1621 "`ovm_field_aa_int_integer"
1622 "`ovm_field_aa_int_integer_unsigned"
1623 "`ovm_field_aa_int_key"
1624 "`ovm_field_aa_int_longint"
1625 "`ovm_field_aa_int_longint_unsigned"
1626 "`ovm_field_aa_int_shortint"
1627 "`ovm_field_aa_int_shortint_unsigned"
1628 "`ovm_field_aa_int_string"
1629 "`ovm_field_aa_object_int"
1630 "`ovm_field_aa_object_string"
1631 "`ovm_field_aa_string_int"
1632 "`ovm_field_aa_string_string"
1633 "`ovm_field_array_int"
1634 "`ovm_field_array_object"
1635 "`ovm_field_array_string"
1636 "`ovm_field_enum"
1637 "`ovm_field_event"
1638 "`ovm_field_int"
1639 "`ovm_field_object"
1640 "`ovm_field_queue_int"
1641 "`ovm_field_queue_object"
1642 "`ovm_field_queue_string"
1643 "`ovm_field_sarray_int"
1644 "`ovm_field_string"
1645 "`ovm_field_utils"
1646 "`ovm_file"
1647 "`ovm_get_imp_decl"
1648 "`ovm_get_peek_imp_decl"
1649 "`ovm_info"
1650 "`ovm_info1"
1651 "`ovm_info2"
1652 "`ovm_info3"
1653 "`ovm_info4"
1654 "`ovm_line"
1655 "`ovm_master_imp_decl"
1656 "`ovm_msg_detail"
1657 "`ovm_non_blocking_transport_imp_decl"
1658 "`ovm_nonblocking_get_imp_decl"
1659 "`ovm_nonblocking_get_peek_imp_decl"
1660 "`ovm_nonblocking_master_imp_decl"
1661 "`ovm_nonblocking_peek_imp_decl"
1662 "`ovm_nonblocking_put_imp_decl"
1663 "`ovm_nonblocking_slave_imp_decl"
1664 "`ovm_object_registry"
1665 "`ovm_object_registry_param"
1666 "`ovm_object_utils"
1667 "`ovm_peek_imp_decl"
1668 "`ovm_phase_func_decl"
1669 "`ovm_phase_task_decl"
1670 "`ovm_print_aa_int_object"
1671 "`ovm_print_aa_string_int"
1672 "`ovm_print_aa_string_object"
1673 "`ovm_print_aa_string_string"
1674 "`ovm_print_array_int"
1675 "`ovm_print_array_object"
1676 "`ovm_print_array_string"
1677 "`ovm_print_object_queue"
1678 "`ovm_print_queue_int"
1679 "`ovm_print_string_queue"
1680 "`ovm_put_imp_decl"
1681 "`ovm_rand_send"
1682 "`ovm_rand_send_with"
1683 "`ovm_send"
1684 "`ovm_sequence_utils"
1685 "`ovm_slave_imp_decl"
1686 "`ovm_transport_imp_decl"
1687 "`ovm_update_sequence_lib"
1688 "`ovm_update_sequence_lib_and_item"
1689 "`ovm_warning"
1690 "`static_dut_error"
1691 "`static_message") nil )))
1692
1693
1694 ;;
1695 ;; Regular expressions used to calculate indent, etc.
1696 ;;
1697 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
1698 ;; Want to match
1699 ;; aa :
1700 ;; aa,bb :
1701 ;; a[34:32] :
1702 ;; a,
1703 ;; b :
1704
1705 (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
1706 (defconst verilog-no-indent-begin-re
1707 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\|always_comb\\|always_ff\\|always_latch\\)\\>")
1708
1709 (defconst verilog-ends-re
1710 ;; Parenthesis indicate type of keyword found
1711 (concat
1712 "\\(\\<else\\>\\)\\|" ; 1
1713 "\\(\\<if\\>\\)\\|" ; 2
1714 "\\(\\<assert\\>\\)\\|" ; 3
1715 "\\(\\<end\\>\\)\\|" ; 3.1
1716 "\\(\\<endcase\\>\\)\\|" ; 4
1717 "\\(\\<endfunction\\>\\)\\|" ; 5
1718 "\\(\\<endtask\\>\\)\\|" ; 6
1719 "\\(\\<endspecify\\>\\)\\|" ; 7
1720 "\\(\\<endtable\\>\\)\\|" ; 8
1721 "\\(\\<endgenerate\\>\\)\\|" ; 9
1722 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
1723 "\\(\\<endclass\\>\\)\\|" ; 11
1724 "\\(\\<endgroup\\>\\)\\|" ; 12
1725 ;; VMM
1726 "\\(\\<`vmm_data_member_end\\>\\)\\|"
1727 "\\(\\<`vmm_env_member_end\\>\\)\\|"
1728 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
1729 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
1730 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
1731 ;; OVM
1732 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
1733 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
1734 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
1735 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
1736 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
1737
1738 ))
1739
1740 (defconst verilog-auto-end-comment-lines-re
1741 ;; Matches to names in this list cause auto-end-commentation
1742 (concat "\\("
1743 verilog-directive-re "\\)\\|\\("
1744 (eval-when-compile
1745 (verilog-regexp-words
1746 `( "begin"
1747 "else"
1748 "end"
1749 "endcase"
1750 "endclass"
1751 "endclocking"
1752 "endgroup"
1753 "endfunction"
1754 "endmodule"
1755 "endprogram"
1756 "endprimitive"
1757 "endinterface"
1758 "endpackage"
1759 "endsequence"
1760 "endspecify"
1761 "endtable"
1762 "endtask"
1763 "join"
1764 "join_any"
1765 "join_none"
1766 "module"
1767 "macromodule"
1768 "primitive"
1769 "interface"
1770 "package")))
1771 "\\)"))
1772
1773 ;;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
1774 ;;; verilog-end-block-ordered-re matches exactly the same strings.
1775 (defconst verilog-end-block-ordered-re
1776 ;; Parenthesis indicate type of keyword found
1777 (concat "\\(\\<endcase\\>\\)\\|" ; 1
1778 "\\(\\<end\\>\\)\\|" ; 2
1779 "\\(\\<end" ; 3, but not used
1780 "\\(" ; 4, but not used
1781 "\\(function\\)\\|" ; 5
1782 "\\(task\\)\\|" ; 6
1783 "\\(module\\)\\|" ; 7
1784 "\\(primitive\\)\\|" ; 8
1785 "\\(interface\\)\\|" ; 9
1786 "\\(package\\)\\|" ; 10
1787 "\\(class\\)\\|" ; 11
1788 "\\(group\\)\\|" ; 12
1789 "\\(program\\)\\|" ; 13
1790 "\\(sequence\\)\\|" ; 14
1791 "\\(clocking\\)\\|" ; 15
1792 "\\)\\>\\)"))
1793 (defconst verilog-end-block-re
1794 (eval-when-compile
1795 (verilog-regexp-words
1796
1797 `("end" ;; closes begin
1798 "endcase" ;; closes any of case, casex casez or randcase
1799 "join" "join_any" "join_none" ;; closes fork
1800 "endclass"
1801 "endtable"
1802 "endspecify"
1803 "endfunction"
1804 "endgenerate"
1805 "endtask"
1806 "endgroup"
1807 "endproperty"
1808 "endinterface"
1809 "endpackage"
1810 "endprogram"
1811 "endsequence"
1812 "endclocking"
1813 ;; OVM
1814 "`ovm_component_utils_end"
1815 "`ovm_field_utils_end"
1816 "`ovm_object_utils_end"
1817 "`ovm_sequence_utils_end"
1818 "`ovm_sequencer_utils_end"
1819 ;; VMM
1820 "`vmm_data_member_end"
1821 "`vmm_env_member_end"
1822 "`vmm_scenario_member_end"
1823 "`vmm_subenv_member_end"
1824 "`vmm_xactor_member_end"
1825 ))))
1826
1827
1828 (defconst verilog-endcomment-reason-re
1829 ;; Parenthesis indicate type of keyword found
1830 (concat
1831 "\\(\\<begin\\>\\)\\|" ; 1
1832 "\\(\\<else\\>\\)\\|" ; 2
1833 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
1834 "\\(\\<always_comb\\>\\(\[ \t\]*@\\)?\\)\\|" ; 4
1835 "\\(\\<always_ff\\>\\(\[ \t\]*@\\)?\\)\\|" ; 5
1836 "\\(\\<always_latch\\>\\(\[ \t\]*@\\)?\\)\\|" ; 6
1837 "\\(\\<fork\\>\\)\\|" ; 7
1838 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
1839 "\\(\\<if\\>\\)\\|"
1840 "\\(\\<clocking\\>\\)\\|"
1841 "\\(\\<task\\>\\)\\|"
1842 "\\(\\<function\\>\\)\\|"
1843 "\\(\\<initial\\>\\)\\|"
1844 "\\(\\<interface\\>\\)\\|"
1845 "\\(\\<package\\>\\)\\|"
1846 "\\(\\<final\\>\\)\\|"
1847 "\\(@\\)\\|"
1848 "\\(\\<while\\>\\)\\|"
1849 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
1850 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
1851 "#"))
1852
1853 (defconst verilog-named-block-re "begin[ \t]*:")
1854
1855 ;; These words begin a block which can occur inside a module which should be indented,
1856 ;; and closed with the respective word from the end-block list
1857
1858 (defconst verilog-beg-block-re
1859 (eval-when-compile
1860 (verilog-regexp-words
1861 `("begin"
1862 "case" "casex" "casez" "randcase"
1863 "clocking"
1864 "generate"
1865 "fork"
1866 "function"
1867 "property"
1868 "specify"
1869 "table"
1870 "task"
1871 ;;; OVM
1872 "`ovm_component_utils_begin"
1873 "`ovm_component_param_utils_begin"
1874 "`ovm_field_utils_begin"
1875 "`ovm_object_utils_begin"
1876 "`ovm_object_param_utils_begin"
1877 "`ovm_sequence_utils_begin"
1878 "`ovm_sequencer_utils_begin"
1879 ;; VMM
1880 "`vmm_data_member_begin"
1881 "`vmm_env_member_begin"
1882 "`vmm_scenario_member_begin"
1883 "`vmm_subenv_member_begin"
1884 "`vmm_xactor_member_begin"
1885 ))))
1886 ;; These are the same words, in a specific order in the regular
1887 ;; expression so that matching will work nicely for
1888 ;; verilog-forward-sexp and verilog-calc-indent
1889 (defconst verilog-beg-block-re-ordered
1890 ( concat "\\(\\<begin\\>\\)" ;1
1891 "\\|\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
1892 "\\|\\(\\(\\<disable\\>\\s-+\\)?fork\\>\\)" ;4,5
1893 "\\|\\(\\<class\\>\\)" ;6
1894 "\\|\\(\\<table\\>\\)" ;7
1895 "\\|\\(\\<specify\\>\\)" ;8
1896 "\\|\\(\\<function\\>\\)" ;9
1897 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
1898 "\\|\\(\\<task\\>\\)" ;14
1899 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
1900 "\\|\\(\\<generate\\>\\)" ;18
1901 "\\|\\(\\<covergroup\\>\\)" ;16 20
1902 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
1903 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
1904 "\\|\\(\\<clocking\\>\\)" ;22 27
1905 "\\|\\(\\<`ovm_[a-z_]+_begin\\>\\)" ;28
1906 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
1907 ;;
1908
1909 ))
1910
1911 (defconst verilog-end-block-ordered-rry
1912 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
1913 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
1914 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
1915 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
1916 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
1917 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
1918 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
1919 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
1920 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
1921 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
1922 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
1923 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
1924 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
1925 ] )
1926
1927 (defconst verilog-nameable-item-re
1928 (eval-when-compile
1929 (verilog-regexp-words
1930 `("begin"
1931 "fork"
1932 "join" "join_any" "join_none"
1933 "end"
1934 "endcase"
1935 "endconfig"
1936 "endclass"
1937 "endclocking"
1938 "endfunction"
1939 "endgenerate"
1940 "endmodule"
1941 "endprimitive"
1942 "endinterface"
1943 "endpackage"
1944 "endspecify"
1945 "endtable"
1946 "endtask" )
1947 )))
1948
1949 (defconst verilog-declaration-opener
1950 (eval-when-compile
1951 (verilog-regexp-words
1952 `("module" "begin" "task" "function"))))
1953
1954 (defconst verilog-declaration-prefix-re
1955 (eval-when-compile
1956 (verilog-regexp-words
1957 `(
1958 ;; port direction
1959 "inout" "input" "output" "ref"
1960 ;; changeableness
1961 "const" "static" "protected" "local"
1962 ;; parameters
1963 "localparam" "parameter" "var"
1964 ;; type creation
1965 "typedef"
1966 ))))
1967 (defconst verilog-declaration-core-re
1968 (eval-when-compile
1969 (verilog-regexp-words
1970 `(
1971 ;; port direction (by themselves)
1972 "inout" "input" "output"
1973 ;; integer_atom_type
1974 "byte" "shortint" "int" "longint" "integer" "time"
1975 ;; integer_vector_type
1976 "bit" "logic" "reg"
1977 ;; non_integer_type
1978 "shortreal" "real" "realtime"
1979 ;; net_type
1980 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
1981 ;; misc
1982 "string" "event" "chandle" "virtual" "enum" "genvar"
1983 "struct" "union"
1984 ;; builtin classes
1985 "mailbox" "semaphore"
1986 ))))
1987 (defconst verilog-declaration-re
1988 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
1989 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
1990 (defconst verilog-optional-signed-re "\\s-*\\(signed\\)?")
1991 (defconst verilog-optional-signed-range-re
1992 (concat
1993 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
1994 (defconst verilog-macroexp-re "`\\sw+")
1995
1996 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
1997 (defconst verilog-declaration-re-2-no-macro
1998 (concat "\\s-*" verilog-declaration-re
1999 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2000 "\\)?"))
2001 (defconst verilog-declaration-re-2-macro
2002 (concat "\\s-*" verilog-declaration-re
2003 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2004 "\\|\\(" verilog-macroexp-re "\\)"
2005 "\\)?"))
2006 (defconst verilog-declaration-re-1-macro
2007 (concat "^" verilog-declaration-re-2-macro))
2008
2009 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2010
2011 (defconst verilog-defun-re
2012 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2013 (defconst verilog-end-defun-re
2014 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2015 (defconst verilog-zero-indent-re
2016 (concat verilog-defun-re "\\|" verilog-end-defun-re))
2017
2018 (defconst verilog-behavioral-block-beg-re
2019 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2020 "function" "task"))))
2021 (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
2022 (defconst verilog-indent-re
2023 (eval-when-compile
2024 (verilog-regexp-words
2025 `(
2026 "{"
2027 "always" "always_latch" "always_ff" "always_comb"
2028 "begin" "end"
2029 ; "unique" "priority"
2030 "case" "casex" "casez" "randcase" "endcase"
2031 "class" "endclass"
2032 "clocking" "endclocking"
2033 "config" "endconfig"
2034 "covergroup" "endgroup"
2035 "fork" "join" "join_any" "join_none"
2036 "function" "endfunction"
2037 "final"
2038 "generate" "endgenerate"
2039 "initial"
2040 "interface" "endinterface"
2041 "module" "macromodule" "endmodule"
2042 "package" "endpackage"
2043 "primitive" "endprimative"
2044 "program" "endprogram"
2045 "property" "endproperty"
2046 "sequence" "randsequence" "endsequence"
2047 "specify" "endspecify"
2048 "table" "endtable"
2049 "task" "endtask"
2050 "virtual"
2051 "`case"
2052 "`default"
2053 "`define" "`undef"
2054 "`if" "`ifdef" "`ifndef" "`else" "`endif"
2055 "`while" "`endwhile"
2056 "`for" "`endfor"
2057 "`format"
2058 "`include"
2059 "`let"
2060 "`protect" "`endprotect"
2061 "`switch" "`endswitch"
2062 "`timescale"
2063 "`time_scale"
2064 ;; OVM Begin tokens
2065 "`ovm_component_utils_begin"
2066 "`ovm_component_param_utils_begin"
2067 "`ovm_field_utils_begin"
2068 "`ovm_object_utils_begin"
2069 "`ovm_object_param_utils_begin"
2070 "`ovm_sequence_utils_begin"
2071 "`ovm_sequencer_utils_begin"
2072 ;; OVM End tokens
2073 "`ovm_component_utils_end"
2074 "`ovm_field_utils_end"
2075 "`ovm_object_utils_end"
2076 "`ovm_sequence_utils_end"
2077 "`ovm_sequencer_utils_end"
2078 ;; VMM Begin tokens
2079 "`vmm_data_member_begin"
2080 "`vmm_env_member_begin"
2081 "`vmm_scenario_member_begin"
2082 "`vmm_subenv_member_begin"
2083 "`vmm_xactor_member_begin"
2084 ;; VMM End tokens
2085 "`vmm_data_member_end"
2086 "`vmm_env_member_end"
2087 "`vmm_scenario_member_end"
2088 "`vmm_subenv_member_end"
2089 "`vmm_xactor_member_end"
2090 ))))
2091
2092 (defconst verilog-defun-level-not-generate-re
2093 (eval-when-compile
2094 (verilog-regexp-words
2095 `( "module" "macromodule" "primitive" "class" "program"
2096 "interface" "package" "config"))))
2097
2098 (defconst verilog-defun-level-re
2099 (eval-when-compile
2100 (verilog-regexp-words
2101 (append
2102 `( "module" "macromodule" "primitive" "class" "program"
2103 "interface" "package" "config")
2104 `( "initial" "final" "always" "always_comb" "always_ff"
2105 "always_latch" "endtask" "endfunction" )))))
2106
2107 (defconst verilog-defun-level-generate-only-re
2108 (eval-when-compile
2109 (verilog-regexp-words
2110 `( "initial" "final" "always" "always_comb" "always_ff"
2111 "always_latch" "endtask" "endfunction" ))))
2112
2113 (defconst verilog-cpp-level-re
2114 (eval-when-compile
2115 (verilog-regexp-words
2116 `(
2117 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2118 ))))
2119 (defconst verilog-disable-fork-re "disable\\s-+fork\\>")
2120 (defconst verilog-fork-wait-re "fork\\s-+wait\\>")
2121 (defconst verilog-extended-case-re "\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?")
2122 (defconst verilog-extended-complete-re
2123 (concat "\\(\\<extern\\s-+\\|\\<virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)"
2124 "\\|\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)"
2125 "\\|\\(\\<import\\>\\s-+\\)?\"DPI-C\"\\s-+\\(function\\>\\|task\\>\\)"
2126 "\\|" verilog-extended-case-re ))
2127 (defconst verilog-basic-complete-re
2128 (eval-when-compile
2129 (verilog-regexp-words
2130 `(
2131 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2132 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
2133 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
2134 ))))
2135 (defconst verilog-complete-reg
2136 (concat
2137 verilog-extended-complete-re
2138 "\\|"
2139 verilog-basic-complete-re))
2140
2141 (defconst verilog-end-statement-re
2142 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2143 verilog-end-block-re "\\)"))
2144
2145 (defconst verilog-endcase-re
2146 (concat verilog-extended-case-re "\\|"
2147 "\\(endcase\\)\\|"
2148 verilog-defun-re
2149 ))
2150
2151 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2152 "String used to mark beginning of excluded text.")
2153 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2154 "String used to mark end of excluded text.")
2155 (defconst verilog-preprocessor-re
2156 (eval-when-compile
2157 (verilog-regexp-words
2158 `(
2159 "`define" "`include" "`ifdef" "`ifndef" "`if" "`endif" "`else"
2160 ))))
2161
2162 (defconst verilog-keywords
2163 '( "`case" "`default" "`define" "`else" "`endfor" "`endif"
2164 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
2165 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
2166 "`time_scale" "`undef" "`while"
2167
2168 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2169 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2170 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2171 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2172 "config" "const" "constraint" "context" "continue" "cover"
2173 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2174 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2175 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2176 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2177 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2178 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2179 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2180 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2181 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2182 "include" "initial" "inout" "input" "inside" "instance" "int"
2183 "integer" "interface" "intersect" "join" "join_any" "join_none"
2184 "large" "liblist" "library" "local" "localparam" "logic"
2185 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2186 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2187 "notif0" "notif1" "null" "or" "output" "package" "packed"
2188 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2189 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2190 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2191 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2192 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2193 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2194 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2195 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2196 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2197 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2198 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2199 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2200 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2201 "wire" "with" "within" "wor" "xnor" "xor"
2202 ;; 1800-2009
2203 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2204 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2205 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2206 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
2207 )
2208 "List of Verilog keywords.")
2209
2210 (defconst verilog-comment-start-regexp "//\\|/\\*"
2211 "Dual comment value for `comment-start-regexp'.")
2212
2213 (defvar verilog-mode-syntax-table
2214 (let ((table (make-syntax-table)))
2215 ;; Populate the syntax TABLE.
2216 (modify-syntax-entry ?\\ "\\" table)
2217 (modify-syntax-entry ?+ "." table)
2218 (modify-syntax-entry ?- "." table)
2219 (modify-syntax-entry ?= "." table)
2220 (modify-syntax-entry ?% "." table)
2221 (modify-syntax-entry ?< "." table)
2222 (modify-syntax-entry ?> "." table)
2223 (modify-syntax-entry ?& "." table)
2224 (modify-syntax-entry ?| "." table)
2225 (modify-syntax-entry ?` "w" table)
2226 (modify-syntax-entry ?_ "w" table)
2227 (modify-syntax-entry ?\' "." table)
2228
2229 ;; Set up TABLE to handle block and line style comments.
2230 (if (featurep 'xemacs)
2231 (progn
2232 ;; XEmacs (formerly Lucid) has the best implementation
2233 (modify-syntax-entry ?/ ". 1456" table)
2234 (modify-syntax-entry ?* ". 23" table)
2235 (modify-syntax-entry ?\n "> b" table))
2236 ;; Emacs does things differently, but we can work with it
2237 (modify-syntax-entry ?/ ". 124b" table)
2238 (modify-syntax-entry ?* ". 23" table)
2239 (modify-syntax-entry ?\n "> b" table))
2240 table)
2241 "Syntax table used in Verilog mode buffers.")
2242
2243 (defvar verilog-font-lock-keywords nil
2244 "Default highlighting for Verilog mode.")
2245
2246 (defvar verilog-font-lock-keywords-1 nil
2247 "Subdued level highlighting for Verilog mode.")
2248
2249 (defvar verilog-font-lock-keywords-2 nil
2250 "Medium level highlighting for Verilog mode.
2251 See also `verilog-font-lock-extra-types'.")
2252
2253 (defvar verilog-font-lock-keywords-3 nil
2254 "Gaudy level highlighting for Verilog mode.
2255 See also `verilog-font-lock-extra-types'.")
2256 (defvar verilog-font-lock-translate-off-face
2257 'verilog-font-lock-translate-off-face
2258 "Font to use for translated off regions.")
2259 (defface verilog-font-lock-translate-off-face
2260 '((((class color)
2261 (background light))
2262 (:background "gray90" :italic t ))
2263 (((class color)
2264 (background dark))
2265 (:background "gray10" :italic t ))
2266 (((class grayscale) (background light))
2267 (:foreground "DimGray" :italic t))
2268 (((class grayscale) (background dark))
2269 (:foreground "LightGray" :italic t))
2270 (t (:italis t)))
2271 "Font lock mode face used to background highlight translate-off regions."
2272 :group 'font-lock-highlighting-faces)
2273
2274 (defvar verilog-font-lock-p1800-face
2275 'verilog-font-lock-p1800-face
2276 "Font to use for p1800 keywords.")
2277 (defface verilog-font-lock-p1800-face
2278 '((((class color)
2279 (background light))
2280 (:foreground "DarkOrange3" :bold t ))
2281 (((class color)
2282 (background dark))
2283 (:foreground "orange1" :bold t ))
2284 (t (:italic t)))
2285 "Font lock mode face used to highlight P1800 keywords."
2286 :group 'font-lock-highlighting-faces)
2287
2288 (defvar verilog-font-lock-ams-face
2289 'verilog-font-lock-ams-face
2290 "Font to use for Analog/Mixed Signal keywords.")
2291 (defface verilog-font-lock-ams-face
2292 '((((class color)
2293 (background light))
2294 (:foreground "Purple" :bold t ))
2295 (((class color)
2296 (background dark))
2297 (:foreground "orange1" :bold t ))
2298 (t (:italic t)))
2299 "Font lock mode face used to highlight AMS keywords."
2300 :group 'font-lock-highlighting-faces)
2301
2302 (defvar verilog-font-grouping-keywords-face
2303 'verilog-font-lock-grouping-keywords-face
2304 "Font to use for Verilog Grouping Keywords (such as begin..end).")
2305 (defface verilog-font-lock-grouping-keywords-face
2306 '((((class color)
2307 (background light))
2308 (:foreground "red4" :bold t ))
2309 (((class color)
2310 (background dark))
2311 (:foreground "red4" :bold t ))
2312 (t (:italic t)))
2313 "Font lock mode face used to highlight verilog grouping keywords."
2314 :group 'font-lock-highlighting-faces)
2315
2316 (let* ((verilog-type-font-keywords
2317 (eval-when-compile
2318 (verilog-regexp-opt
2319 '(
2320 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
2321 "event" "genvar" "inout" "input" "integer" "localparam"
2322 "logic" "mailbox" "nand" "nmos" "not" "notif0" "notif1" "or"
2323 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
2324 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
2325 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
2326 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
2327 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
2328 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
2329 ) nil )))
2330
2331 (verilog-pragma-keywords
2332 (eval-when-compile
2333 (verilog-regexp-opt
2334 '("surefire" "synopsys" "rtl_synthesis" "verilint" "leda" "0in") nil
2335 )))
2336
2337 (verilog-1800-2005-keywords
2338 (eval-when-compile
2339 (verilog-regexp-opt
2340 '("alias" "assert" "assume" "automatic" "before" "bind"
2341 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
2342 "clocking" "config" "const" "constraint" "context" "continue"
2343 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
2344 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
2345 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
2346 "expect" "export" "extends" "extern" "first_match" "foreach"
2347 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
2348 "illegal_bins" "import" "incdir" "include" "inside" "instance"
2349 "int" "intersect" "large" "liblist" "library" "local" "longint"
2350 "matches" "medium" "modport" "new" "noshowcancelled" "null"
2351 "packed" "program" "property" "protected" "pull0" "pull1"
2352 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2353 "randcase" "randsequence" "ref" "release" "return" "scalared"
2354 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
2355 "specparam" "static" "string" "strong0" "strong1" "struct"
2356 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
2357 "type" "union" "unsigned" "use" "var" "virtual" "void"
2358 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
2359 ) nil )))
2360
2361 (verilog-1800-2009-keywords
2362 (eval-when-compile
2363 (verilog-regexp-opt
2364 '("accept_on" "checker" "endchecker" "eventually" "global"
2365 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
2366 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
2367 "sync_accept_on" "sync_reject_on" "unique0" "until"
2368 "until_with" "untyped" "weak" ) nil )))
2369
2370 (verilog-ams-keywords
2371 (eval-when-compile
2372 (verilog-regexp-opt
2373 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
2374 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
2375 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
2376 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
2377 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
2378 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
2379 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
2380 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
2381 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
2382 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
2383 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
2384
2385 (verilog-font-keywords
2386 (eval-when-compile
2387 (verilog-regexp-opt
2388 '(
2389 "assign" "case" "casex" "casez" "randcase" "deassign"
2390 "default" "disable" "else" "endcase" "endfunction"
2391 "endgenerate" "endinterface" "endmodule" "endprimitive"
2392 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
2393 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
2394 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
2395 "package" "endpackage" "always" "always_comb" "always_ff"
2396 "always_latch" "posedge" "primitive" "priority" "release"
2397 "repeat" "specify" "table" "task" "unique" "wait" "while"
2398 "class" "program" "endclass" "endprogram"
2399 ) nil )))
2400
2401 (verilog-font-grouping-keywords
2402 (eval-when-compile
2403 (verilog-regexp-opt
2404 '( "begin" "end" ) nil ))))
2405
2406 (setq verilog-font-lock-keywords
2407 (list
2408 ;; Fontify all builtin keywords
2409 (concat "\\<\\(" verilog-font-keywords "\\|"
2410 ;; And user/system tasks and functions
2411 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
2412 "\\)\\>")
2413 ;; Fontify all types
2414 (if verilog-highlight-grouping-keywords
2415 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2416 'verilog-font-lock-ams-face)
2417 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2418 'font-lock-type-face))
2419 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
2420 'font-lock-type-face)
2421 ;; Fontify IEEE-1800-2005 keywords appropriately
2422 (if verilog-highlight-p1800-keywords
2423 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2424 'verilog-font-lock-p1800-face)
2425 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2426 'font-lock-type-face))
2427 ;; Fontify IEEE-1800-2009 keywords appropriately
2428 (if verilog-highlight-p1800-keywords
2429 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2430 'verilog-font-lock-p1800-face)
2431 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2432 'font-lock-type-face))
2433 ;; Fontify Verilog-AMS keywords
2434 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
2435 'verilog-font-lock-ams-face)))
2436
2437 (setq verilog-font-lock-keywords-1
2438 (append verilog-font-lock-keywords
2439 (list
2440 ;; Fontify module definitions
2441 (list
2442 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
2443 '(1 font-lock-keyword-face)
2444 '(3 font-lock-function-name-face 'prepend))
2445 ;; Fontify function definitions
2446 (list
2447 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
2448 '(1 font-lock-keyword-face)
2449 '(3 font-lock-reference-face prepend))
2450 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
2451 (1 font-lock-keyword-face)
2452 (2 font-lock-reference-face append))
2453 '("\\<function\\>\\s-+\\(\\sw+\\)"
2454 1 'font-lock-reference-face append))))
2455
2456 (setq verilog-font-lock-keywords-2
2457 (append verilog-font-lock-keywords-1
2458 (list
2459 ;; Fontify pragmas
2460 (concat "\\(//\\s-*" verilog-pragma-keywords "\\s-.*\\)")
2461 ;; Fontify escaped names
2462 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
2463 ;; Fontify macro definitions/ uses
2464 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
2465 'font-lock-preprocessor-face
2466 'font-lock-type-face))
2467 ;; Fontify delays/numbers
2468 '("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9_.\]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
2469 0 font-lock-type-face append)
2470 ;; Fontify instantiation names
2471 '("\\([A-Za-z][A-Za-z0-9_]+\\)\\s-*(" 1 font-lock-function-name-face)
2472 )))
2473
2474 (setq verilog-font-lock-keywords-3
2475 (append verilog-font-lock-keywords-2
2476 (when verilog-highlight-translate-off
2477 (list
2478 ;; Fontify things in translate off regions
2479 '(verilog-match-translate-off
2480 (0 'verilog-font-lock-translate-off-face prepend))
2481 )))))
2482
2483
2484 (defun verilog-inside-comment-p ()
2485 "Check if point inside a nested comment."
2486 (save-excursion
2487 (let ((st-point (point)) hitbeg)
2488 (or (search-backward "//" (verilog-get-beg-of-line) t)
2489 (if (progn
2490 ;; This is for tricky case //*, we keep searching if /*
2491 ;; is proceeded by // on same line.
2492 (while
2493 (and (setq hitbeg (search-backward "/*" nil t))
2494 (progn
2495 (forward-char 1)
2496 (search-backward "//" (verilog-get-beg-of-line) t))))
2497 hitbeg)
2498 (not (search-forward "*/" st-point t)))))))
2499
2500 (defun verilog-declaration-end ()
2501 (search-forward ";"))
2502
2503 (defun verilog-point-text (&optional pointnum)
2504 "Return text describing where POINTNUM or current point is (for errors).
2505 Use filename, if current buffer being edited shorten to just buffer name."
2506 (concat (or (and (equal (window-buffer (selected-window)) (current-buffer))
2507 (buffer-name))
2508 buffer-file-name
2509 (buffer-name))
2510 ":" (int-to-string (count-lines (point-min) (or pointnum (point))))))
2511
2512 (defun electric-verilog-backward-sexp ()
2513 "Move backward over one balanced expression."
2514 (interactive)
2515 ;; before that see if we are in a comment
2516 (verilog-backward-sexp))
2517
2518 (defun electric-verilog-forward-sexp ()
2519 "Move forward over one balanced expression."
2520 (interactive)
2521 ;; before that see if we are in a comment
2522 (verilog-forward-sexp))
2523
2524 ;;;used by hs-minor-mode
2525 (defun verilog-forward-sexp-function (arg)
2526 (if (< arg 0)
2527 (verilog-backward-sexp)
2528 (verilog-forward-sexp)))
2529
2530
2531 (defun verilog-backward-sexp ()
2532 (let ((reg)
2533 (elsec 1)
2534 (found nil)
2535 (st (point)))
2536 (if (not (looking-at "\\<"))
2537 (forward-word -1))
2538 (cond
2539 ((verilog-skip-backward-comment-or-string))
2540 ((looking-at "\\<else\\>")
2541 (setq reg (concat
2542 verilog-end-block-re
2543 "\\|\\(\\<else\\>\\)"
2544 "\\|\\(\\<if\\>\\)"))
2545 (while (and (not found)
2546 (verilog-re-search-backward reg nil 'move))
2547 (cond
2548 ((match-end 1) ; matched verilog-end-block-re
2549 ; try to leap back to matching outward block by striding across
2550 ; indent level changing tokens then immediately
2551 ; previous line governs indentation.
2552 (verilog-leap-to-head))
2553 ((match-end 2) ; else, we're in deep
2554 (setq elsec (1+ elsec)))
2555 ((match-end 3) ; found it
2556 (setq elsec (1- elsec))
2557 (if (= 0 elsec)
2558 ;; Now previous line describes syntax
2559 (setq found 't))))))
2560 ((looking-at verilog-end-block-re)
2561 (verilog-leap-to-head))
2562 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
2563 (cond
2564 ((match-end 1)
2565 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
2566 ((match-end 2)
2567 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
2568 ((match-end 3)
2569 (verilog-re-search-backward "\\<class\\>" nil 'move))
2570 ((match-end 4)
2571 (verilog-re-search-backward "\\<program\\>" nil 'move))
2572 ((match-end 5)
2573 (verilog-re-search-backward "\\<interface\\>" nil 'move))
2574 ((match-end 6)
2575 (verilog-re-search-backward "\\<package\\>" nil 'move))
2576 (t
2577 (goto-char st)
2578 (backward-sexp 1))))
2579 (t
2580 (goto-char st)
2581 (backward-sexp)))))
2582
2583 (defun verilog-forward-sexp ()
2584 (let ((reg)
2585 (md 2)
2586 (st (point))
2587 (nest 'yes))
2588 (if (not (looking-at "\\<"))
2589 (forward-word -1))
2590 (cond
2591 ((verilog-skip-forward-comment-or-string)
2592 (verilog-forward-syntactic-ws))
2593 ((looking-at verilog-beg-block-re-ordered)
2594 (cond
2595 ((match-end 1);
2596 ;; Search forward for matching end
2597 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
2598 ((match-end 2)
2599 ;; Search forward for matching endcase
2600 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
2601 (setq md 3) ;; ender is third item in regexp
2602 )
2603 ((match-end 4)
2604 ;; might be "disable fork" or "fork wait"
2605 (let
2606 (here)
2607 (if (looking-at verilog-fork-wait-re)
2608 (progn ;; it is a fork wait; ignore it
2609 (goto-char (match-end 0))
2610 (setq reg nil))
2611 (if (or
2612 (looking-at verilog-disable-fork-re)
2613 (and (looking-at "fork")
2614 (progn
2615 (setq here (point)) ;; sometimes a fork is just a fork
2616 (forward-word -1)
2617 (looking-at verilog-disable-fork-re))))
2618 (progn ;; it is a disable fork; ignore it
2619 (goto-char (match-end 0))
2620 (forward-word 1)
2621 (setq reg nil))
2622 (progn ;; it is a nice simple fork
2623 (goto-char here) ;; return from looking for "disable fork"
2624 ;; Search forward for matching join
2625 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))))))
2626 ((match-end 6)
2627 ;; Search forward for matching endclass
2628 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
2629
2630 ((match-end 7)
2631 ;; Search forward for matching endtable
2632 (setq reg "\\<endtable\\>" )
2633 (setq nest 'no))
2634 ((match-end 8)
2635 ;; Search forward for matching endspecify
2636 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
2637 ((match-end 9)
2638 ;; Search forward for matching endfunction
2639 (setq reg "\\<endfunction\\>" )
2640 (setq nest 'no))
2641 ((match-end 10)
2642 ;; Search forward for matching endfunction
2643 (setq reg "\\<endfunction\\>" )
2644 (setq nest 'no))
2645 ((match-end 14)
2646 ;; Search forward for matching endtask
2647 (setq reg "\\<endtask\\>" )
2648 (setq nest 'no))
2649 ((match-end 15)
2650 ;; Search forward for matching endtask
2651 (setq reg "\\<endtask\\>" )
2652 (setq nest 'no))
2653 ((match-end 19)
2654 ;; Search forward for matching endgenerate
2655 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
2656 ((match-end 20)
2657 ;; Search forward for matching endgroup
2658 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
2659 ((match-end 21)
2660 ;; Search forward for matching endproperty
2661 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
2662 ((match-end 25)
2663 ;; Search forward for matching endsequence
2664 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
2665 (setq md 3)) ; 3 to get to endsequence in the reg above
2666 ((match-end 27)
2667 ;; Search forward for matching endclocking
2668 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
2669 (if (and reg
2670 (forward-word 1))
2671 (catch 'skip
2672 (if (eq nest 'yes)
2673 (let ((depth 1)
2674 here)
2675 (while (verilog-re-search-forward reg nil 'move)
2676 (cond
2677 ((match-end md) ; a closer in regular expression, so we are climbing out
2678 (setq depth (1- depth))
2679 (if (= 0 depth) ; we are out!
2680 (throw 'skip 1)))
2681 ((match-end 1) ; an opener in the r-e, so we are in deeper now
2682 (setq here (point)) ; remember where we started
2683 (goto-char (match-beginning 1))
2684 (cond
2685 ((looking-at verilog-fork-wait-re)
2686 (goto-char (match-end 0))) ; false alarm
2687 ((if (or
2688 (looking-at verilog-disable-fork-re)
2689 (and (looking-at "fork")
2690 (progn
2691 (forward-word -1)
2692 (looking-at verilog-disable-fork-re))))
2693 (progn ;; it is a disable fork; another false alarm
2694 (goto-char (match-end 0)))
2695 (progn ;; it is a simple fork (or has nothing to do with fork)
2696 (goto-char here)
2697 (setq depth (1+ depth))))))))))
2698 (if (verilog-re-search-forward reg nil 'move)
2699 (throw 'skip 1))))))
2700
2701 ((looking-at (concat
2702 "\\(\\<\\(macro\\)?module\\>\\)\\|"
2703 "\\(\\<primitive\\>\\)\\|"
2704 "\\(\\<class\\>\\)\\|"
2705 "\\(\\<program\\>\\)\\|"
2706 "\\(\\<interface\\>\\)\\|"
2707 "\\(\\<package\\>\\)"))
2708 (cond
2709 ((match-end 1)
2710 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
2711 ((match-end 2)
2712 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
2713 ((match-end 3)
2714 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
2715 ((match-end 4)
2716 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
2717 ((match-end 5)
2718 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
2719 ((match-end 6)
2720 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
2721 (t
2722 (goto-char st)
2723 (if (= (following-char) ?\) )
2724 (forward-char 1)
2725 (forward-sexp 1)))))
2726 (t
2727 (goto-char st)
2728 (if (= (following-char) ?\) )
2729 (forward-char 1)
2730 (forward-sexp 1))))))
2731
2732 (defun verilog-declaration-beg ()
2733 (verilog-re-search-backward verilog-declaration-re (bobp) t))
2734
2735 ;;
2736 ;;
2737 ;; Mode
2738 ;;
2739 (defvar verilog-which-tool 1)
2740 ;;;###autoload
2741 (defun verilog-mode ()
2742 "Major mode for editing Verilog code.
2743 \\<verilog-mode-map>
2744 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
2745 AUTOs can improve coding efficiency.
2746
2747 Use \\[verilog-faq] for a pointer to frequently asked questions.
2748
2749 NEWLINE, TAB indents for Verilog code.
2750 Delete converts tabs to spaces as it moves back.
2751
2752 Supports highlighting.
2753
2754 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
2755 with no args, if that value is non-nil.
2756
2757 Variables controlling indentation/edit style:
2758
2759 variable `verilog-indent-level' (default 3)
2760 Indentation of Verilog statements with respect to containing block.
2761 `verilog-indent-level-module' (default 3)
2762 Absolute indentation of Module level Verilog statements.
2763 Set to 0 to get initial and always statements lined up
2764 on the left side of your screen.
2765 `verilog-indent-level-declaration' (default 3)
2766 Indentation of declarations with respect to containing block.
2767 Set to 0 to get them list right under containing block.
2768 `verilog-indent-level-behavioral' (default 3)
2769 Indentation of first begin in a task or function block
2770 Set to 0 to get such code to lined up underneath the task or
2771 function keyword.
2772 `verilog-indent-level-directive' (default 1)
2773 Indentation of `ifdef/`endif blocks.
2774 `verilog-cexp-indent' (default 1)
2775 Indentation of Verilog statements broken across lines i.e.:
2776 if (a)
2777 begin
2778 `verilog-case-indent' (default 2)
2779 Indentation for case statements.
2780 `verilog-auto-newline' (default nil)
2781 Non-nil means automatically newline after semicolons and the punctuation
2782 mark after an end.
2783 `verilog-auto-indent-on-newline' (default t)
2784 Non-nil means automatically indent line after newline.
2785 `verilog-tab-always-indent' (default t)
2786 Non-nil means TAB in Verilog mode should always reindent the current line,
2787 regardless of where in the line point is when the TAB command is used.
2788 `verilog-indent-begin-after-if' (default t)
2789 Non-nil means to indent begin statements following a preceding
2790 if, else, while, for and repeat statements, if any. Otherwise,
2791 the begin is lined up with the preceding token. If t, you get:
2792 if (a)
2793 begin // amount of indent based on `verilog-cexp-indent'
2794 otherwise you get:
2795 if (a)
2796 begin
2797 `verilog-auto-endcomments' (default t)
2798 Non-nil means a comment /* ... */ is set after the ends which ends
2799 cases, tasks, functions and modules.
2800 The type and name of the object will be set between the braces.
2801 `verilog-minimum-comment-distance' (default 10)
2802 Minimum distance (in lines) between begin and end required before a comment
2803 will be inserted. Setting this variable to zero results in every
2804 end acquiring a comment; the default avoids too many redundant
2805 comments in tight quarters.
2806 `verilog-auto-lineup' (default 'declarations)
2807 List of contexts where auto lineup of code should be done.
2808
2809 Variables controlling other actions:
2810
2811 `verilog-linter' (default surelint)
2812 Unix program to call to run the lint checker. This is the default
2813 command for \\[compile-command] and \\[verilog-auto-save-compile].
2814
2815 See \\[customize] for the complete list of variables.
2816
2817 AUTO expansion functions are, in part:
2818
2819 \\[verilog-auto] Expand AUTO statements.
2820 \\[verilog-delete-auto] Remove the AUTOs.
2821 \\[verilog-inject-auto] Insert AUTOs for the first time.
2822
2823 Some other functions are:
2824
2825 \\[verilog-complete-word] Complete word with appropriate possibilities.
2826 \\[verilog-mark-defun] Mark function.
2827 \\[verilog-beg-of-defun] Move to beginning of current function.
2828 \\[verilog-end-of-defun] Move to end of current function.
2829 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
2830
2831 \\[verilog-comment-region] Put marked area in a comment.
2832 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
2833 \\[verilog-insert-block] Insert begin ... end.
2834 \\[verilog-star-comment] Insert /* ... */.
2835
2836 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
2837 \\[verilog-sk-begin] Insert a begin .. end block.
2838 \\[verilog-sk-case] Insert a case block, prompting for details.
2839 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
2840 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
2841 \\[verilog-sk-header] Insert a header block at the top of file.
2842 \\[verilog-sk-initial] Insert an initial begin .. end block.
2843 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
2844 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
2845 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
2846 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
2847 \\[verilog-sk-specify] Insert a specify .. endspecify block.
2848 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
2849 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
2850 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
2851 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
2852 \\[verilog-sk-if] Insert an if (..) begin .. end block.
2853 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
2854 \\[verilog-sk-comment] Insert a comment block.
2855 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
2856 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
2857 \\[verilog-sk-input] Insert an input declaration, prompting for details.
2858 \\[verilog-sk-output] Insert an output declaration, prompting for details.
2859 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
2860 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
2861 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
2862 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
2863 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
2864
2865 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
2866 Key bindings specific to `verilog-mode-map' are:
2867
2868 \\{verilog-mode-map}"
2869 (interactive)
2870 (kill-all-local-variables)
2871 (use-local-map verilog-mode-map)
2872 (setq major-mode 'verilog-mode)
2873 (setq mode-name "Verilog")
2874 (setq local-abbrev-table verilog-mode-abbrev-table)
2875 (set (make-local-variable 'beginning-of-defun-function)
2876 'verilog-beg-of-defun)
2877 (set (make-local-variable 'end-of-defun-function)
2878 'verilog-end-of-defun)
2879 (set-syntax-table verilog-mode-syntax-table)
2880 (make-local-variable 'indent-line-function)
2881 (setq indent-line-function 'verilog-indent-line-relative)
2882 (setq comment-indent-function 'verilog-comment-indent)
2883 (make-local-variable 'parse-sexp-ignore-comments)
2884 (setq parse-sexp-ignore-comments nil)
2885 (make-local-variable 'comment-start)
2886 (make-local-variable 'comment-end)
2887 (make-local-variable 'comment-multi-line)
2888 (make-local-variable 'comment-start-skip)
2889 (setq comment-start "// "
2890 comment-end ""
2891 comment-start-skip "/\\*+ *\\|// *"
2892 comment-multi-line nil)
2893 ;; Set up for compilation
2894 (setq verilog-which-tool 1)
2895 (setq verilog-tool 'verilog-linter)
2896 (verilog-set-compile-command)
2897 (when (boundp 'hack-local-variables-hook) ;; Also modify any file-local-variables
2898 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
2899
2900 ;; Setting up menus
2901 (when (featurep 'xemacs)
2902 (easy-menu-add verilog-stmt-menu)
2903 (easy-menu-add verilog-menu)
2904 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
2905
2906 ;; Stuff for GNU Emacs
2907 (set (make-local-variable 'font-lock-defaults)
2908 `((verilog-font-lock-keywords verilog-font-lock-keywords-1
2909 verilog-font-lock-keywords-2
2910 verilog-font-lock-keywords-3)
2911 nil nil nil
2912 ,(if (functionp 'syntax-ppss)
2913 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
2914 ;; font-lock-beginning-of-syntax-function, so
2915 ;; font-lock-beginning-of-syntax-function, can't use
2916 ;; verilog-beg-of-defun.
2917 nil
2918 'verilog-beg-of-defun)))
2919 ;;------------------------------------------------------------
2920 ;; now hook in 'verilog-colorize-include-files (eldo-mode.el&spice-mode.el)
2921 ;; all buffer local:
2922 (when (featurep 'xemacs)
2923 (make-local-hook 'font-lock-mode-hook)
2924 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
2925 (make-local-hook 'after-change-functions))
2926 (add-hook 'font-lock-mode-hook 'verilog-colorize-include-files-buffer t t)
2927 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-colorize-include-files-buffer t t) ; not in Emacs
2928 (add-hook 'after-change-functions 'verilog-colorize-include-files t t)
2929
2930 ;; Tell imenu how to handle Verilog.
2931 (make-local-variable 'imenu-generic-expression)
2932 (setq imenu-generic-expression verilog-imenu-generic-expression)
2933 ;; Tell which-func-modes that imenu knows about verilog
2934 (when (boundp 'which-function-modes)
2935 (add-to-list 'which-func-modes 'verilog-mode))
2936 ;; hideshow support
2937 (when (boundp 'hs-special-modes-alist)
2938 (unless (assq 'verilog-mode hs-special-modes-alist)
2939 (setq hs-special-modes-alist
2940 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
2941 verilog-forward-sexp-function)
2942 hs-special-modes-alist))))
2943
2944 ;; Stuff for autos
2945 (add-hook 'write-contents-hooks 'verilog-auto-save-check) ; already local
2946 (run-hooks 'verilog-mode-hook))
2947 \f
2948
2949 ;;
2950 ;; Electric functions
2951 ;;
2952 (defun electric-verilog-terminate-line (&optional arg)
2953 "Terminate line and indent next line.
2954 With optional ARG, remove existing end of line comments."
2955 (interactive)
2956 ;; before that see if we are in a comment
2957 (let ((state (save-excursion (verilog-syntax-ppss))))
2958 (cond
2959 ((nth 7 state) ; Inside // comment
2960 (if (eolp)
2961 (progn
2962 (delete-horizontal-space)
2963 (newline))
2964 (progn
2965 (newline)
2966 (insert "// ")
2967 (beginning-of-line)))
2968 (verilog-indent-line))
2969 ((nth 4 state) ; Inside any comment (hence /**/)
2970 (newline)
2971 (verilog-more-comment))
2972 ((eolp)
2973 ;; First, check if current line should be indented
2974 (if (save-excursion
2975 (delete-horizontal-space)
2976 (beginning-of-line)
2977 (skip-chars-forward " \t")
2978 (if (looking-at verilog-auto-end-comment-lines-re)
2979 (let ((indent-str (verilog-indent-line)))
2980 ;; Maybe we should set some endcomments
2981 (if verilog-auto-endcomments
2982 (verilog-set-auto-endcomments indent-str arg))
2983 (end-of-line)
2984 (delete-horizontal-space)
2985 (if arg
2986 ()
2987 (newline))
2988 nil)
2989 (progn
2990 (end-of-line)
2991 (delete-horizontal-space)
2992 't)))
2993 ;; see if we should line up assignments
2994 (progn
2995 (if (or (eq 'all verilog-auto-lineup)
2996 (eq 'assignments verilog-auto-lineup))
2997 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
2998 (newline))
2999 (forward-line 1))
3000 ;; Indent next line
3001 (if verilog-auto-indent-on-newline
3002 (verilog-indent-line)))
3003 (t
3004 (newline)))))
3005
3006 (defun electric-verilog-terminate-and-indent ()
3007 "Insert a newline and indent for the next statement."
3008 (interactive)
3009 (electric-verilog-terminate-line 1))
3010
3011 (defun electric-verilog-semi ()
3012 "Insert `;' character and reindent the line."
3013 (interactive)
3014 (verilog-insert-last-command-event)
3015
3016 (if (or (verilog-in-comment-or-string-p)
3017 (verilog-in-escaped-name-p))
3018 ()
3019 (save-excursion
3020 (beginning-of-line)
3021 (verilog-forward-ws&directives)
3022 (verilog-indent-line))
3023 (if (and verilog-auto-newline
3024 (not (verilog-parenthesis-depth)))
3025 (electric-verilog-terminate-line))))
3026
3027 (defun electric-verilog-semi-with-comment ()
3028 "Insert `;' character, reindent the line and indent for comment."
3029 (interactive)
3030 (insert "\;")
3031 (save-excursion
3032 (beginning-of-line)
3033 (verilog-indent-line))
3034 (indent-for-comment))
3035
3036 (defun electric-verilog-colon ()
3037 "Insert `:' and do all indentations except line indent on this line."
3038 (interactive)
3039 (verilog-insert-last-command-event)
3040 ;; Do nothing if within string.
3041 (if (or
3042 (verilog-within-string)
3043 (not (verilog-in-case-region-p)))
3044 ()
3045 (save-excursion
3046 (let ((p (point))
3047 (lim (progn (verilog-beg-of-statement) (point))))
3048 (goto-char p)
3049 (verilog-backward-case-item lim)
3050 (verilog-indent-line)))
3051 ;; (let ((verilog-tab-always-indent nil))
3052 ;; (verilog-indent-line))
3053 ))
3054
3055 ;;(defun electric-verilog-equal ()
3056 ;; "Insert `=', and do indentation if within block."
3057 ;; (interactive)
3058 ;; (verilog-insert-last-command-event)
3059 ;; Could auto line up expressions, but not yet
3060 ;; (if (eq (car (verilog-calculate-indent)) 'block)
3061 ;; (let ((verilog-tab-always-indent nil))
3062 ;; (verilog-indent-command)))
3063 ;; )
3064
3065 (defun electric-verilog-tick ()
3066 "Insert back-tick, and indent to column 0 if this is a CPP directive."
3067 (interactive)
3068 (verilog-insert-last-command-event)
3069 (save-excursion
3070 (if (verilog-in-directive-p)
3071 (verilog-indent-line))))
3072
3073 (defun electric-verilog-tab ()
3074 "Function called when TAB is pressed in Verilog mode."
3075 (interactive)
3076 ;; If verilog-tab-always-indent, indent the beginning of the line.
3077 (cond
3078 ;; The region is active, indent it.
3079 ((and (region-active-p)
3080 (not (eq (region-beginning) (region-end))))
3081 (indent-region (region-beginning) (region-end) nil))
3082 ((or verilog-tab-always-indent
3083 (save-excursion
3084 (skip-chars-backward " \t")
3085 (bolp)))
3086 (let* ((oldpnt (point))
3087 (boi-point
3088 (save-excursion
3089 (beginning-of-line)
3090 (skip-chars-forward " \t")
3091 (verilog-indent-line)
3092 (back-to-indentation)
3093 (point))))
3094 (if (< (point) boi-point)
3095 (back-to-indentation)
3096 (cond ((not verilog-tab-to-comment))
3097 ((not (eolp))
3098 (end-of-line))
3099 (t
3100 (indent-for-comment)
3101 (when (and (eolp) (= oldpnt (point)))
3102 ; kill existing comment
3103 (beginning-of-line)
3104 (re-search-forward comment-start-skip oldpnt 'move)
3105 (goto-char (match-beginning 0))
3106 (skip-chars-backward " \t")
3107 (kill-region (point) oldpnt)))))))
3108 (t (progn (insert "\t")))))
3109
3110 \f
3111
3112 ;;
3113 ;; Interactive functions
3114 ;;
3115
3116 (defun verilog-indent-buffer ()
3117 "Indent-region the entire buffer as Verilog code.
3118 To call this from the command line, see \\[verilog-batch-indent]."
3119 (interactive)
3120 (verilog-mode)
3121 (indent-region (point-min) (point-max) nil))
3122
3123 (defun verilog-insert-block ()
3124 "Insert Verilog begin ... end; block in the code with right indentation."
3125 (interactive)
3126 (verilog-indent-line)
3127 (insert "begin")
3128 (electric-verilog-terminate-line)
3129 (save-excursion
3130 (electric-verilog-terminate-line)
3131 (insert "end")
3132 (beginning-of-line)
3133 (verilog-indent-line)))
3134
3135 (defun verilog-star-comment ()
3136 "Insert Verilog star comment at point."
3137 (interactive)
3138 (verilog-indent-line)
3139 (insert "/*")
3140 (save-excursion
3141 (newline)
3142 (insert " */"))
3143 (newline)
3144 (insert " * "))
3145
3146 (defun verilog-insert-1 (fmt max)
3147 "Use format string FMT to insert integers 0 to MAX - 1.
3148 Inserts one integer per line, at the current column. Stops early
3149 if it reaches the end of the buffer."
3150 (let ((col (current-column))
3151 (n 0))
3152 (save-excursion
3153 (while (< n max)
3154 (insert (format fmt n))
3155 (forward-line 1)
3156 ;; Note that this function does not bother to check for lines
3157 ;; shorter than col.
3158 (if (eobp)
3159 (setq n max)
3160 (setq n (1+ n))
3161 (move-to-column col))))))
3162
3163 (defun verilog-insert-indices (max)
3164 "Insert a set of indices into a rectangle.
3165 The upper left corner is defined by point. Indices begin with 0
3166 and extend to the MAX - 1. If no prefix arg is given, the user
3167 is prompted for a value. The indices are surrounded by square
3168 brackets \[]. For example, the following code with the point
3169 located after the first 'a' gives:
3170
3171 a = b a[ 0] = b
3172 a = b a[ 1] = b
3173 a = b a[ 2] = b
3174 a = b a[ 3] = b
3175 a = b ==> insert-indices ==> a[ 4] = b
3176 a = b a[ 5] = b
3177 a = b a[ 6] = b
3178 a = b a[ 7] = b
3179 a = b a[ 8] = b"
3180
3181 (interactive "NMAX: ")
3182 (verilog-insert-1 "[%3d]" max))
3183
3184 (defun verilog-generate-numbers (max)
3185 "Insert a set of generated numbers into a rectangle.
3186 The upper left corner is defined by point. The numbers are padded to three
3187 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
3188 is supplied, then the user is prompted for the MAX number. Consider the
3189 following code fragment:
3190
3191 buf buf buf buf000
3192 buf buf buf buf001
3193 buf buf buf buf002
3194 buf buf buf buf003
3195 buf buf ==> generate-numbers ==> buf buf004
3196 buf buf buf buf005
3197 buf buf buf buf006
3198 buf buf buf buf007
3199 buf buf buf buf008"
3200
3201 (interactive "NMAX: ")
3202 (verilog-insert-1 "%3.3d" max))
3203
3204 (defun verilog-mark-defun ()
3205 "Mark the current Verilog function (or procedure).
3206 This puts the mark at the end, and point at the beginning."
3207 (interactive)
3208 (if (featurep 'xemacs)
3209 (progn
3210 (push-mark (point))
3211 (verilog-end-of-defun)
3212 (push-mark (point))
3213 (verilog-beg-of-defun)
3214 (if (fboundp 'zmacs-activate-region)
3215 (zmacs-activate-region)))
3216 (mark-defun)))
3217
3218 (defun verilog-comment-region (start end)
3219 ; checkdoc-params: (start end)
3220 "Put the region into a Verilog comment.
3221 The comments that are in this area are \"deformed\":
3222 `*)' becomes `!(*' and `}' becomes `!{'.
3223 These deformed comments are returned to normal if you use
3224 \\[verilog-uncomment-region] to undo the commenting.
3225
3226 The commented area starts with `verilog-exclude-str-start', and ends with
3227 `verilog-exclude-str-end'. But if you change these variables,
3228 \\[verilog-uncomment-region] won't recognize the comments."
3229 (interactive "r")
3230 (save-excursion
3231 ;; Insert start and endcomments
3232 (goto-char end)
3233 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
3234 (not (save-excursion (skip-chars-backward " \t") (bolp))))
3235 (forward-line 1)
3236 (beginning-of-line))
3237 (insert verilog-exclude-str-end)
3238 (setq end (point))
3239 (newline)
3240 (goto-char start)
3241 (beginning-of-line)
3242 (insert verilog-exclude-str-start)
3243 (newline)
3244 ;; Replace end-comments within commented area
3245 (goto-char end)
3246 (save-excursion
3247 (while (re-search-backward "\\*/" start t)
3248 (replace-match "*-/" t t)))
3249 (save-excursion
3250 (let ((s+1 (1+ start)))
3251 (while (re-search-backward "/\\*" s+1 t)
3252 (replace-match "/-*" t t))))))
3253
3254 (defun verilog-uncomment-region ()
3255 "Uncomment a commented area; change deformed comments back to normal.
3256 This command does nothing if the pointer is not in a commented
3257 area. See also `verilog-comment-region'."
3258 (interactive)
3259 (save-excursion
3260 (let ((start (point))
3261 (end (point)))
3262 ;; Find the boundaries of the comment
3263 (save-excursion
3264 (setq start (progn (search-backward verilog-exclude-str-start nil t)
3265 (point)))
3266 (setq end (progn (search-forward verilog-exclude-str-end nil t)
3267 (point))))
3268 ;; Check if we're really inside a comment
3269 (if (or (equal start (point)) (<= end (point)))
3270 (message "Not standing within commented area.")
3271 (progn
3272 ;; Remove endcomment
3273 (goto-char end)
3274 (beginning-of-line)
3275 (let ((pos (point)))
3276 (end-of-line)
3277 (delete-region pos (1+ (point))))
3278 ;; Change comments back to normal
3279 (save-excursion
3280 (while (re-search-backward "\\*-/" start t)
3281 (replace-match "*/" t t)))
3282 (save-excursion
3283 (while (re-search-backward "/-\\*" start t)
3284 (replace-match "/*" t t)))
3285 ;; Remove start comment
3286 (goto-char start)
3287 (beginning-of-line)
3288 (let ((pos (point)))
3289 (end-of-line)
3290 (delete-region pos (1+ (point)))))))))
3291
3292 (defun verilog-beg-of-defun ()
3293 "Move backward to the beginning of the current function or procedure."
3294 (interactive)
3295 (verilog-re-search-backward verilog-defun-re nil 'move))
3296
3297 (defun verilog-end-of-defun ()
3298 "Move forward to the end of the current function or procedure."
3299 (interactive)
3300 (verilog-re-search-forward verilog-end-defun-re nil 'move))
3301
3302 (defun verilog-get-beg-of-defun (&optional warn)
3303 (save-excursion
3304 (cond ((verilog-re-search-forward-quick verilog-defun-re nil t)
3305 (point))
3306 (t
3307 (error "%s: Can't find module beginning" (verilog-point-text))
3308 (point-max)))))
3309 (defun verilog-get-end-of-defun (&optional warn)
3310 (save-excursion
3311 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
3312 (point))
3313 (t
3314 (error "%s: Can't find endmodule" (verilog-point-text))
3315 (point-max)))))
3316
3317 (defun verilog-label-be (&optional arg)
3318 "Label matching begin ... end, fork ... join and case ... endcase statements.
3319 With ARG, first kill any existing labels."
3320 (interactive)
3321 (let ((cnt 0)
3322 (oldpos (point))
3323 (b (progn
3324 (verilog-beg-of-defun)
3325 (point-marker)))
3326 (e (progn
3327 (verilog-end-of-defun)
3328 (point-marker))))
3329 (goto-char (marker-position b))
3330 (if (> (- e b) 200)
3331 (message "Relabeling module..."))
3332 (while (and
3333 (> (marker-position e) (point))
3334 (verilog-re-search-forward
3335 (concat
3336 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|\\(primitive\\)\\|\\(interface\\)\\|\\(package\\)\\|\\(case\\)\\)?\\>"
3337 "\\|\\(`endif\\)\\|\\(`else\\)")
3338 nil 'move))
3339 (goto-char (match-beginning 0))
3340 (let ((indent-str (verilog-indent-line)))
3341 (verilog-set-auto-endcomments indent-str 't)
3342 (end-of-line)
3343 (delete-horizontal-space))
3344 (setq cnt (1+ cnt))
3345 (if (= 9 (% cnt 10))
3346 (message "%d..." cnt)))
3347 (goto-char oldpos)
3348 (if (or
3349 (> (- e b) 200)
3350 (> cnt 20))
3351 (message "%d lines auto commented" cnt))))
3352
3353 (defun verilog-beg-of-statement ()
3354 "Move backward to beginning of statement."
3355 (interactive)
3356 ;; Move back token by token until we see the end
3357 ;; of some ealier line.
3358 (while
3359 ;; If the current point does not begin a new
3360 ;; statement, as in the character ahead of us is a ';', or SOF
3361 ;; or the string after us unambiguously starts a statement,
3362 ;; or the token before us unambiguously ends a statement,
3363 ;; then move back a token and test again.
3364 (not (or
3365 (bolp)
3366 (= (preceding-char) ?\;)
3367 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
3368 (not (or
3369 (looking-at "\\<")
3370 (forward-word -1)))
3371 (and
3372 (looking-at verilog-complete-reg)
3373 (not (save-excursion
3374 (verilog-backward-token)
3375 (looking-at verilog-extended-complete-re))))
3376 (looking-at verilog-basic-complete-re)
3377 (save-excursion
3378 (verilog-backward-token)
3379 (or
3380 (looking-at verilog-end-block-re)
3381 (looking-at verilog-preprocessor-re)))))
3382 (verilog-backward-syntactic-ws)
3383 (verilog-backward-token))
3384 ;; Now point is where the previous line ended.
3385 (verilog-forward-syntactic-ws))
3386
3387 (defun verilog-beg-of-statement-1 ()
3388 "Move backward to beginning of statement."
3389 (interactive)
3390 (if (verilog-in-comment-p)
3391 (verilog-backward-syntactic-ws))
3392 (let ((pt (point)))
3393 (catch 'done
3394 (while (not (looking-at verilog-complete-reg))
3395 (setq pt (point))
3396 (verilog-backward-syntactic-ws)
3397 (if (or (bolp)
3398 (= (preceding-char) ?\;))
3399 (progn
3400 (goto-char pt)
3401 (throw 'done t))
3402 (verilog-backward-token))))
3403 (verilog-forward-syntactic-ws)))
3404 ;
3405 ; (while (and
3406 ; (not (looking-at verilog-complete-reg))
3407 ; (not (bolp))
3408 ; (not (= (preceding-char) ?\;)))
3409 ; (verilog-backward-token)
3410 ; (verilog-backward-syntactic-ws)
3411 ; (setq pt (point)))
3412 ; (goto-char pt)
3413 ; ;(verilog-forward-syntactic-ws)
3414
3415 (defun verilog-end-of-statement ()
3416 "Move forward to end of current statement."
3417 (interactive)
3418 (let ((nest 0) pos)
3419 (cond
3420 ((verilog-in-directive-p)
3421 (forward-line 1)
3422 (backward-char 1))
3423
3424 ((looking-at verilog-beg-block-re)
3425 (verilog-forward-sexp))
3426
3427 ((equal (char-after) ?\})
3428 (forward-char))
3429
3430 ;; Skip to end of statement
3431 ((condition-case nil
3432 (setq pos
3433 (catch 'found
3434 (while t
3435 (forward-sexp 1)
3436 (verilog-skip-forward-comment-or-string)
3437 (if (eolp)
3438 (forward-line 1))
3439 (cond ((looking-at "[ \t]*;")
3440 (skip-chars-forward "^;")
3441 (forward-char 1)
3442 (throw 'found (point)))
3443 ((save-excursion
3444 (forward-sexp -1)
3445 (looking-at verilog-beg-block-re))
3446 (goto-char (match-beginning 0))
3447 (throw 'found nil))
3448 ((looking-at "[ \t]*)")
3449 (throw 'found (point)))
3450 ((eobp)
3451 (throw 'found (point)))
3452 )))
3453
3454 )
3455 (error nil))
3456 (if (not pos)
3457 ;; Skip a whole block
3458 (catch 'found
3459 (while t
3460 (verilog-re-search-forward verilog-end-statement-re nil 'move)
3461 (setq nest (if (match-end 1)
3462 (1+ nest)
3463 (1- nest)))
3464 (cond ((eobp)
3465 (throw 'found (point)))
3466 ((= 0 nest)
3467 (throw 'found (verilog-end-of-statement))))))
3468 pos)))))
3469
3470 (defun verilog-in-case-region-p ()
3471 "Return true if in a case region.
3472 More specifically, point @ in the line foo : @ begin"
3473 (interactive)
3474 (save-excursion
3475 (if (and
3476 (progn (verilog-forward-syntactic-ws)
3477 (looking-at "\\<begin\\>"))
3478 (progn (verilog-backward-syntactic-ws)
3479 (= (preceding-char) ?\:)))
3480 (catch 'found
3481 (let ((nest 1))
3482 (while t
3483 (verilog-re-search-backward
3484 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
3485 "\\(\\<endcase\\>\\)\\>")
3486 nil 'move)
3487 (cond
3488 ((match-end 3)
3489 (setq nest (1+ nest)))
3490 ((match-end 2)
3491 (if (= nest 1)
3492 (throw 'found 1))
3493 (setq nest (1- nest)))
3494 (t
3495 (throw 'found (= nest 0)))))))
3496 nil)))
3497 (defun verilog-in-struct-region-p ()
3498 "Return true if in a struct region.
3499 More specifically, in a list after a struct|union keyword."
3500 (interactive)
3501 (save-excursion
3502 (let* ((state (verilog-syntax-ppss))
3503 (depth (nth 0 state)))
3504 (if depth
3505 (progn (backward-up-list depth)
3506 (verilog-beg-of-statement)
3507 (looking-at "\\<typedef\\>?\\s-*\\<struct\\|union\\>"))))))
3508
3509 (defun verilog-in-generate-region-p ()
3510 "Return true if in a generate region.
3511 More specifically, after a generate and before an endgenerate."
3512 (interactive)
3513 (let ((nest 1))
3514 (save-excursion
3515 (catch 'done
3516 (while (and
3517 (/= nest 0)
3518 (verilog-re-search-backward
3519 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
3520 (cond
3521 ((match-end 1) ; module - we have crawled out
3522 (throw 'done 1))
3523 ((match-end 2) ; generate
3524 (setq nest (1- nest)))
3525 ((match-end 3) ; endgenerate
3526 (setq nest (1+ nest))))))))
3527 (= nest 0) )) ; return nest
3528
3529 (defun verilog-in-fork-region-p ()
3530 "Return true if between a fork and join."
3531 (interactive)
3532 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
3533 (nest 1))
3534 (save-excursion
3535 (while (and
3536 (/= nest 0)
3537 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
3538 (cond
3539 ((match-end 1) ; fork
3540 (setq nest (1- nest)))
3541 ((match-end 2) ; join
3542 (setq nest (1+ nest)))))))
3543 (= nest 0) )) ; return nest
3544
3545 (defun verilog-backward-case-item (lim)
3546 "Skip backward to nearest enclosing case item.
3547 Limit search to point LIM."
3548 (interactive)
3549 (let ((str 'nil)
3550 (lim1
3551 (progn
3552 (save-excursion
3553 (verilog-re-search-backward verilog-endcomment-reason-re
3554 lim 'move)
3555 (point)))))
3556 ;; Try to find the real :
3557 (if (save-excursion (search-backward ":" lim1 t))
3558 (let ((colon 0)
3559 b e )
3560 (while
3561 (and
3562 (< colon 1)
3563 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
3564 lim1 'move))
3565 (cond
3566 ((match-end 1) ;; [
3567 (setq colon (1+ colon))
3568 (if (>= colon 0)
3569 (error "%s: unbalanced [" (verilog-point-text))))
3570 ((match-end 2) ;; ]
3571 (setq colon (1- colon)))
3572
3573 ((match-end 3) ;; :
3574 (setq colon (1+ colon)))))
3575 ;; Skip back to beginning of case item
3576 (skip-chars-backward "\t ")
3577 (verilog-skip-backward-comment-or-string)
3578 (setq e (point))
3579 (setq b
3580 (progn
3581 (if
3582 (verilog-re-search-backward
3583 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
3584 (progn
3585 (cond
3586 ((match-end 1)
3587 (goto-char (match-end 1))
3588 (verilog-forward-ws&directives)
3589 (if (looking-at "(")
3590 (progn
3591 (forward-sexp)
3592 (verilog-forward-ws&directives)))
3593 (point))
3594 (t
3595 (goto-char (match-end 0))
3596 (verilog-forward-ws&directives)
3597 (point))))
3598 (error "Malformed case item"))))
3599 (setq str (buffer-substring b e))
3600 (if
3601 (setq e
3602 (string-match
3603 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
3604 (setq str (concat (substring str 0 e) "...")))
3605 str)
3606 'nil)))
3607 \f
3608
3609 ;;
3610 ;; Other functions
3611 ;;
3612
3613 (defun verilog-kill-existing-comment ()
3614 "Kill auto comment on this line."
3615 (save-excursion
3616 (let* (
3617 (e (progn
3618 (end-of-line)
3619 (point)))
3620 (b (progn
3621 (beginning-of-line)
3622 (search-forward "//" e t))))
3623 (if b
3624 (delete-region (- b 2) e)))))
3625
3626 (defconst verilog-directive-nest-re
3627 (concat "\\(`else\\>\\)\\|"
3628 "\\(`endif\\>\\)\\|"
3629 "\\(`if\\>\\)\\|"
3630 "\\(`ifdef\\>\\)\\|"
3631 "\\(`ifndef\\>\\)"))
3632 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
3633 "Add ending comment with given INDENT-STR.
3634 With KILL-EXISTING-COMMENT, remove what was there before.
3635 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
3636 Insert `// case expr ' if this line ends a case block.
3637 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
3638 Insert `// NAME ' if this line ends a function, task, module,
3639 primitive or interface named NAME."
3640 (save-excursion
3641 (cond
3642 (; Comment close preprocessor directives
3643 (and
3644 (looking-at "\\(`endif\\)\\|\\(`else\\)")
3645 (or kill-existing-comment
3646 (not (save-excursion
3647 (end-of-line)
3648 (search-backward "//" (verilog-get-beg-of-line) t)))))
3649 (let ((nest 1) b e
3650 m
3651 (else (if (match-end 2) "!" " ")))
3652 (end-of-line)
3653 (if kill-existing-comment
3654 (verilog-kill-existing-comment))
3655 (delete-horizontal-space)
3656 (save-excursion
3657 (backward-sexp 1)
3658 (while (and (/= nest 0)
3659 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
3660 (cond
3661 ((match-end 1) ; `else
3662 (if (= nest 1)
3663 (setq else "!")))
3664 ((match-end 2) ; `endif
3665 (setq nest (1+ nest)))
3666 ((match-end 3) ; `if
3667 (setq nest (1- nest)))
3668 ((match-end 4) ; `ifdef
3669 (setq nest (1- nest)))
3670 ((match-end 5) ; `ifndef
3671 (setq nest (1- nest)))))
3672 (if (match-end 0)
3673 (setq
3674 m (buffer-substring
3675 (match-beginning 0)
3676 (match-end 0))
3677 b (progn
3678 (skip-chars-forward "^ \t")
3679 (verilog-forward-syntactic-ws)
3680 (point))
3681 e (progn
3682 (skip-chars-forward "a-zA-Z0-9_")
3683 (point)))))
3684 (if b
3685 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
3686 (insert (concat " // " else m " " (buffer-substring b e))))
3687 (progn
3688 (insert " // unmatched `else or `endif")
3689 (ding 't)))))
3690
3691 (; Comment close case/class/function/task/module and named block
3692 (and (looking-at "\\<end")
3693 (or kill-existing-comment
3694 (not (save-excursion
3695 (end-of-line)
3696 (search-backward "//" (verilog-get-beg-of-line) t)))))
3697 (let ((type (car indent-str)))
3698 (unless (eq type 'declaration)
3699 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ;; ignore named ends
3700 (if (looking-at verilog-end-block-ordered-re)
3701 (cond
3702 (;- This is a case block; search back for the start of this case
3703 (match-end 1) ;; of verilog-end-block-ordered-re
3704
3705 (let ((err 't)
3706 (str "UNMATCHED!!"))
3707 (save-excursion
3708 (verilog-leap-to-head)
3709 (cond
3710 ((looking-at "\\<randcase\\>")
3711 (setq str "randcase")
3712 (setq err nil))
3713 ((looking-at "\\(\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
3714 (goto-char (match-end 0))
3715 (setq str (concat (match-string 0) " " (verilog-get-expr)))
3716 (setq err nil))
3717 ))
3718 (end-of-line)
3719 (if kill-existing-comment
3720 (verilog-kill-existing-comment))
3721 (delete-horizontal-space)
3722 (insert (concat " // " str ))
3723 (if err (ding 't))))
3724
3725 (;- This is a begin..end block
3726 (match-end 2) ;; of verilog-end-block-ordered-re
3727 (let ((str " // UNMATCHED !!")
3728 (err 't)
3729 (here (point))
3730 there
3731 cntx)
3732 (save-excursion
3733 (verilog-leap-to-head)
3734 (setq there (point))
3735 (if (not (match-end 0))
3736 (progn
3737 (goto-char here)
3738 (end-of-line)
3739 (if kill-existing-comment
3740 (verilog-kill-existing-comment))
3741 (delete-horizontal-space)
3742 (insert str)
3743 (ding 't))
3744 (let ((lim
3745 (save-excursion (verilog-beg-of-defun) (point)))
3746 (here (point)))
3747 (cond
3748 (;-- handle named block differently
3749 (looking-at verilog-named-block-re)
3750 (search-forward ":")
3751 (setq there (point))
3752 (setq str (verilog-get-expr))
3753 (setq err nil)
3754 (setq str (concat " // block: " str )))
3755
3756 ((verilog-in-case-region-p) ;-- handle case item differently
3757 (goto-char here)
3758 (setq str (verilog-backward-case-item lim))
3759 (setq there (point))
3760 (setq err nil)
3761 (setq str (concat " // case: " str )))
3762
3763 (;- try to find "reason" for this begin
3764 (cond
3765 (;
3766 (eq here (progn
3767 (verilog-backward-token)
3768 (verilog-beg-of-statement-1)
3769 (point)))
3770 (setq err nil)
3771 (setq str ""))
3772 ((looking-at verilog-endcomment-reason-re)
3773 (setq there (match-end 0))
3774 (setq cntx (concat (match-string 0) " "))
3775 (cond
3776 (;- begin
3777 (match-end 1)
3778 (setq err nil)
3779 (save-excursion
3780 (if (and (verilog-continued-line)
3781 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
3782 (progn
3783 (goto-char (match-end 0))
3784 (setq there (point))
3785 (setq str
3786 (concat " // " (match-string 0) " " (verilog-get-expr))))
3787 (setq str ""))))
3788
3789 (;- else
3790 (match-end 2)
3791 (let ((nest 0)
3792 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)"))
3793 (catch 'skip
3794 (while (verilog-re-search-backward reg nil 'move)
3795 (cond
3796 ((match-end 1) ; begin
3797 (setq nest (1- nest)))
3798 ((match-end 2) ; end
3799 (setq nest (1+ nest)))
3800 ((match-end 3)
3801 (if (= 0 nest)
3802 (progn
3803 (goto-char (match-end 0))
3804 (setq there (point))
3805 (setq err nil)
3806 (setq str (verilog-get-expr))
3807 (setq str (concat " // else: !if" str ))
3808 (throw 'skip 1)))))))))
3809
3810 (;- end else
3811 (match-end 3)
3812 (goto-char there)
3813 (let ((nest 0)
3814 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)"))
3815 (catch 'skip
3816 (while (verilog-re-search-backward reg nil 'move)
3817 (cond
3818 ((match-end 1) ; begin
3819 (setq nest (1- nest)))
3820 ((match-end 2) ; end
3821 (setq nest (1+ nest)))
3822 ((match-end 3)
3823 (if (= 0 nest)
3824 (progn
3825 (goto-char (match-end 0))
3826 (setq there (point))
3827 (setq err nil)
3828 (setq str (verilog-get-expr))
3829 (setq str (concat " // else: !if" str ))
3830 (throw 'skip 1)))))))))
3831 (; always_comb, always_ff, always_latch
3832 (or (match-end 4) (match-end 5) (match-end 6))
3833 (goto-char (match-end 0))
3834 (setq there (point))
3835 (setq err nil)
3836 (setq str (concat " // " cntx )))
3837
3838 (;- task/function/initial et cetera
3839 t
3840 (match-end 0)
3841 (goto-char (match-end 0))
3842 (setq there (point))
3843 (setq err nil)
3844 (setq str (concat " // " cntx (verilog-get-expr))))
3845
3846 (;-- otherwise...
3847 (setq str " // auto-endcomment confused "))))
3848
3849 ((and
3850 (verilog-in-case-region-p) ;-- handle case item differently
3851 (progn
3852 (setq there (point))
3853 (goto-char here)
3854 (setq str (verilog-backward-case-item lim))))
3855 (setq err nil)
3856 (setq str (concat " // case: " str )))
3857
3858 ((verilog-in-fork-region-p)
3859 (setq err nil)
3860 (setq str " // fork branch" ))
3861
3862 ((looking-at "\\<end\\>")
3863 ;; HERE
3864 (forward-word 1)
3865 (verilog-forward-syntactic-ws)
3866 (setq err nil)
3867 (setq str (verilog-get-expr))
3868 (setq str (concat " // " cntx str )))
3869
3870 ))))
3871 (goto-char here)
3872 (end-of-line)
3873 (if kill-existing-comment
3874 (verilog-kill-existing-comment))
3875 (delete-horizontal-space)
3876 (if (or err
3877 (> (count-lines here there) verilog-minimum-comment-distance))
3878 (insert str))
3879 (if err (ding 't))
3880 ))))
3881 (;- this is endclass, which can be nested
3882 (match-end 11) ;; of verilog-end-block-ordered-re
3883 ;;(goto-char there)
3884 (let ((nest 0)
3885 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
3886 string)
3887 (save-excursion
3888 (catch 'skip
3889 (while (verilog-re-search-backward reg nil 'move)
3890 (cond
3891 ((match-end 3) ; endclass
3892 (ding 't)
3893 (setq string "unmatched endclass")
3894 (throw 'skip 1))
3895
3896 ((match-end 2) ; endclass
3897 (setq nest (1+ nest)))
3898
3899 ((match-end 1) ; class
3900 (setq nest (1- nest))
3901 (if (< nest 0)
3902 (progn
3903 (goto-char (match-end 0))
3904 (let (b e)
3905 (setq b (progn
3906 (skip-chars-forward "^ \t")
3907 (verilog-forward-ws&directives)
3908 (point))
3909 e (progn
3910 (skip-chars-forward "a-zA-Z0-9_")
3911 (point)))
3912 (setq string (buffer-substring b e)))
3913 (throw 'skip 1))))
3914 ))))
3915 (end-of-line)
3916 (insert (concat " // " string ))))
3917
3918 (;- this is end{function,generate,task,module,primitive,table,generate}
3919 ;- which can not be nested.
3920 t
3921 (let (string reg (name-re nil))
3922 (end-of-line)
3923 (if kill-existing-comment
3924 (save-match-data
3925 (verilog-kill-existing-comment)))
3926 (delete-horizontal-space)
3927 (backward-sexp)
3928 (cond
3929 ((match-end 5) ;; of verilog-end-block-ordered-re
3930 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
3931 (setq name-re "\\w+\\s-*(")
3932 )
3933 ((match-end 6) ;; of verilog-end-block-ordered-re
3934 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)"))
3935 ((match-end 7) ;; of verilog-end-block-ordered-re
3936 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
3937 ((match-end 8) ;; of verilog-end-block-ordered-re
3938 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3939 ((match-end 9) ;; of verilog-end-block-ordered-re
3940 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
3941 ((match-end 10) ;; of verilog-end-block-ordered-re
3942 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3943 ((match-end 11) ;; of verilog-end-block-ordered-re
3944 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3945 ((match-end 12) ;; of verilog-end-block-ordered-re
3946 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3947 ((match-end 13) ;; of verilog-end-block-ordered-re
3948 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3949 ((match-end 14) ;; of verilog-end-block-ordered-re
3950 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
3951 ((match-end 15) ;; of verilog-end-block-ordered-re
3952 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
3953
3954 (t (error "Problem in verilog-set-auto-endcomments")))
3955 (let (b e)
3956 (save-excursion
3957 (verilog-re-search-backward reg nil 'move)
3958 (cond
3959 ((match-end 1)
3960 (setq b (progn
3961 (skip-chars-forward "^ \t")
3962 (verilog-forward-ws&directives)
3963 (if (and name-re (verilog-re-search-forward name-re nil 'move))
3964 (progn
3965 (goto-char (match-beginning 0))
3966 (verilog-forward-ws&directives)))
3967 (point))
3968 e (progn
3969 (skip-chars-forward "a-zA-Z0-9_")
3970 (point)))
3971 (setq string (buffer-substring b e)))
3972 (t
3973 (ding 't)
3974 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
3975 (end-of-line)
3976 (insert (concat " // " string )))
3977 ))))))))))
3978
3979 (defun verilog-get-expr()
3980 "Grab expression at point, e.g, case ( a | b & (c ^d))."
3981 (let* ((b (progn
3982 (verilog-forward-syntactic-ws)
3983 (skip-chars-forward " \t")
3984 (point)))
3985 (e (let ((par 1))
3986 (cond
3987 ((looking-at "@")
3988 (forward-char 1)
3989 (verilog-forward-syntactic-ws)
3990 (if (looking-at "(")
3991 (progn
3992 (forward-char 1)
3993 (while (and (/= par 0)
3994 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
3995 (cond
3996 ((match-end 1)
3997 (setq par (1+ par)))
3998 ((match-end 2)
3999 (setq par (1- par)))))))
4000 (point))
4001 ((looking-at "(")
4002 (forward-char 1)
4003 (while (and (/= par 0)
4004 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4005 (cond
4006 ((match-end 1)
4007 (setq par (1+ par)))
4008 ((match-end 2)
4009 (setq par (1- par)))))
4010 (point))
4011 ((looking-at "\\[")
4012 (forward-char 1)
4013 (while (and (/= par 0)
4014 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
4015 (cond
4016 ((match-end 1)
4017 (setq par (1+ par)))
4018 ((match-end 2)
4019 (setq par (1- par)))))
4020 (verilog-forward-syntactic-ws)
4021 (skip-chars-forward "^ \t\n\f")
4022 (point))
4023 ((looking-at "/[/\\*]")
4024 b)
4025 ('t
4026 (skip-chars-forward "^: \t\n\f")
4027 (point)))))
4028 (str (buffer-substring b e)))
4029 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4030 (setq str (concat (substring str 0 e) "...")))
4031 str))
4032
4033 (defun verilog-expand-vector ()
4034 "Take a signal vector on the current line and expand it to multiple lines.
4035 Useful for creating tri's and other expanded fields."
4036 (interactive)
4037 (verilog-expand-vector-internal "[" "]"))
4038
4039 (defun verilog-expand-vector-internal (bra ket)
4040 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
4041 (save-excursion
4042 (forward-line 0)
4043 (let ((signal-string (buffer-substring (point)
4044 (progn
4045 (end-of-line) (point)))))
4046 (if (string-match
4047 (concat "\\(.*\\)"
4048 (regexp-quote bra)
4049 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
4050 (regexp-quote ket)
4051 "\\(.*\\)$") signal-string)
4052 (let* ((sig-head (match-string 1 signal-string))
4053 (vec-start (string-to-number (match-string 2 signal-string)))
4054 (vec-end (if (= (match-beginning 3) (match-end 3))
4055 vec-start
4056 (string-to-number
4057 (substring signal-string (1+ (match-beginning 3))
4058 (match-end 3)))))
4059 (vec-range
4060 (if (= (match-beginning 4) (match-end 4))
4061 1
4062 (string-to-number
4063 (substring signal-string (+ 2 (match-beginning 4))
4064 (match-end 4)))))
4065 (sig-tail (match-string 5 signal-string))
4066 vec)
4067 ;; Decode vectors
4068 (setq vec nil)
4069 (if (< vec-range 0)
4070 (let ((tmp vec-start))
4071 (setq vec-start vec-end
4072 vec-end tmp
4073 vec-range (- vec-range))))
4074 (if (< vec-end vec-start)
4075 (while (<= vec-end vec-start)
4076 (setq vec (append vec (list vec-start)))
4077 (setq vec-start (- vec-start vec-range)))
4078 (while (<= vec-start vec-end)
4079 (setq vec (append vec (list vec-start)))
4080 (setq vec-start (+ vec-start vec-range))))
4081 ;;
4082 ;; Delete current line
4083 (delete-region (point) (progn (forward-line 0) (point)))
4084 ;;
4085 ;; Expand vector
4086 (while vec
4087 (insert (concat sig-head bra
4088 (int-to-string (car vec)) ket sig-tail "\n"))
4089 (setq vec (cdr vec)))
4090 (delete-char -1)
4091 ;;
4092 )))))
4093
4094 (defun verilog-strip-comments ()
4095 "Strip all comments from the Verilog code."
4096 (interactive)
4097 (goto-char (point-min))
4098 (while (re-search-forward "//" nil t)
4099 (if (verilog-within-string)
4100 (re-search-forward "\"" nil t)
4101 (if (verilog-in-star-comment-p)
4102 (re-search-forward "\*/" nil t)
4103 (let ((bpt (- (point) 2)))
4104 (end-of-line)
4105 (delete-region bpt (point))))))
4106 ;;
4107 (goto-char (point-min))
4108 (while (re-search-forward "/\\*" nil t)
4109 (if (verilog-within-string)
4110 (re-search-forward "\"" nil t)
4111 (let ((bpt (- (point) 2)))
4112 (re-search-forward "\\*/")
4113 (delete-region bpt (point))))))
4114
4115 (defun verilog-one-line ()
4116 "Convert structural Verilog instances to occupy one line."
4117 (interactive)
4118 (goto-char (point-min))
4119 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
4120 (replace-match "\\1 " nil nil)))
4121
4122 (defun verilog-linter-name ()
4123 "Return name of linter, either surelint or verilint."
4124 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4125 compile-command))
4126 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4127 verilog-linter)))
4128 (cond ((equal compile-word1 "surelint") `surelint)
4129 ((equal compile-word1 "verilint") `verilint)
4130 ((equal lint-word1 "surelint") `surelint)
4131 ((equal lint-word1 "verilint") `verilint)
4132 (t `surelint)))) ;; back compatibility
4133
4134 (defun verilog-lint-off ()
4135 "Convert a Verilog linter warning line into a disable statement.
4136 For example:
4137 pci_bfm_null.v, line 46: Unused input: pci_rst_
4138 becomes a comment for the appropriate tool.
4139
4140 The first word of the `compile-command' or `verilog-linter'
4141 variables is used to determine which product is being used.
4142
4143 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
4144 (interactive)
4145 (let ((linter (verilog-linter-name)))
4146 (cond ((equal linter `surelint)
4147 (verilog-surelint-off))
4148 ((equal linter `verilint)
4149 (verilog-verilint-off))
4150 (t (error "Linter name not set")))))
4151
4152 (defvar compilation-last-buffer)
4153 (defvar next-error-last-buffer)
4154
4155 (defun verilog-surelint-off ()
4156 "Convert a SureLint warning line into a disable statement.
4157 Run from Verilog source window; assumes there is a *compile* buffer
4158 with point set appropriately.
4159
4160 For example:
4161 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
4162 becomes:
4163 // surefire lint_line_off UDDONX"
4164 (interactive)
4165 (let ((buff (if (boundp 'next-error-last-buffer)
4166 next-error-last-buffer
4167 compilation-last-buffer)))
4168 (when (buffer-live-p buff)
4169 ;; FIXME with-current-buffer?
4170 (save-excursion
4171 (switch-to-buffer buff)
4172 (beginning-of-line)
4173 (when
4174 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
4175 (let* ((code (match-string 2))
4176 (file (match-string 3))
4177 (line (match-string 4))
4178 (buffer (get-file-buffer file))
4179 dir filename)
4180 (unless buffer
4181 (progn
4182 (setq buffer
4183 (and (file-exists-p file)
4184 (find-file-noselect file)))
4185 (or buffer
4186 (let* ((pop-up-windows t))
4187 (let ((name (expand-file-name
4188 (read-file-name
4189 (format "Find this error in: (default %s) "
4190 file)
4191 dir file t))))
4192 (if (file-directory-p name)
4193 (setq name (expand-file-name filename name)))
4194 (setq buffer
4195 (and (file-exists-p name)
4196 (find-file-noselect name))))))))
4197 (switch-to-buffer buffer)
4198 (goto-char (point-min))
4199 (forward-line (- (string-to-number line)))
4200 (end-of-line)
4201 (catch 'already
4202 (cond
4203 ((verilog-in-slash-comment-p)
4204 (re-search-backward "//")
4205 (cond
4206 ((looking-at "// surefire lint_off_line ")
4207 (goto-char (match-end 0))
4208 (let ((lim (save-excursion (end-of-line) (point))))
4209 (if (re-search-forward code lim 'move)
4210 (throw 'already t)
4211 (insert (concat " " code)))))
4212 (t
4213 )))
4214 ((verilog-in-star-comment-p)
4215 (re-search-backward "/\*")
4216 (insert (format " // surefire lint_off_line %6s" code )))
4217 (t
4218 (insert (format " // surefire lint_off_line %6s" code ))
4219 )))))))))
4220
4221 (defun verilog-verilint-off ()
4222 "Convert a Verilint warning line into a disable statement.
4223
4224 For example:
4225 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
4226 becomes:
4227 //Verilint 240 off // WARNING: Unused input"
4228 (interactive)
4229 (save-excursion
4230 (beginning-of-line)
4231 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
4232 (replace-match (format
4233 ;; %3s makes numbers 1-999 line up nicely
4234 "\\1//Verilint %3s off // WARNING: \\3"
4235 (match-string 2)))
4236 (beginning-of-line)
4237 (verilog-indent-line))))
4238
4239 (defun verilog-auto-save-compile ()
4240 "Update automatics with \\[verilog-auto], save the buffer, and compile."
4241 (interactive)
4242 (verilog-auto) ; Always do it for safety
4243 (save-buffer)
4244 (compile compile-command))
4245
4246 \f
4247
4248 ;;
4249 ;; Batch
4250 ;;
4251
4252 (defmacro verilog-batch-error-wrapper (&rest body)
4253 "Execute BODY and add error prefix to any errors found.
4254 This lets programs calling batch mode to easily extract error messages."
4255 `(condition-case err
4256 (progn ,@body)
4257 (error
4258 (error "%%Error: %s%s" (error-message-string err)
4259 (if (featurep 'xemacs) "\n" ""))))) ;; XEmacs forgets to add a newline
4260
4261 (defun verilog-batch-execute-func (funref)
4262 "Internal processing of a batch command, running FUNREF on all command arguments."
4263 (verilog-batch-error-wrapper
4264 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
4265 ;; However, this function is called only when Emacs is being used as
4266 ;; a standalone language instead of as an editor, so we'll live.
4267 ;;
4268 ;; General globals needed
4269 (setq make-backup-files nil)
4270 (setq-default make-backup-files nil)
4271 (setq enable-local-variables t)
4272 (setq enable-local-eval t)
4273 ;; Make sure any sub-files we read get proper mode
4274 (setq-default major-mode 'verilog-mode)
4275 ;; Ditto files already read in
4276 (mapc (lambda (buf)
4277 (when (buffer-file-name buf)
4278 (with-current-buffer buf
4279 (verilog-mode))))
4280 (buffer-list))
4281 ;; Process the files
4282 (mapcar '(lambda (buf)
4283 (when (buffer-file-name buf)
4284 (save-excursion
4285 (if (not (file-exists-p (buffer-file-name buf)))
4286 (error
4287 (concat "File not found: " (buffer-file-name buf))))
4288 (message (concat "Processing " (buffer-file-name buf)))
4289 (set-buffer buf)
4290 (funcall funref)
4291 (save-buffer))))
4292 (buffer-list))))
4293
4294 (defun verilog-batch-auto ()
4295 "For use with --batch, perform automatic expansions as a stand-alone tool.
4296 This sets up the appropriate Verilog mode environment, updates automatics
4297 with \\[verilog-auto] on all command-line files, and saves the buffers.
4298 For proper results, multiple filenames need to be passed on the command
4299 line in bottom-up order."
4300 (unless noninteractive
4301 (error "Use verilog-batch-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
4302 (verilog-batch-execute-func `verilog-auto))
4303
4304 (defun verilog-batch-delete-auto ()
4305 "For use with --batch, perform automatic deletion as a stand-alone tool.
4306 This sets up the appropriate Verilog mode environment, deletes automatics
4307 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
4308 (unless noninteractive
4309 (error "Use verilog-batch-delete-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
4310 (verilog-batch-execute-func `verilog-delete-auto))
4311
4312 (defun verilog-batch-inject-auto ()
4313 "For use with --batch, perform automatic injection as a stand-alone tool.
4314 This sets up the appropriate Verilog mode environment, injects new automatics
4315 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
4316 For proper results, multiple filenames need to be passed on the command
4317 line in bottom-up order."
4318 (unless noninteractive
4319 (error "Use verilog-batch-inject-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
4320 (verilog-batch-execute-func `verilog-inject-auto))
4321
4322 (defun verilog-batch-indent ()
4323 "For use with --batch, reindent an a entire file as a stand-alone tool.
4324 This sets up the appropriate Verilog mode environment, calls
4325 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
4326 (unless noninteractive
4327 (error "Use verilog-batch-indent only with --batch")) ;; Otherwise we'd mess up buffer modes
4328 (verilog-batch-execute-func `verilog-indent-buffer))
4329 \f
4330
4331 ;;
4332 ;; Indentation
4333 ;;
4334 (defconst verilog-indent-alist
4335 '((block . (+ ind verilog-indent-level))
4336 (case . (+ ind verilog-case-indent))
4337 (cparenexp . (+ ind verilog-indent-level))
4338 (cexp . (+ ind verilog-cexp-indent))
4339 (defun . verilog-indent-level-module)
4340 (declaration . verilog-indent-level-declaration)
4341 (directive . (verilog-calculate-indent-directive))
4342 (tf . verilog-indent-level)
4343 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
4344 (statement . ind)
4345 (cpp . 0)
4346 (comment . (verilog-comment-indent))
4347 (unknown . 3)
4348 (string . 0)))
4349
4350 (defun verilog-continued-line-1 (lim)
4351 "Return true if this is a continued line.
4352 Set point to where line starts. Limit search to point LIM."
4353 (let ((continued 't))
4354 (if (eq 0 (forward-line -1))
4355 (progn
4356 (end-of-line)
4357 (verilog-backward-ws&directives lim)
4358 (if (bobp)
4359 (setq continued nil)
4360 (setq continued (verilog-backward-token))))
4361 (setq continued nil))
4362 continued))
4363
4364 (defun verilog-calculate-indent ()
4365 "Calculate the indent of the current Verilog line.
4366 Examine previous lines. Once a line is found that is definitive as to the
4367 type of the current line, return that lines' indent level and its type.
4368 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
4369 (save-excursion
4370 (let* ((starting_position (point))
4371 (par 0)
4372 (begin (looking-at "[ \t]*begin\\>"))
4373 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
4374 (type (catch 'nesting
4375 ;; Keep working backwards until we can figure out
4376 ;; what type of statement this is.
4377 ;; Basically we need to figure out
4378 ;; 1) if this is a continuation of the previous line;
4379 ;; 2) are we in a block scope (begin..end)
4380
4381 ;; if we are in a comment, done.
4382 (if (verilog-in-star-comment-p)
4383 (throw 'nesting 'comment))
4384
4385 ;; if we have a directive, done.
4386 (if (save-excursion (beginning-of-line)
4387 (and (looking-at verilog-directive-re-1)
4388 (not (or (looking-at "[ \t]*`ovm_")
4389 (looking-at "[ \t]*`vmm_")))))
4390 (throw 'nesting 'directive))
4391 ;; indent structs as if there were module level
4392 (if (verilog-in-struct-p)
4393 (throw 'nesting 'block))
4394
4395 ;; unless we are in the newfangled coverpoint or constraint blocks
4396 ;; if we are in a parenthesized list, and the user likes to indent these, return.
4397 (if (and
4398 verilog-indent-lists
4399 (verilog-in-paren)
4400 (not (verilog-in-coverage-p))
4401 )
4402 (progn (setq par 1)
4403 (throw 'nesting 'block)))
4404
4405 ;; See if we are continuing a previous line
4406 (while t
4407 ;; trap out if we crawl off the top of the buffer
4408 (if (bobp) (throw 'nesting 'cpp))
4409
4410 (if (verilog-continued-line-1 lim)
4411 (let ((sp (point)))
4412 (if (and
4413 (not (looking-at verilog-complete-reg))
4414 (verilog-continued-line-1 lim))
4415 (progn (goto-char sp)
4416 (throw 'nesting 'cexp))
4417
4418 (goto-char sp))
4419
4420 (if (and begin
4421 (not verilog-indent-begin-after-if)
4422 (looking-at verilog-no-indent-begin-re))
4423 (progn
4424 (beginning-of-line)
4425 (skip-chars-forward " \t")
4426 (throw 'nesting 'statement))
4427 (progn
4428 (throw 'nesting 'cexp))))
4429 ;; not a continued line
4430 (goto-char starting_position))
4431
4432 (if (looking-at "\\<else\\>")
4433 ;; search back for governing if, striding across begin..end pairs
4434 ;; appropriately
4435 (let ((elsec 1))
4436 (while (verilog-re-search-backward verilog-ends-re nil 'move)
4437 (cond
4438 ((match-end 1) ; else, we're in deep
4439 (setq elsec (1+ elsec)))
4440 ((match-end 2) ; if
4441 (setq elsec (1- elsec))
4442 (if (= 0 elsec)
4443 (if verilog-align-ifelse
4444 (throw 'nesting 'statement)
4445 (progn ;; back up to first word on this line
4446 (beginning-of-line)
4447 (verilog-forward-syntactic-ws)
4448 (throw 'nesting 'statement)))))
4449 ((match-end 3) ; assert block
4450 (setq elsec (1- elsec))
4451 (verilog-beg-of-statement) ;; doesn't get to beginning
4452 (if (looking-at (concat "\\(" verilog-label-re "\\)?"
4453 "\\(assert\\|assume\\|cover\\)\\s-+property\\>"))
4454 (throw 'nesting 'statement) ; We don't need an endproperty for these
4455 (throw 'nesting 'block) ;We still need a endproperty
4456 ))
4457 (t ; endblock
4458 ; try to leap back to matching outward block by striding across
4459 ; indent level changing tokens then immediately
4460 ; previous line governs indentation.
4461 (let (( reg) (nest 1))
4462 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
4463 (cond
4464 ((match-end 4) ; end
4465 ;; Search back for matching begin
4466 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
4467 ((match-end 5) ; endcase
4468 ;; Search back for matching case
4469 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
4470 ((match-end 6) ; endfunction
4471 ;; Search back for matching function
4472 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
4473 ((match-end 7) ; endtask
4474 ;; Search back for matching task
4475 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
4476 ((match-end 8) ; endspecify
4477 ;; Search back for matching specify
4478 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
4479 ((match-end 9) ; endtable
4480 ;; Search back for matching table
4481 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
4482 ((match-end 10) ; endgenerate
4483 ;; Search back for matching generate
4484 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
4485 ((match-end 11) ; joins
4486 ;; Search back for matching fork
4487 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
4488 ((match-end 12) ; class
4489 ;; Search back for matching class
4490 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
4491 ((match-end 13) ; covergroup
4492 ;; Search back for matching covergroup
4493 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
4494 (catch 'skip
4495 (while (verilog-re-search-backward reg nil 'move)
4496 (cond
4497 ((match-end 1) ; begin
4498 (setq nest (1- nest))
4499 (if (= 0 nest)
4500 (throw 'skip 1)))
4501 ((match-end 2) ; end
4502 (setq nest (1+ nest)))))
4503 )))))))
4504 (throw 'nesting (verilog-calc-1)))
4505 );; catch nesting
4506 );; type
4507 )
4508 ;; Return type of block and indent level.
4509 (if (not type)
4510 (setq type 'cpp))
4511 (if (> par 0) ; Unclosed Parenthesis
4512 (list 'cparenexp par)
4513 (cond
4514 ((eq type 'case)
4515 (list type (verilog-case-indent-level)))
4516 ((eq type 'statement)
4517 (list type (current-column)))
4518 ((eq type 'defun)
4519 (list type 0))
4520 (t
4521 (list type (verilog-current-indent-level))))))))
4522
4523 (defun verilog-wai ()
4524 "Show matching nesting block for debugging."
4525 (interactive)
4526 (save-excursion
4527 (let* ((type (verilog-calc-1))
4528 depth)
4529 ;; Return type of block and indent level.
4530 (if (not type)
4531 (setq type 'cpp))
4532 (if (and
4533 verilog-indent-lists
4534 (not(or (verilog-in-coverage-p)
4535 (verilog-in-struct-p)))
4536 (verilog-in-paren))
4537 (setq depth 1)
4538 (cond
4539 ((eq type 'case)
4540 (setq depth (verilog-case-indent-level)))
4541 ((eq type 'statement)
4542 (setq depth (current-column)))
4543 ((eq type 'defun)
4544 (setq depth 0))
4545 (t
4546 (setq depth (verilog-current-indent-level)))))
4547 (message "You are at nesting %s depth %d" type depth))))
4548
4549 (defun verilog-calc-1 ()
4550 (catch 'nesting
4551 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)")))
4552 (while (verilog-re-search-backward re nil 'move)
4553 (catch 'continue
4554 (cond
4555 ((equal (char-after) ?\{)
4556 (if (verilog-at-constraint-p)
4557 (throw 'nesting 'block)))
4558
4559 ((equal (char-after) ?\})
4560 (let ((there (verilog-at-close-constraint-p)))
4561 (if there ;; we are at the } that closes a constraint. Find the { that opens it
4562 (progn
4563 (forward-char 1)
4564 (backward-list 1)
4565 (verilog-beg-of-statement)))))
4566
4567 ((looking-at verilog-beg-block-re-ordered)
4568 (cond
4569 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
4570 (let ((here (point)))
4571 (verilog-beg-of-statement)
4572 (if (looking-at verilog-extended-case-re)
4573 (throw 'nesting 'case)
4574 (goto-char here)))
4575 (throw 'nesting 'case))
4576
4577 ((match-end 4) ; *sigh* could be "disable fork"
4578 (let ((here (point)))
4579 (verilog-beg-of-statement)
4580 (if (or (looking-at verilog-disable-fork-re)
4581 (looking-at verilog-fork-wait-re))
4582 t ; this is a normal statement
4583 (progn ; or is fork, starts a new block
4584 (goto-char here)
4585 (throw 'nesting 'block)))))
4586
4587
4588 ;; need to consider typedef struct here...
4589 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
4590 ; *sigh* These words have an optional prefix:
4591 ; extern {virtual|protected}? function a();
4592 ; typedef class foo;
4593 ; and we don't want to confuse this with
4594 ; function a();
4595 ; property
4596 ; ...
4597 ; endfunction
4598 (verilog-beg-of-statement)
4599 (if (looking-at verilog-beg-block-re-ordered)
4600 (throw 'nesting 'block)
4601 (throw 'nesting 'defun)))
4602
4603 ((looking-at "\\<property\\>")
4604 ; *sigh*
4605 ; {assert|assume|cover} property (); are complete
4606 ; and could also be labeled: - foo: assert property
4607 ; but
4608 ; property ID () ... needs end_property
4609 (verilog-beg-of-statement)
4610 (if (looking-at (concat "\\(" verilog-label-re "\\)?"
4611 "\\(assert\\|assume\\|cover\\)\\s-+property\\>"))
4612 (throw 'continue 'statement) ; We don't need an endproperty for these
4613 (throw 'nesting 'block) ;We still need a endproperty
4614 ))
4615
4616 (t (throw 'nesting 'block))))
4617
4618 ((looking-at verilog-end-block-re)
4619 (verilog-leap-to-head)
4620 (if (verilog-in-case-region-p)
4621 (progn
4622 (verilog-leap-to-case-head)
4623 (if (looking-at verilog-extended-case-re)
4624 (throw 'nesting 'case)))))
4625
4626 ((looking-at verilog-defun-level-re)
4627 (if (looking-at verilog-defun-level-generate-only-re)
4628 (if (verilog-in-generate-region-p)
4629 (throw 'continue 'foo) ; always block in a generate - keep looking
4630 (throw 'nesting 'defun))
4631 (throw 'nesting 'defun)))
4632
4633 ((looking-at verilog-cpp-level-re)
4634 (throw 'nesting 'cpp))
4635
4636 ((bobp)
4637 (throw 'nesting 'cpp)))))
4638
4639 (throw 'nesting 'cpp))))
4640
4641 (defun verilog-calculate-indent-directive ()
4642 "Return indentation level for directive.
4643 For speed, the searcher looks at the last directive, not the indent
4644 of the appropriate enclosing block."
4645 (let ((base -1) ;; Indent of the line that determines our indentation
4646 (ind 0)) ;; Relative offset caused by other directives (like `endif on same line as `else)
4647 ;; Start at current location, scan back for another directive
4648
4649 (save-excursion
4650 (beginning-of-line)
4651 (while (and (< base 0)
4652 (verilog-re-search-backward verilog-directive-re nil t))
4653 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
4654 (setq base (current-indentation))))
4655 (cond ((and (looking-at verilog-directive-end) (< base 0)) ;; Only matters when not at BOL
4656 (setq ind (- ind verilog-indent-level-directive)))
4657 ((and (looking-at verilog-directive-middle) (>= base 0)) ;; Only matters when at BOL
4658 (setq ind (+ ind verilog-indent-level-directive)))
4659 ((looking-at verilog-directive-begin)
4660 (setq ind (+ ind verilog-indent-level-directive)))))
4661 ;; Adjust indent to starting indent of critical line
4662 (setq ind (max 0 (+ ind base))))
4663
4664 (save-excursion
4665 (beginning-of-line)
4666 (skip-chars-forward " \t")
4667 (cond ((or (looking-at verilog-directive-middle)
4668 (looking-at verilog-directive-end))
4669 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
4670 ind))
4671
4672 (defun verilog-leap-to-case-head ()
4673 (let ((nest 1))
4674 (while (/= 0 nest)
4675 (verilog-re-search-backward
4676 (concat
4677 "\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
4678 "\\|\\(\\<endcase\\>\\)" )
4679 nil 'move)
4680 (cond
4681 ((match-end 1)
4682 (let ((here (point)))
4683 (verilog-beg-of-statement)
4684 (unless (looking-at verilog-extended-case-re)
4685 (goto-char here)))
4686 (setq nest (1- nest)))
4687 ((match-end 3)
4688 (setq nest (1+ nest)))
4689 ((bobp)
4690 (ding 't)
4691 (setq nest 0))))))
4692
4693 (defun verilog-leap-to-head ()
4694 "Move point to the head of this block.
4695 Jump from end to matching begin, from endcase to matching case, and so on."
4696 (let ((reg nil)
4697 snest
4698 (nesting 'yes)
4699 (nest 1))
4700 (cond
4701 ((looking-at "\\<end\\>")
4702 ;; 1: Search back for matching begin
4703 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
4704 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
4705 ((looking-at "\\<endtask\\>")
4706 ;; 2: Search back for matching task
4707 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
4708 (setq nesting 'no))
4709 ((looking-at "\\<endcase\\>")
4710 (catch 'nesting
4711 (verilog-leap-to-case-head) )
4712 (setq reg nil) ; to force skip
4713 )
4714
4715 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
4716 ;; 4: Search back for matching fork
4717 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
4718 ((looking-at "\\<endclass\\>")
4719 ;; 5: Search back for matching class
4720 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
4721 ((looking-at "\\<endtable\\>")
4722 ;; 6: Search back for matching table
4723 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
4724 ((looking-at "\\<endspecify\\>")
4725 ;; 7: Search back for matching specify
4726 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
4727 ((looking-at "\\<endfunction\\>")
4728 ;; 8: Search back for matching function
4729 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
4730 (setq nesting 'no))
4731 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
4732 ((looking-at "\\<endgenerate\\>")
4733 ;; 8: Search back for matching generate
4734 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
4735 ((looking-at "\\<endgroup\\>")
4736 ;; 10: Search back for matching covergroup
4737 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
4738 ((looking-at "\\<endproperty\\>")
4739 ;; 11: Search back for matching property
4740 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
4741 ((looking-at verilog-ovm-end-re)
4742 ;; 12: Search back for matching sequence
4743 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
4744 ((looking-at verilog-vmm-end-re)
4745 ;; 12: Search back for matching sequence
4746 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
4747 ((looking-at "\\<endinterface\\>")
4748 ;; 12: Search back for matching interface
4749 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
4750 ((looking-at "\\<endsequence\\>")
4751 ;; 12: Search back for matching sequence
4752 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
4753 ((looking-at "\\<endclocking\\>")
4754 ;; 12: Search back for matching clocking
4755 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
4756 (if reg
4757 (catch 'skip
4758 (if (eq nesting 'yes)
4759 (let (sreg)
4760 (while (verilog-re-search-backward reg nil 'move)
4761 (cond
4762 ((match-end 1) ; begin
4763 (if (looking-at "fork")
4764 (let ((here (point)))
4765 (verilog-beg-of-statement)
4766 (unless (looking-at verilog-disable-fork-re)
4767 (goto-char here)
4768 (setq nest (1- nest))))
4769 (setq nest (1- nest)))
4770 (if (= 0 nest)
4771 ;; Now previous line describes syntax
4772 (throw 'skip 1))
4773 (if (and snest
4774 (= snest nest))
4775 (setq reg sreg)))
4776 ((match-end 2) ; end
4777 (setq nest (1+ nest)))
4778 ((match-end 3)
4779 ;; endcase, jump to case
4780 (setq snest nest)
4781 (setq nest (1+ nest))
4782 (setq sreg reg)
4783 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
4784 ((match-end 4)
4785 ;; join, jump to fork
4786 (setq snest nest)
4787 (setq nest (1+ nest))
4788 (setq sreg reg)
4789 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
4790 )))
4791 ;no nesting
4792 (if (and
4793 (verilog-re-search-backward reg nil 'move)
4794 (match-end 1)) ; task -> could be virtual and/or protected
4795 (progn
4796 (verilog-beg-of-statement)
4797 (throw 'skip 1))
4798 (throw 'skip 1)))))))
4799
4800 (defun verilog-continued-line ()
4801 "Return true if this is a continued line.
4802 Set point to where line starts."
4803 (let ((continued 't))
4804 (if (eq 0 (forward-line -1))
4805 (progn
4806 (end-of-line)
4807 (verilog-backward-ws&directives)
4808 (if (bobp)
4809 (setq continued nil)
4810 (while (and continued
4811 (save-excursion
4812 (skip-chars-backward " \t")
4813 (not (bolp))))
4814 (setq continued (verilog-backward-token)))))
4815 (setq continued nil))
4816 continued))
4817
4818 (defun verilog-backward-token ()
4819 "Step backward token, returing true if this is a continued line."
4820 (interactive)
4821 (verilog-backward-syntactic-ws)
4822 (cond
4823 ((bolp)
4824 nil)
4825 (;-- Anything ending in a ; is complete
4826 (= (preceding-char) ?\;)
4827 nil)
4828 (; If a "}" is prefixed by a ";", then this is a complete statement
4829 ; i.e.: constraint foo { a = b; }
4830 (= (preceding-char) ?\})
4831 (progn
4832 (backward-char)
4833 (not(verilog-at-close-constraint-p))))
4834 (;-- constraint foo { a = b }
4835 ; is a complete statement. *sigh*
4836 (= (preceding-char) ?\{)
4837 (progn
4838 (backward-char)
4839 (not (verilog-at-constraint-p))))
4840 (;" string "
4841 (= (preceding-char) ?\")
4842 (backward-char)
4843 (verilog-skip-backward-comment-or-string)
4844 nil)
4845
4846 (; [3:4]
4847 (= (preceding-char) ?\])
4848 (backward-char)
4849 (verilog-backward-open-bracket)
4850 t)
4851
4852 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
4853 ; also could be simply '@(foo)'
4854 ; or foo u1 #(a=8)
4855 ; (b, ... which ISN'T complete
4856 ;;;; Do we need this???
4857 (= (preceding-char) ?\))
4858 (progn
4859 (backward-char)
4860 (backward-up-list 1)
4861 (verilog-backward-syntactic-ws)
4862 (let ((back (point)))
4863 (forward-word -1)
4864 (cond
4865 ;;XX
4866 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
4867 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
4868 ((looking-at verilog-ovm-statement-re)
4869 nil)
4870 ((looking-at verilog-ovm-begin-re)
4871 t)
4872 ((looking-at verilog-ovm-end-re)
4873 t)
4874 ;; JBA find VMM macros
4875 ((looking-at verilog-vmm-statement-re)
4876 nil )
4877 ((looking-at verilog-vmm-begin-re)
4878 t)
4879 ((looking-at verilog-vmm-end-re)
4880 nil)
4881 ;; JBA trying to catch macro lines with no ; at end
4882 ((looking-at "\\<`")
4883 nil)
4884 (t
4885 (goto-char back)
4886 (cond
4887 ((= (preceding-char) ?\@)
4888 (backward-char)
4889 (save-excursion
4890 (verilog-backward-token)
4891 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
4892 ((= (preceding-char) ?\#)
4893 (backward-char))
4894 (t t)))))))
4895
4896 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
4897 t
4898 (forward-word -1)
4899 (while (= (preceding-char) ?\_)
4900 (forward-word -1))
4901 (cond
4902 ((looking-at "\\<else\\>")
4903 t)
4904 ((looking-at verilog-behavioral-block-beg-re)
4905 t)
4906 ((looking-at verilog-indent-re)
4907 nil)
4908 (t
4909 (let
4910 ((back (point)))
4911 (verilog-backward-syntactic-ws)
4912 (cond
4913 ((= (preceding-char) ?\:)
4914 (backward-char)
4915 (verilog-backward-syntactic-ws)
4916 (backward-sexp)
4917 (if (looking-at verilog-nameable-item-re )
4918 nil
4919 t))
4920 ((= (preceding-char) ?\#)
4921 (backward-char)
4922 t)
4923 ((= (preceding-char) ?\`)
4924 (backward-char)
4925 t)
4926
4927 (t
4928 (goto-char back)
4929 t))))))))
4930
4931 (defun verilog-backward-syntactic-ws ()
4932 (verilog-skip-backward-comments)
4933 (forward-comment (- (buffer-size))))
4934
4935 (defun verilog-forward-syntactic-ws ()
4936 (verilog-skip-forward-comment-p)
4937 (forward-comment (buffer-size)))
4938
4939 (defun verilog-backward-ws&directives (&optional bound)
4940 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
4941 Optional BOUND limits search."
4942 (save-restriction
4943 (let* ((bound (or bound (point-min)))
4944 (here bound)
4945 (p nil) )
4946 (if (< bound (point))
4947 (progn
4948 (let ((state (save-excursion (verilog-syntax-ppss))))
4949 (cond
4950 ((nth 7 state) ;; in // comment
4951 (verilog-re-search-backward "//" nil 'move)
4952 (skip-chars-backward "/"))
4953 ((nth 4 state) ;; in /* */ comment
4954 (verilog-re-search-backward "/\*" nil 'move))))
4955 (narrow-to-region bound (point))
4956 (while (/= here (point))
4957 (setq here (point))
4958 (verilog-skip-backward-comments)
4959 (setq p
4960 (save-excursion
4961 (beginning-of-line)
4962 (cond
4963 ((and verilog-highlight-translate-off
4964 (verilog-within-translate-off))
4965 (verilog-back-to-start-translate-off (point-min)))
4966 ((looking-at verilog-directive-re-1)
4967 (point))
4968 (t
4969 nil))))
4970 (if p (goto-char p))))))))
4971
4972 (defun verilog-forward-ws&directives (&optional bound)
4973 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
4974 Optional BOUND limits search."
4975 (save-restriction
4976 (let* ((bound (or bound (point-max)))
4977 (here bound)
4978 jump)
4979 (if (> bound (point))
4980 (progn
4981 (let ((state (save-excursion (verilog-syntax-ppss))))
4982 (cond
4983 ((nth 7 state) ;; in // comment
4984 (verilog-re-search-forward "//" nil 'move))
4985 ((nth 4 state) ;; in /* */ comment
4986 (verilog-re-search-forward "/\*" nil 'move))))
4987 (narrow-to-region (point) bound)
4988 (while (/= here (point))
4989 (setq here (point)
4990 jump nil)
4991 (forward-comment (buffer-size))
4992 (save-excursion
4993 (beginning-of-line)
4994 (if (looking-at verilog-directive-re-1)
4995 (setq jump t)))
4996 (if jump
4997 (beginning-of-line 2))))))))
4998
4999 (defun verilog-in-comment-p ()
5000 "Return true if in a star or // comment."
5001 (let ((state (save-excursion (verilog-syntax-ppss))))
5002 (or (nth 4 state) (nth 7 state))))
5003
5004 (defun verilog-in-star-comment-p ()
5005 "Return true if in a star comment."
5006 (let ((state (save-excursion (verilog-syntax-ppss))))
5007 (and
5008 (nth 4 state) ; t if in a comment of style a // or b /**/
5009 (not
5010 (nth 7 state) ; t if in a comment of style b /**/
5011 ))))
5012
5013 (defun verilog-in-slash-comment-p ()
5014 "Return true if in a slash comment."
5015 (let ((state (save-excursion (verilog-syntax-ppss))))
5016 (nth 7 state)))
5017
5018 (defun verilog-in-comment-or-string-p ()
5019 "Return true if in a string or comment."
5020 (let ((state (save-excursion (verilog-syntax-ppss))))
5021 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
5022
5023 (defun verilog-in-escaped-name-p ()
5024 "Return true if in an escaped name."
5025 (save-excursion
5026 (backward-char)
5027 (skip-chars-backward "^ \t\n\f")
5028 (if (equal (char-after (point) ) ?\\ )
5029 t
5030 nil)))
5031 (defun verilog-in-directive-p ()
5032 "Return true if in a star or // comment."
5033 (save-excursion
5034 (beginning-of-line)
5035 (looking-at verilog-directive-re-1)))
5036
5037 (defun verilog-in-paren ()
5038 "Return true if in a parenthetical expression."
5039 (let ((state (save-excursion (verilog-syntax-ppss))))
5040 (> (nth 0 state) 0 )))
5041
5042 (defun verilog-in-struct-p ()
5043 "Return true if in a struct declaration."
5044 (interactive)
5045 (save-excursion
5046 (if (verilog-in-paren)
5047 (progn
5048 (backward-up-list 1)
5049 (verilog-at-struct-p)
5050 )
5051 nil)))
5052
5053 (defun verilog-in-coverage-p ()
5054 "Return true if in a constraint or coverpoint expression."
5055 (interactive)
5056 (save-excursion
5057 (if (verilog-in-paren)
5058 (progn
5059 (backward-up-list 1)
5060 (verilog-at-constraint-p)
5061 )
5062 nil)))
5063 (defun verilog-at-close-constraint-p ()
5064 "If at the } that closes a constraint or covergroup, return true."
5065 (if (and
5066 (equal (char-after) ?\})
5067 (verilog-in-paren))
5068
5069 (save-excursion
5070 (verilog-backward-ws&directives)
5071 (if (equal (char-before) ?\;)
5072 (point)
5073 nil))))
5074
5075 (defun verilog-at-constraint-p ()
5076 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
5077 (if (save-excursion
5078 (and
5079 (equal (char-after) ?\{)
5080 (forward-list)
5081 (progn (backward-char 1)
5082 (verilog-backward-ws&directives)
5083 (equal (char-before) ?\;))))
5084 ;; maybe
5085 (verilog-re-search-backward "\\<constraint\\|coverpoint\\|cross\\>" nil 'move)
5086 ;; not
5087 nil))
5088
5089 (defun verilog-at-struct-p ()
5090 "If at the { of a struct, return true, moving point to struct."
5091 (save-excursion
5092 (if (and (equal (char-after) ?\{)
5093 (verilog-backward-token))
5094 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
5095 nil)))
5096
5097 (defun verilog-parenthesis-depth ()
5098 "Return non zero if in parenthetical-expression."
5099 (save-excursion (nth 1 (verilog-syntax-ppss))))
5100
5101
5102 (defun verilog-skip-forward-comment-or-string ()
5103 "Return true if in a string or comment."
5104 (let ((state (save-excursion (verilog-syntax-ppss))))
5105 (cond
5106 ((nth 3 state) ;Inside string
5107 (search-forward "\"")
5108 t)
5109 ((nth 7 state) ;Inside // comment
5110 (forward-line 1)
5111 t)
5112 ((nth 4 state) ;Inside any comment (hence /**/)
5113 (search-forward "*/"))
5114 (t
5115 nil))))
5116
5117 (defun verilog-skip-backward-comment-or-string ()
5118 "Return true if in a string or comment."
5119 (let ((state (save-excursion (verilog-syntax-ppss))))
5120 (cond
5121 ((nth 3 state) ;Inside string
5122 (search-backward "\"")
5123 t)
5124 ((nth 7 state) ;Inside // comment
5125 (search-backward "//")
5126 (skip-chars-backward "/")
5127 t)
5128 ((nth 4 state) ;Inside /* */ comment
5129 (search-backward "/*")
5130 t)
5131 (t
5132 nil))))
5133
5134 (defun verilog-skip-backward-comments ()
5135 "Return true if a comment was skipped."
5136 (let ((more t))
5137 (while more
5138 (setq more
5139 (let ((state (save-excursion (verilog-syntax-ppss))))
5140 (cond
5141 ((nth 7 state) ;Inside // comment
5142 (search-backward "//")
5143 (skip-chars-backward "/")
5144 (skip-chars-backward " \t\n\f")
5145 t)
5146 ((nth 4 state) ;Inside /* */ comment
5147 (search-backward "/*")
5148 (skip-chars-backward " \t\n\f")
5149 t)
5150 ((if (and (not (bobp))
5151 (= (char-before) ?\/)
5152 (= (char-before (1- (point))) ?\*))
5153 (goto-char (- (point) 2))
5154 (/= (skip-chars-backward " \t\n\f") 0)))))))))
5155
5156 (defun verilog-skip-forward-comment-p ()
5157 "If in comment, move to end and return true."
5158 (let (state)
5159 (progn
5160 (setq state (save-excursion (verilog-syntax-ppss)))
5161 (cond
5162 ((nth 3 state) ;Inside string
5163 t)
5164 ((nth 7 state) ;Inside // comment
5165 (end-of-line)
5166 (forward-char 1)
5167 t)
5168 ((nth 4 state) ;Inside any comment
5169 (search-forward "*/")
5170 (skip-chars-forward " \t\n\f")
5171 t)
5172 (t
5173 (skip-chars-forward " \t\n\f"))))))
5174
5175 (defun verilog-indent-line-relative ()
5176 "Cheap version of indent line.
5177 Only look at a few lines to determine indent level."
5178 (interactive)
5179 (let ((indent-str)
5180 (sp (point)))
5181 (if (looking-at "^[ \t]*$")
5182 (cond ;- A blank line; No need to be too smart.
5183 ((bobp)
5184 (setq indent-str (list 'cpp 0)))
5185 ((verilog-continued-line)
5186 (let ((sp1 (point)))
5187 (if (verilog-continued-line)
5188 (progn
5189 (goto-char sp)
5190 (setq indent-str
5191 (list 'statement (verilog-current-indent-level))))
5192 (goto-char sp1)
5193 (setq indent-str (list 'block (verilog-current-indent-level)))))
5194 (goto-char sp))
5195 ((goto-char sp)
5196 (setq indent-str (verilog-calculate-indent))))
5197 (progn (skip-chars-forward " \t")
5198 (setq indent-str (verilog-calculate-indent))))
5199 (verilog-do-indent indent-str)))
5200
5201 (defun verilog-indent-line ()
5202 "Indent for special part of code."
5203 (verilog-do-indent (verilog-calculate-indent)))
5204
5205 (defun verilog-do-indent (indent-str)
5206 (let ((type (car indent-str))
5207 (ind (car (cdr indent-str))))
5208 (cond
5209 (; handle continued exp
5210 (eq type 'cexp)
5211 (let ((here (point)))
5212 (verilog-backward-syntactic-ws)
5213 (cond
5214 ((or
5215 (= (preceding-char) ?\,)
5216 (= (preceding-char) ?\])
5217 (save-excursion
5218 (verilog-beg-of-statement-1)
5219 (looking-at verilog-declaration-re)))
5220 (let* ( fst
5221 (val
5222 (save-excursion
5223 (backward-char 1)
5224 (verilog-beg-of-statement-1)
5225 (setq fst (point))
5226 (if (looking-at verilog-declaration-re)
5227 (progn ;; we have multiple words
5228 (goto-char (match-end 0))
5229 (skip-chars-forward " \t")
5230 (cond
5231 ((and verilog-indent-declaration-macros
5232 (= (following-char) ?\`))
5233 (progn
5234 (forward-char 1)
5235 (forward-word 1)
5236 (skip-chars-forward " \t")))
5237 ((= (following-char) ?\[)
5238 (progn
5239 (forward-char 1)
5240 (backward-up-list -1)
5241 (skip-chars-forward " \t"))))
5242 (current-column))
5243 (progn
5244 (goto-char fst)
5245 (+ (current-column) verilog-cexp-indent))))))
5246 (goto-char here)
5247 (indent-line-to val)))
5248 ((= (preceding-char) ?\) )
5249 (goto-char here)
5250 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
5251 (indent-line-to val)))
5252 (t
5253 (goto-char here)
5254 (let ((val))
5255 (verilog-beg-of-statement-1)
5256 (if (and (< (point) here)
5257 (verilog-re-search-forward "=[ \\t]*" here 'move))
5258 (setq val (current-column))
5259 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
5260 (goto-char here)
5261 (indent-line-to val))))))
5262
5263 (; handle inside parenthetical expressions
5264 (eq type 'cparenexp)
5265 (let ((val (save-excursion
5266 (backward-up-list 1)
5267 (forward-char 1)
5268 (skip-chars-forward " \t")
5269 (current-column))))
5270 (indent-line-to val)
5271 ))
5272
5273 (;-- Handle the ends
5274 (or
5275 (looking-at verilog-end-block-re )
5276 (verilog-at-close-constraint-p))
5277 (let ((val (if (eq type 'statement)
5278 (- ind verilog-indent-level)
5279 ind)))
5280 (indent-line-to val)))
5281
5282 (;-- Case -- maybe line 'em up
5283 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
5284 (progn
5285 (cond
5286 ((looking-at "\\<endcase\\>")
5287 (indent-line-to ind))
5288 (t
5289 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
5290 (indent-line-to val))))))
5291
5292 (;-- defun
5293 (and (eq type 'defun)
5294 (looking-at verilog-zero-indent-re))
5295 (indent-line-to 0))
5296
5297 (;-- declaration
5298 (and (or
5299 (eq type 'defun)
5300 (eq type 'block))
5301 (looking-at verilog-declaration-re))
5302 (verilog-indent-declaration ind))
5303
5304 (;-- Everything else
5305 t
5306 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
5307 (indent-line-to val))))
5308
5309 (if (looking-at "[ \t]+$")
5310 (skip-chars-forward " \t"))
5311 indent-str ; Return indent data
5312 ))
5313
5314 (defun verilog-current-indent-level ()
5315 "Return the indent-level of the current statement."
5316 (save-excursion
5317 (let (par-pos)
5318 (beginning-of-line)
5319 (setq par-pos (verilog-parenthesis-depth))
5320 (while par-pos
5321 (goto-char par-pos)
5322 (beginning-of-line)
5323 (setq par-pos (verilog-parenthesis-depth)))
5324 (skip-chars-forward " \t")
5325 (current-column))))
5326
5327 (defun verilog-case-indent-level ()
5328 "Return the indent-level of the current statement.
5329 Do not count named blocks or case-statements."
5330 (save-excursion
5331 (skip-chars-forward " \t")
5332 (cond
5333 ((looking-at verilog-named-block-re)
5334 (current-column))
5335 ((and (not (looking-at verilog-extended-case-re))
5336 (looking-at "^[^:;]+[ \t]*:"))
5337 (verilog-re-search-forward ":" nil t)
5338 (skip-chars-forward " \t")
5339 (current-column))
5340 (t
5341 (current-column)))))
5342
5343 (defun verilog-indent-comment ()
5344 "Indent current line as comment."
5345 (let* ((stcol
5346 (cond
5347 ((verilog-in-star-comment-p)
5348 (save-excursion
5349 (re-search-backward "/\\*" nil t)
5350 (1+(current-column))))
5351 (comment-column
5352 comment-column )
5353 (t
5354 (save-excursion
5355 (re-search-backward "//" nil t)
5356 (current-column))))))
5357 (indent-line-to stcol)
5358 stcol))
5359
5360 (defun verilog-more-comment ()
5361 "Make more comment lines like the previous."
5362 (let* ((star 0)
5363 (stcol
5364 (cond
5365 ((verilog-in-star-comment-p)
5366 (save-excursion
5367 (setq star 1)
5368 (re-search-backward "/\\*" nil t)
5369 (1+(current-column))))
5370 (comment-column
5371 comment-column )
5372 (t
5373 (save-excursion
5374 (re-search-backward "//" nil t)
5375 (current-column))))))
5376 (progn
5377 (indent-to stcol)
5378 (if (and star
5379 (save-excursion
5380 (forward-line -1)
5381 (skip-chars-forward " \t")
5382 (looking-at "\*")))
5383 (insert "* ")))))
5384
5385 (defun verilog-comment-indent (&optional arg)
5386 "Return the column number the line should be indented to.
5387 ARG is ignored, for `comment-indent-function' compatibility."
5388 (cond
5389 ((verilog-in-star-comment-p)
5390 (save-excursion
5391 (re-search-backward "/\\*" nil t)
5392 (1+(current-column))))
5393 ( comment-column
5394 comment-column )
5395 (t
5396 (save-excursion
5397 (re-search-backward "//" nil t)
5398 (current-column)))))
5399
5400 ;;
5401
5402 (defun verilog-pretty-declarations (&optional quiet)
5403 "Line up declarations around point.
5404 Be verbose about progress unless optional QUIET set."
5405 (interactive)
5406 (let* ((m1 (make-marker))
5407 (e (point))
5408 el
5409 r
5410 (here (point))
5411 ind
5412 start
5413 startpos
5414 end
5415 endpos
5416 base-ind
5417 )
5418 (save-excursion
5419 (if (progn
5420 ; (verilog-beg-of-statement-1)
5421 (beginning-of-line)
5422 (verilog-forward-syntactic-ws)
5423 (and (not (verilog-in-directive-p)) ;; could have `define input foo
5424 (looking-at verilog-declaration-re)))
5425 (progn
5426 (if (verilog-parenthesis-depth)
5427 ;; in an argument list or parameter block
5428 (setq el (backward-up-list -1)
5429 start (progn
5430 (goto-char e)
5431 (backward-up-list 1)
5432 (forward-line) ;; ignore ( input foo,
5433 (verilog-re-search-forward verilog-declaration-re el 'move)
5434 (goto-char (match-beginning 0))
5435 (skip-chars-backward " \t")
5436 (point))
5437 startpos (set-marker (make-marker) start)
5438 end (progn
5439 (goto-char start)
5440 (backward-up-list -1)
5441 (forward-char -1)
5442 (verilog-backward-syntactic-ws)
5443 (point))
5444 endpos (set-marker (make-marker) end)
5445 base-ind (progn
5446 (goto-char start)
5447 (verilog-do-indent (verilog-calculate-indent))
5448 (verilog-forward-ws&directives)
5449 (current-column))
5450 )
5451 ;; in a declaration block (not in argument list)
5452 (setq
5453 start (progn
5454 (verilog-beg-of-statement-1)
5455 (while (and (looking-at verilog-declaration-re)
5456 (not (bobp)))
5457 (skip-chars-backward " \t")
5458 (setq e (point))
5459 (beginning-of-line)
5460 (verilog-backward-syntactic-ws)
5461 (backward-char)
5462 (verilog-beg-of-statement-1))
5463 e)
5464 startpos (set-marker (make-marker) start)
5465 end (progn
5466 (goto-char here)
5467 (verilog-end-of-statement)
5468 (setq e (point)) ;Might be on last line
5469 (verilog-forward-syntactic-ws)
5470 (while (looking-at verilog-declaration-re)
5471 ;;(beginning-of-line)
5472 (verilog-end-of-statement)
5473 (setq e (point))
5474 (verilog-forward-syntactic-ws))
5475 e)
5476 endpos (set-marker (make-marker) end)
5477 base-ind (progn
5478 (goto-char start)
5479 (verilog-do-indent (verilog-calculate-indent))
5480 (verilog-forward-ws&directives)
5481 (current-column))))
5482 ;; OK, start and end are set
5483 (goto-char (marker-position startpos))
5484 (if (and (not quiet)
5485 (> (- end start) 100))
5486 (message "Lining up declarations..(please stand by)"))
5487 ;; Get the beginning of line indent first
5488 (while (progn (setq e (marker-position endpos))
5489 (< (point) e))
5490 (cond
5491 ((save-excursion (skip-chars-backward " \t")
5492 (bolp))
5493 (verilog-forward-ws&directives)
5494 (indent-line-to base-ind)
5495 (verilog-forward-ws&directives)
5496 (if (< (point) e)
5497 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
5498 (t
5499 (just-one-space)
5500 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
5501 ;;(forward-line)
5502 )
5503 ;; Now find biggest prefix
5504 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
5505 ;; Now indent each line.
5506 (goto-char (marker-position startpos))
5507 (while (progn (setq e (marker-position endpos))
5508 (setq r (- e (point)))
5509 (> r 0))
5510 (setq e (point))
5511 (unless quiet (message "%d" r))
5512 (verilog-indent-line)
5513 (verilog-forward-ws&directives)
5514 (cond
5515 ((or (and verilog-indent-declaration-macros
5516 (looking-at verilog-declaration-re-2-macro))
5517 (looking-at verilog-declaration-re-2-no-macro))
5518 (let ((p (match-end 0)))
5519 (set-marker m1 p)
5520 (if (verilog-re-search-forward "[[#`]" p 'move)
5521 (progn
5522 (forward-char -1)
5523 (just-one-space)
5524 (goto-char (marker-position m1))
5525 (just-one-space)
5526 (indent-to ind))
5527 (progn
5528 (just-one-space)
5529 (indent-to ind)))))
5530 ((verilog-continued-line-1 (marker-position startpos))
5531 (goto-char e)
5532 (indent-line-to ind))
5533 ((verilog-in-struct-p)
5534 ;; could have a declaration of a user defined item
5535 (goto-char e)
5536 (verilog-end-of-statement))
5537 (t ; Must be comment or white space
5538 (goto-char e)
5539 (verilog-forward-ws&directives)
5540 (forward-line -1)))
5541 (forward-line 1))
5542 (unless quiet (message "")))))))
5543
5544 (defun verilog-pretty-expr (&optional quiet myre)
5545 "Line up expressions around point, optionally QUIET with regexp MYRE."
5546 (interactive "sRegular Expression: ((<|:)?=) ")
5547 (save-excursion
5548 (if (or (eq myre nil)
5549 (string-equal myre ""))
5550 (setq myre "\\(<\\|:\\)?="))
5551 ;; want to match the first <= | := | =
5552 (setq myre (concat "\\(^.*?\\)\\(" myre "\\)"))
5553 (let ((rexp(concat "^\\s-*" verilog-complete-reg)))
5554 (beginning-of-line)
5555 (if (and (not (looking-at rexp ))
5556 (looking-at myre)
5557 (save-excursion
5558 (goto-char (match-beginning 2))
5559 (not (verilog-in-comment-or-string-p))))
5560 (let* ((here (point))
5561 (e) (r)
5562 (start
5563 (progn
5564 (beginning-of-line)
5565 (setq e (point))
5566 (verilog-backward-syntactic-ws)
5567 (beginning-of-line)
5568 (while (and (not (looking-at rexp ))
5569 (looking-at myre)
5570 (not (bobp))
5571 )
5572 (setq e (point))
5573 (verilog-backward-syntactic-ws)
5574 (beginning-of-line)
5575 ) ;Ack, need to grok `define
5576 e))
5577 (end
5578 (progn
5579 (goto-char here)
5580 (end-of-line)
5581 (setq e (point)) ;Might be on last line
5582 (verilog-forward-syntactic-ws)
5583 (beginning-of-line)
5584 (while (and
5585 (not (looking-at rexp ))
5586 (looking-at myre)
5587 (progn
5588 (end-of-line)
5589 (not (eq e (point)))))
5590 (setq e (point))
5591 (verilog-forward-syntactic-ws)
5592 (beginning-of-line)
5593 )
5594 e))
5595 (endpos (set-marker (make-marker) end))
5596 (ind)
5597 )
5598 (goto-char start)
5599 (verilog-do-indent (verilog-calculate-indent))
5600 (if (and (not quiet)
5601 (> (- end start) 100))
5602 (message "Lining up expressions..(please stand by)"))
5603
5604 ;; Set indent to minimum throughout region
5605 (while (< (point) (marker-position endpos))
5606 (beginning-of-line)
5607 (verilog-just-one-space myre)
5608 (end-of-line)
5609 (verilog-forward-syntactic-ws)
5610 )
5611
5612 ;; Now find biggest prefix
5613 (setq ind (verilog-get-lineup-indent-2 myre start endpos))
5614
5615 ;; Now indent each line.
5616 (goto-char start)
5617 (while (progn (setq e (marker-position endpos))
5618 (setq r (- e (point)))
5619 (> r 0))
5620 (setq e (point))
5621 (if (not quiet) (message "%d" r))
5622 (cond
5623 ((looking-at myre)
5624 (goto-char (match-beginning 2))
5625 (if (not (verilog-parenthesis-depth)) ;; ignore parenthesized exprs
5626 (if (eq (char-after) ?=)
5627 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
5628 (indent-to ind)
5629 )))
5630 ((verilog-continued-line-1 start)
5631 (goto-char e)
5632 (indent-line-to ind))
5633 (t ; Must be comment or white space
5634 (goto-char e)
5635 (verilog-forward-ws&directives)
5636 (forward-line -1))
5637 )
5638 (forward-line 1))
5639 (unless quiet (message ""))
5640 )))))
5641
5642 (defun verilog-just-one-space (myre)
5643 "Remove extra spaces around regular expression MYRE."
5644 (interactive)
5645 (if (and (not(looking-at verilog-complete-reg))
5646 (looking-at myre))
5647 (let ((p1 (match-end 1))
5648 (p2 (match-end 2)))
5649 (progn
5650 (goto-char p2)
5651 (if (looking-at "\\s-") (just-one-space))
5652 (goto-char p1)
5653 (forward-char -1)
5654 (if (looking-at "\\s-") (just-one-space))
5655 ))))
5656
5657 (defun verilog-indent-declaration (baseind)
5658 "Indent current lines as declaration.
5659 Line up the variable names based on previous declaration's indentation.
5660 BASEIND is the base indent to offset everything."
5661 (interactive)
5662 (let ((pos (point-marker))
5663 (lim (save-excursion
5664 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
5665 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
5666 (point)))
5667 (ind)
5668 (val)
5669 (m1 (make-marker)))
5670 (setq val
5671 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
5672 (indent-line-to val)
5673
5674 ;; Use previous declaration (in this module) as template.
5675 (if (or (eq 'all verilog-auto-lineup)
5676 (eq 'declarations verilog-auto-lineup))
5677 (if (verilog-re-search-backward
5678 (or (and verilog-indent-declaration-macros
5679 verilog-declaration-re-1-macro)
5680 verilog-declaration-re-1-no-macro) lim t)
5681 (progn
5682 (goto-char (match-end 0))
5683 (skip-chars-forward " \t")
5684 (setq ind (current-column))
5685 (goto-char pos)
5686 (setq val
5687 (+ baseind
5688 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
5689 (indent-line-to val)
5690 (if (and verilog-indent-declaration-macros
5691 (looking-at verilog-declaration-re-2-macro))
5692 (let ((p (match-end 0)))
5693 (set-marker m1 p)
5694 (if (verilog-re-search-forward "[[#`]" p 'move)
5695 (progn
5696 (forward-char -1)
5697 (just-one-space)
5698 (goto-char (marker-position m1))
5699 (just-one-space)
5700 (indent-to ind))
5701 (if (/= (current-column) ind)
5702 (progn
5703 (just-one-space)
5704 (indent-to ind)))))
5705 (if (looking-at verilog-declaration-re-2-no-macro)
5706 (let ((p (match-end 0)))
5707 (set-marker m1 p)
5708 (if (verilog-re-search-forward "[[`#]" p 'move)
5709 (progn
5710 (forward-char -1)
5711 (just-one-space)
5712 (goto-char (marker-position m1))
5713 (just-one-space)
5714 (indent-to ind))
5715 (if (/= (current-column) ind)
5716 (progn
5717 (just-one-space)
5718 (indent-to ind))))))))))
5719 (goto-char pos)))
5720
5721 (defun verilog-get-lineup-indent (b edpos)
5722 "Return the indent level that will line up several lines within the region.
5723 Region is defined by B and EDPOS."
5724 (save-excursion
5725 (let ((ind 0) e)
5726 (goto-char b)
5727 ;; Get rightmost position
5728 (while (progn (setq e (marker-position edpos))
5729 (< (point) e))
5730 (if (verilog-re-search-forward
5731 (or (and verilog-indent-declaration-macros
5732 verilog-declaration-re-1-macro)
5733 verilog-declaration-re-1-no-macro) e 'move)
5734 (progn
5735 (goto-char (match-end 0))
5736 (verilog-backward-syntactic-ws)
5737 (if (> (current-column) ind)
5738 (setq ind (current-column)))
5739 (goto-char (match-end 0)))))
5740 (if (> ind 0)
5741 (1+ ind)
5742 ;; No lineup-string found
5743 (goto-char b)
5744 (end-of-line)
5745 (verilog-backward-syntactic-ws)
5746 ;;(skip-chars-backward " \t")
5747 (1+ (current-column))))))
5748
5749 (defun verilog-get-lineup-indent-2 (myre b edpos)
5750 "Return the indent level that will line up several lines within the region."
5751 (save-excursion
5752 (let ((ind 0) e)
5753 (goto-char b)
5754 ;; Get rightmost position
5755 (while (progn (setq e (marker-position edpos))
5756 (< (point) e))
5757 (if (and (verilog-re-search-forward myre e 'move)
5758 (not (verilog-parenthesis-depth))) ;; skip parenthesized exprs
5759 (progn
5760 (goto-char (match-beginning 2))
5761 (verilog-backward-syntactic-ws)
5762 (if (> (current-column) ind)
5763 (setq ind (current-column)))
5764 (goto-char (match-end 0)))
5765 ))
5766 (if (> ind 0)
5767 (1+ ind)
5768 ;; No lineup-string found
5769 (goto-char b)
5770 (end-of-line)
5771 (skip-chars-backward " \t")
5772 (1+ (current-column))))))
5773
5774 (defun verilog-comment-depth (type val)
5775 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
5776 (save-excursion
5777 (let
5778 ((b (prog2
5779 (beginning-of-line)
5780 (point-marker)
5781 (end-of-line)))
5782 (e (point-marker)))
5783 (if (re-search-backward " /\\* \[#-\]# \[a-zA-Z\]+ \[0-9\]+ ## \\*/" b t)
5784 (progn
5785 (replace-match " /* -# ## */")
5786 (end-of-line))
5787 (progn
5788 (end-of-line)
5789 (insert " /* ## ## */"))))
5790 (backward-char 6)
5791 (insert
5792 (format "%s %d" type val))))
5793
5794 ;; \f
5795 ;;
5796 ;; Completion
5797 ;;
5798 (defvar verilog-str nil)
5799 (defvar verilog-all nil)
5800 (defvar verilog-pred nil)
5801 (defvar verilog-buffer-to-use nil)
5802 (defvar verilog-flag nil)
5803 (defvar verilog-toggle-completions nil
5804 "*True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
5805 Repeated use of \\[verilog-complete-word] will show you all of them.
5806 Normally, when there is more than one possible completion,
5807 it displays a list of all possible completions.")
5808
5809
5810 (defvar verilog-type-keywords
5811 '(
5812 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
5813 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
5814 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
5815 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
5816 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
5817 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
5818 )
5819 "*Keywords for types used when completing a word in a declaration or parmlist.
5820 \(integer, real, reg...)")
5821
5822 (defvar verilog-cpp-keywords
5823 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
5824 "endif")
5825 "*Keywords to complete when at first word of a line in declarative scope.
5826 \(initial, always, begin, assign...)
5827 The procedures and variables defined within the Verilog program
5828 will be completed at runtime and should not be added to this list.")
5829
5830 (defvar verilog-defun-keywords
5831 (append
5832 '(
5833 "always" "always_comb" "always_ff" "always_latch" "assign"
5834 "begin" "end" "generate" "endgenerate" "module" "endmodule"
5835 "specify" "endspecify" "function" "endfunction" "initial" "final"
5836 "task" "endtask" "primitive" "endprimitive"
5837 )
5838 verilog-type-keywords)
5839 "*Keywords to complete when at first word of a line in declarative scope.
5840 \(initial, always, begin, assign...)
5841 The procedures and variables defined within the Verilog program
5842 will be completed at runtime and should not be added to this list.")
5843
5844 (defvar verilog-block-keywords
5845 '(
5846 "begin" "break" "case" "continue" "else" "end" "endfunction"
5847 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
5848 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
5849 "while")
5850 "*Keywords to complete when at first word of a line in behavioral scope.
5851 \(begin, if, then, else, for, fork...)
5852 The procedures and variables defined within the Verilog program
5853 will be completed at runtime and should not be added to this list.")
5854
5855 (defvar verilog-tf-keywords
5856 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
5857 "*Keywords to complete when at first word of a line in a task or function.
5858 \(begin, if, then, else, for, fork.)
5859 The procedures and variables defined within the Verilog program
5860 will be completed at runtime and should not be added to this list.")
5861
5862 (defvar verilog-case-keywords
5863 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
5864 "*Keywords to complete when at first word of a line in case scope.
5865 \(begin, if, then, else, for, fork...)
5866 The procedures and variables defined within the Verilog program
5867 will be completed at runtime and should not be added to this list.")
5868
5869 (defvar verilog-separator-keywords
5870 '("else" "then" "begin")
5871 "*Keywords to complete when NOT standing at the first word of a statement.
5872 \(else, then, begin...)
5873 Variables and function names defined within the Verilog program
5874 will be completed at runtime and should not be added to this list.")
5875
5876 (defun verilog-string-diff (str1 str2)
5877 "Return index of first letter where STR1 and STR2 differs."
5878 (catch 'done
5879 (let ((diff 0))
5880 (while t
5881 (if (or (> (1+ diff) (length str1))
5882 (> (1+ diff) (length str2)))
5883 (throw 'done diff))
5884 (or (equal (aref str1 diff) (aref str2 diff))
5885 (throw 'done diff))
5886 (setq diff (1+ diff))))))
5887
5888 ;; Calculate all possible completions for functions if argument is `function',
5889 ;; completions for procedures if argument is `procedure' or both functions and
5890 ;; procedures otherwise.
5891
5892 (defun verilog-func-completion (type)
5893 "Build regular expression for module/task/function names.
5894 TYPE is 'module, 'tf for task or function, or t if unknown."
5895 (if (string= verilog-str "")
5896 (setq verilog-str "[a-zA-Z_]"))
5897 (let ((verilog-str (concat (cond
5898 ((eq type 'module) "\\<\\(module\\)\\s +")
5899 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
5900 (t "\\<\\(task\\|function\\|module\\)\\s +"))
5901 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
5902 match)
5903
5904 (if (not (looking-at verilog-defun-re))
5905 (verilog-re-search-backward verilog-defun-re nil t))
5906 (forward-char 1)
5907
5908 ;; Search through all reachable functions
5909 (goto-char (point-min))
5910 (while (verilog-re-search-forward verilog-str (point-max) t)
5911 (progn (setq match (buffer-substring (match-beginning 2)
5912 (match-end 2)))
5913 (if (or (null verilog-pred)
5914 (funcall verilog-pred match))
5915 (setq verilog-all (cons match verilog-all)))))
5916 (if (match-beginning 0)
5917 (goto-char (match-beginning 0)))))
5918
5919 (defun verilog-get-completion-decl (end)
5920 "Macro for searching through current declaration (var, type or const)
5921 for matches of `str' and adding the occurrence tp `all' through point END."
5922 (let ((re (or (and verilog-indent-declaration-macros
5923 verilog-declaration-re-2-macro)
5924 verilog-declaration-re-2-no-macro))
5925 decl-end match)
5926 ;; Traverse lines
5927 (while (and (< (point) end)
5928 (verilog-re-search-forward re end t))
5929 ;; Traverse current line
5930 (setq decl-end (save-excursion (verilog-declaration-end)))
5931 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
5932 (not (match-end 1)))
5933 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
5934 (if (string-match (concat "\\<" verilog-str) match)
5935 (if (or (null verilog-pred)
5936 (funcall verilog-pred match))
5937 (setq verilog-all (cons match verilog-all)))))
5938 (forward-line 1)))
5939 verilog-all)
5940
5941 (defun verilog-type-completion ()
5942 "Calculate all possible completions for types."
5943 (let ((start (point))
5944 goon)
5945 ;; Search for all reachable type declarations
5946 (while (or (verilog-beg-of-defun)
5947 (setq goon (not goon)))
5948 (save-excursion
5949 (if (and (< start (prog1 (save-excursion (verilog-end-of-defun)
5950 (point))
5951 (forward-char 1)))
5952 (verilog-re-search-forward
5953 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
5954 start t)
5955 (not (match-end 1)))
5956 ;; Check current type declaration
5957 (verilog-get-completion-decl start))))))
5958
5959 (defun verilog-var-completion ()
5960 "Calculate all possible completions for variables (or constants)."
5961 (let ((start (point)))
5962 ;; Search for all reachable var declarations
5963 (verilog-beg-of-defun)
5964 (save-excursion
5965 ;; Check var declarations
5966 (verilog-get-completion-decl start))))
5967
5968 (defun verilog-keyword-completion (keyword-list)
5969 "Give list of all possible completions of keywords in KEYWORD-LIST."
5970 (mapcar '(lambda (s)
5971 (if (string-match (concat "\\<" verilog-str) s)
5972 (if (or (null verilog-pred)
5973 (funcall verilog-pred s))
5974 (setq verilog-all (cons s verilog-all)))))
5975 keyword-list))
5976
5977
5978 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
5979 "Function passed to `completing-read', `try-completion' or `all-completions'.
5980 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
5981 must be a function to be called for every match to check if this should
5982 really be a match. If VERILOG-FLAG is t, the function returns a list of
5983 all possible completions. If VERILOG-FLAG is nil it returns a string,
5984 the longest possible completion, or t if VERILOG-STR is an exact match.
5985 If VERILOG-FLAG is 'lambda, the function returns t if VERILOG-STR is an
5986 exact match, nil otherwise."
5987 (save-excursion
5988 (let ((verilog-all nil))
5989 ;; Set buffer to use for searching labels. This should be set
5990 ;; within functions which use verilog-completions
5991 (set-buffer verilog-buffer-to-use)
5992
5993 ;; Determine what should be completed
5994 (let ((state (car (verilog-calculate-indent))))
5995 (cond ((eq state 'defun)
5996 (save-excursion (verilog-var-completion))
5997 (verilog-func-completion 'module)
5998 (verilog-keyword-completion verilog-defun-keywords))
5999
6000 ((eq state 'behavioral)
6001 (save-excursion (verilog-var-completion))
6002 (verilog-func-completion 'module)
6003 (verilog-keyword-completion verilog-defun-keywords))
6004
6005 ((eq state 'block)
6006 (save-excursion (verilog-var-completion))
6007 (verilog-func-completion 'tf)
6008 (verilog-keyword-completion verilog-block-keywords))
6009
6010 ((eq state 'case)
6011 (save-excursion (verilog-var-completion))
6012 (verilog-func-completion 'tf)
6013 (verilog-keyword-completion verilog-case-keywords))
6014
6015 ((eq state 'tf)
6016 (save-excursion (verilog-var-completion))
6017 (verilog-func-completion 'tf)
6018 (verilog-keyword-completion verilog-tf-keywords))
6019
6020 ((eq state 'cpp)
6021 (save-excursion (verilog-var-completion))
6022 (verilog-keyword-completion verilog-cpp-keywords))
6023
6024 ((eq state 'cparenexp)
6025 (save-excursion (verilog-var-completion)))
6026
6027 (t;--Anywhere else
6028 (save-excursion (verilog-var-completion))
6029 (verilog-func-completion 'both)
6030 (verilog-keyword-completion verilog-separator-keywords))))
6031
6032 ;; Now we have built a list of all matches. Give response to caller
6033 (verilog-completion-response))))
6034
6035 (defun verilog-completion-response ()
6036 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
6037 ;; This was not called by all-completions
6038 (if (null verilog-all)
6039 ;; Return nil if there was no matching label
6040 nil
6041 ;; Get longest string common in the labels
6042 (let* ((elm (cdr verilog-all))
6043 (match (car verilog-all))
6044 (min (length match))
6045 tmp)
6046 (if (string= match verilog-str)
6047 ;; Return t if first match was an exact match
6048 (setq match t)
6049 (while (not (null elm))
6050 ;; Find longest common string
6051 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
6052 (progn
6053 (setq min tmp)
6054 (setq match (substring match 0 min))))
6055 ;; Terminate with match=t if this is an exact match
6056 (if (string= (car elm) verilog-str)
6057 (progn
6058 (setq match t)
6059 (setq elm nil))
6060 (setq elm (cdr elm)))))
6061 ;; If this is a test just for exact match, return nil ot t
6062 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
6063 nil
6064 match))))
6065 ;; If flag is t, this was called by all-completions. Return
6066 ;; list of all possible completions
6067 (verilog-flag
6068 verilog-all)))
6069
6070 (defvar verilog-last-word-numb 0)
6071 (defvar verilog-last-word-shown nil)
6072 (defvar verilog-last-completions nil)
6073
6074 (defun verilog-complete-word ()
6075 "Complete word at current point.
6076 \(See also `verilog-toggle-completions', `verilog-type-keywords',
6077 and `verilog-separator-keywords'.)"
6078 (interactive)
6079 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
6080 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
6081 (verilog-str (buffer-substring b e))
6082 ;; The following variable is used in verilog-completion
6083 (verilog-buffer-to-use (current-buffer))
6084 (allcomp (if (and verilog-toggle-completions
6085 (string= verilog-last-word-shown verilog-str))
6086 verilog-last-completions
6087 (all-completions verilog-str 'verilog-completion)))
6088 (match (if verilog-toggle-completions
6089 "" (try-completion
6090 verilog-str (mapcar '(lambda (elm)
6091 (cons elm 0)) allcomp)))))
6092 ;; Delete old string
6093 (delete-region b e)
6094
6095 ;; Toggle-completions inserts whole labels
6096 (if verilog-toggle-completions
6097 (progn
6098 ;; Update entry number in list
6099 (setq verilog-last-completions allcomp
6100 verilog-last-word-numb
6101 (if (>= verilog-last-word-numb (1- (length allcomp)))
6102 0
6103 (1+ verilog-last-word-numb)))
6104 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
6105 ;; Display next match or same string if no match was found
6106 (if (not (null allcomp))
6107 (insert "" verilog-last-word-shown)
6108 (insert "" verilog-str)
6109 (message "(No match)")))
6110 ;; The other form of completion does not necessarily do that.
6111
6112 ;; Insert match if found, or the original string if no match
6113 (if (or (null match) (equal match 't))
6114 (progn (insert "" verilog-str)
6115 (message "(No match)"))
6116 (insert "" match))
6117 ;; Give message about current status of completion
6118 (cond ((equal match 't)
6119 (if (not (null (cdr allcomp)))
6120 (message "(Complete but not unique)")
6121 (message "(Sole completion)")))
6122 ;; Display buffer if the current completion didn't help
6123 ;; on completing the label.
6124 ((and (not (null (cdr allcomp))) (= (length verilog-str)
6125 (length match)))
6126 (with-output-to-temp-buffer "*Completions*"
6127 (display-completion-list allcomp))
6128 ;; Wait for a key press. Then delete *Completion* window
6129 (momentary-string-display "" (point))
6130 (delete-window (get-buffer-window (get-buffer "*Completions*")))
6131 )))))
6132
6133 (defun verilog-show-completions ()
6134 "Show all possible completions at current point."
6135 (interactive)
6136 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
6137 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
6138 (verilog-str (buffer-substring b e))
6139 ;; The following variable is used in verilog-completion
6140 (verilog-buffer-to-use (current-buffer))
6141 (allcomp (if (and verilog-toggle-completions
6142 (string= verilog-last-word-shown verilog-str))
6143 verilog-last-completions
6144 (all-completions verilog-str 'verilog-completion))))
6145 ;; Show possible completions in a temporary buffer.
6146 (with-output-to-temp-buffer "*Completions*"
6147 (display-completion-list allcomp))
6148 ;; Wait for a key press. Then delete *Completion* window
6149 (momentary-string-display "" (point))
6150 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
6151
6152
6153 (defun verilog-get-default-symbol ()
6154 "Return symbol around current point as a string."
6155 (save-excursion
6156 (buffer-substring (progn
6157 (skip-chars-backward " \t")
6158 (skip-chars-backward "a-zA-Z0-9_")
6159 (point))
6160 (progn
6161 (skip-chars-forward "a-zA-Z0-9_")
6162 (point)))))
6163
6164 (defun verilog-build-defun-re (str &optional arg)
6165 "Return function/task/module starting with STR as regular expression.
6166 With optional second ARG non-nil, STR is the complete name of the instruction."
6167 (if arg
6168 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
6169 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
6170
6171 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
6172 "Function passed to `completing-read', `try-completion' or `all-completions'.
6173 Returns a completion on any function name based on VERILOG-STR prefix. If
6174 VERILOG-PRED is non-nil, it must be a function to be called for every match
6175 to check if this should really be a match. If VERILOG-FLAG is t, the
6176 function returns a list of all possible completions. If it is nil it
6177 returns a string, the longest possible completion, or t if VERILOG-STR is
6178 an exact match. If VERILOG-FLAG is 'lambda, the function returns t if
6179 VERILOG-STR is an exact match, nil otherwise."
6180 (save-excursion
6181 (let ((verilog-all nil)
6182 match)
6183
6184 ;; Set buffer to use for searching labels. This should be set
6185 ;; within functions which use verilog-completions
6186 (set-buffer verilog-buffer-to-use)
6187
6188 (let ((verilog-str verilog-str))
6189 ;; Build regular expression for functions
6190 (if (string= verilog-str "")
6191 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
6192 (setq verilog-str (verilog-build-defun-re verilog-str)))
6193 (goto-char (point-min))
6194
6195 ;; Build a list of all possible completions
6196 (while (verilog-re-search-forward verilog-str nil t)
6197 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
6198 (if (or (null verilog-pred)
6199 (funcall verilog-pred match))
6200 (setq verilog-all (cons match verilog-all)))))
6201
6202 ;; Now we have built a list of all matches. Give response to caller
6203 (verilog-completion-response))))
6204
6205 (defun verilog-goto-defun ()
6206 "Move to specified Verilog module/task/function.
6207 The default is a name found in the buffer around point.
6208 If search fails, other files are checked based on
6209 `verilog-library-flags'."
6210 (interactive)
6211 (let* ((default (verilog-get-default-symbol))
6212 ;; The following variable is used in verilog-comp-function
6213 (verilog-buffer-to-use (current-buffer))
6214 (label (if (not (string= default ""))
6215 ;; Do completion with default
6216 (completing-read (concat "Goto-Label: (default "
6217 default ") ")
6218 'verilog-comp-defun nil nil "")
6219 ;; There is no default value. Complete without it
6220 (completing-read "Goto-Label: "
6221 'verilog-comp-defun nil nil "")))
6222 pt)
6223 ;; Make sure library paths are correct, in case need to resolve module
6224 (verilog-auto-reeval-locals)
6225 (verilog-getopt-flags)
6226 ;; If there was no response on prompt, use default value
6227 (if (string= label "")
6228 (setq label default))
6229 ;; Goto right place in buffer if label is not an empty string
6230 (or (string= label "")
6231 (progn
6232 (save-excursion
6233 (goto-char (point-min))
6234 (setq pt
6235 (re-search-forward (verilog-build-defun-re label t) nil t)))
6236 (when pt
6237 (goto-char pt)
6238 (beginning-of-line))
6239 pt)
6240 (verilog-goto-defun-file label))))
6241
6242 ;; Eliminate compile warning
6243 (defvar occur-pos-list)
6244
6245 (defun verilog-showscopes ()
6246 "List all scopes in this module."
6247 (interactive)
6248 (let ((buffer (current-buffer))
6249 (linenum 1)
6250 (nlines 0)
6251 (first 1)
6252 (prevpos (point-min))
6253 (final-context-start (make-marker))
6254 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
6255 (with-output-to-temp-buffer "*Occur*"
6256 (save-excursion
6257 (message (format "Searching for %s ..." regexp))
6258 ;; Find next match, but give up if prev match was at end of buffer.
6259 (while (and (not (= prevpos (point-max)))
6260 (verilog-re-search-forward regexp nil t))
6261 (goto-char (match-beginning 0))
6262 (beginning-of-line)
6263 (save-match-data
6264 (setq linenum (+ linenum (count-lines prevpos (point)))))
6265 (setq prevpos (point))
6266 (goto-char (match-end 0))
6267 (let* ((start (save-excursion
6268 (goto-char (match-beginning 0))
6269 (forward-line (if (< nlines 0) nlines (- nlines)))
6270 (point)))
6271 (end (save-excursion
6272 (goto-char (match-end 0))
6273 (if (> nlines 0)
6274 (forward-line (1+ nlines))
6275 (forward-line 1))
6276 (point)))
6277 (tag (format "%3d" linenum))
6278 (empty (make-string (length tag) ?\ ))
6279 tem)
6280 (save-excursion
6281 (setq tem (make-marker))
6282 (set-marker tem (point))
6283 (set-buffer standard-output)
6284 (setq occur-pos-list (cons tem occur-pos-list))
6285 (or first (zerop nlines)
6286 (insert "--------\n"))
6287 (setq first nil)
6288 (insert-buffer-substring buffer start end)
6289 (backward-char (- end start))
6290 (setq tem (if (< nlines 0) (- nlines) nlines))
6291 (while (> tem 0)
6292 (insert empty ?:)
6293 (forward-line 1)
6294 (setq tem (1- tem)))
6295 (let ((this-linenum linenum))
6296 (set-marker final-context-start
6297 (+ (point) (- (match-end 0) (match-beginning 0))))
6298 (while (< (point) final-context-start)
6299 (if (null tag)
6300 (setq tag (format "%3d" this-linenum)))
6301 (insert tag ?:)))))))
6302 (set-buffer-modified-p nil))))
6303
6304
6305 ;; Highlight helper functions
6306 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
6307 (defun verilog-within-translate-off ()
6308 "Return point if within translate-off region, else nil."
6309 (and (save-excursion
6310 (re-search-backward
6311 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
6312 nil t))
6313 (equal "off" (match-string 2))
6314 (point)))
6315
6316 (defun verilog-start-translate-off (limit)
6317 "Return point before translate-off directive if before LIMIT, else nil."
6318 (when (re-search-forward
6319 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
6320 limit t)
6321 (match-beginning 0)))
6322
6323 (defun verilog-back-to-start-translate-off (limit)
6324 "Return point before translate-off directive if before LIMIT, else nil."
6325 (when (re-search-backward
6326 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
6327 limit t)
6328 (match-beginning 0)))
6329
6330 (defun verilog-end-translate-off (limit)
6331 "Return point after translate-on directive if before LIMIT, else nil."
6332
6333 (re-search-forward (concat
6334 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
6335
6336 (defun verilog-match-translate-off (limit)
6337 "Match a translate-off block, setting `match-data' and returning t, else nil.
6338 Bound search by LIMIT."
6339 (when (< (point) limit)
6340 (let ((start (or (verilog-within-translate-off)
6341 (verilog-start-translate-off limit)))
6342 (case-fold-search t))
6343 (when start
6344 (let ((end (or (verilog-end-translate-off limit) limit)))
6345 (set-match-data (list start end))
6346 (goto-char end))))))
6347
6348 (defun verilog-font-lock-match-item (limit)
6349 "Match, and move over, any declaration item after point.
6350 Bound search by LIMIT. Adapted from
6351 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
6352 (condition-case nil
6353 (save-restriction
6354 (narrow-to-region (point-min) limit)
6355 ;; match item
6356 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
6357 (save-match-data
6358 (goto-char (match-end 1))
6359 ;; move to next item
6360 (if (looking-at "\\(\\s-*,\\)")
6361 (goto-char (match-end 1))
6362 (end-of-line) t))))
6363 (error nil)))
6364
6365
6366 ;; Added by Subbu Meiyappan for Header
6367
6368 (defun verilog-header ()
6369 "Insert a standard Verilog file header.
6370 See also `verilog-sk-header' for an alternative format."
6371 (interactive)
6372 (let ((start (point)))
6373 (insert "\
6374 //-----------------------------------------------------------------------------
6375 // Title : <title>
6376 // Project : <project>
6377 //-----------------------------------------------------------------------------
6378 // File : <filename>
6379 // Author : <author>
6380 // Created : <credate>
6381 // Last modified : <moddate>
6382 //-----------------------------------------------------------------------------
6383 // Description :
6384 // <description>
6385 //-----------------------------------------------------------------------------
6386 // Copyright (c) <copydate> by <company> This model is the confidential and
6387 // proprietary property of <company> and the possession or use of this
6388 // file requires a written license from <company>.
6389 //------------------------------------------------------------------------------
6390 // Modification history :
6391 // <modhist>
6392 //-----------------------------------------------------------------------------
6393
6394 ")
6395 (goto-char start)
6396 (search-forward "<filename>")
6397 (replace-match (buffer-name) t t)
6398 (search-forward "<author>") (replace-match "" t t)
6399 (insert (user-full-name))
6400 (insert " <" (user-login-name) "@" (system-name) ">")
6401 (search-forward "<credate>") (replace-match "" t t)
6402 (verilog-insert-date)
6403 (search-forward "<moddate>") (replace-match "" t t)
6404 (verilog-insert-date)
6405 (search-forward "<copydate>") (replace-match "" t t)
6406 (verilog-insert-year)
6407 (search-forward "<modhist>") (replace-match "" t t)
6408 (verilog-insert-date)
6409 (insert " : created")
6410 (goto-char start)
6411 (let (string)
6412 (setq string (read-string "title: "))
6413 (search-forward "<title>")
6414 (replace-match string t t)
6415 (setq string (read-string "project: " verilog-project))
6416 (setq verilog-project string)
6417 (search-forward "<project>")
6418 (replace-match string t t)
6419 (setq string (read-string "Company: " verilog-company))
6420 (setq verilog-company string)
6421 (search-forward "<company>")
6422 (replace-match string t t)
6423 (search-forward "<company>")
6424 (replace-match string t t)
6425 (search-forward "<company>")
6426 (replace-match string t t)
6427 (search-backward "<description>")
6428 (replace-match "" t t))))
6429
6430 ;; verilog-header Uses the verilog-insert-date function
6431
6432 (defun verilog-insert-date ()
6433 "Insert date from the system."
6434 (interactive)
6435 (if verilog-date-scientific-format
6436 (insert (format-time-string "%Y/%m/%d"))
6437 (insert (format-time-string "%d.%m.%Y"))))
6438
6439 (defun verilog-insert-year ()
6440 "Insert year from the system."
6441 (interactive)
6442 (insert (format-time-string "%Y")))
6443
6444 \f
6445 ;;
6446 ;; Signal list parsing
6447 ;;
6448
6449 ;; Elements of a signal list
6450 (defsubst verilog-sig-name (sig)
6451 (car sig))
6452 (defsubst verilog-sig-bits (sig)
6453 (nth 1 sig))
6454 (defsubst verilog-sig-comment (sig)
6455 (nth 2 sig))
6456 (defsubst verilog-sig-memory (sig)
6457 (nth 3 sig))
6458 (defsubst verilog-sig-enum (sig)
6459 (nth 4 sig))
6460 (defsubst verilog-sig-signed (sig)
6461 (nth 5 sig))
6462 (defsubst verilog-sig-type (sig)
6463 (nth 6 sig))
6464 (defsubst verilog-sig-multidim (sig)
6465 (nth 7 sig))
6466 (defsubst verilog-sig-multidim-string (sig)
6467 (if (verilog-sig-multidim sig)
6468 (let ((str "") (args (verilog-sig-multidim sig)))
6469 (while args
6470 (setq str (concat str (car args)))
6471 (setq args (cdr args)))
6472 str)))
6473 (defsubst verilog-sig-modport (sig)
6474 (nth 8 sig))
6475 (defsubst verilog-sig-width (sig)
6476 (verilog-make-width-expression (verilog-sig-bits sig)))
6477
6478 (defsubst verilog-alw-get-inputs (sigs)
6479 (nth 2 sigs))
6480 (defsubst verilog-alw-get-outputs (sigs)
6481 (nth 0 sigs))
6482 (defsubst verilog-alw-get-uses-delayed (sigs)
6483 (nth 3 sigs))
6484
6485 (defun verilog-signals-not-in (in-list not-list)
6486 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
6487 Also remove any duplicates in IN-LIST.
6488 Signals must be in standard (base vector) form."
6489 (let (out-list)
6490 (while in-list
6491 (if (not (or (assoc (car (car in-list)) not-list)
6492 (assoc (car (car in-list)) out-list)))
6493 (setq out-list (cons (car in-list) out-list)))
6494 (setq in-list (cdr in-list)))
6495 (nreverse out-list)))
6496 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
6497
6498 (defun verilog-signals-in (in-list other-list)
6499 "Return list of signals in IN-LIST that are also in OTHER-LIST.
6500 Signals must be in standard (base vector) form."
6501 (let (out-list)
6502 (while in-list
6503 (if (assoc (car (car in-list)) other-list)
6504 (setq out-list (cons (car in-list) out-list)))
6505 (setq in-list (cdr in-list)))
6506 (nreverse out-list)))
6507 ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
6508
6509 (defun verilog-signals-memory (in-list)
6510 "Return list of signals in IN-LIST that are memoried (multidimensional)."
6511 (let (out-list)
6512 (while in-list
6513 (if (nth 3 (car in-list))
6514 (setq out-list (cons (car in-list) out-list)))
6515 (setq in-list (cdr in-list)))
6516 out-list))
6517 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
6518
6519 (defun verilog-signals-sort-compare (a b)
6520 "Compare signal A and B for sorting."
6521 (string< (car a) (car b)))
6522
6523 (defun verilog-signals-not-params (in-list)
6524 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
6525 (let (out-list)
6526 (while in-list
6527 (unless (boundp (intern (concat "vh-" (car (car in-list)))))
6528 (setq out-list (cons (car in-list) out-list)))
6529 (setq in-list (cdr in-list)))
6530 (nreverse out-list)))
6531
6532 (defun verilog-signals-combine-bus (in-list)
6533 "Return a list of signals in IN-LIST, with busses combined.
6534 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
6535 (let (combo buswarn
6536 out-list
6537 sig highbit lowbit ; Temp information about current signal
6538 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
6539 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
6540 sv-modport
6541 bus)
6542 ;; Shove signals so duplicated signals will be adjacent
6543 (setq in-list (sort in-list `verilog-signals-sort-compare))
6544 (while in-list
6545 (setq sig (car in-list))
6546 ;; No current signal; form from existing details
6547 (unless sv-name
6548 (setq sv-name (verilog-sig-name sig)
6549 sv-highbit nil
6550 sv-busstring nil
6551 sv-comment (verilog-sig-comment sig)
6552 sv-memory (verilog-sig-memory sig)
6553 sv-enum (verilog-sig-enum sig)
6554 sv-signed (verilog-sig-signed sig)
6555 sv-type (verilog-sig-type sig)
6556 sv-multidim (verilog-sig-multidim sig)
6557 sv-modport (verilog-sig-modport sig)
6558 combo ""
6559 buswarn ""))
6560 ;; Extract bus details
6561 (setq bus (verilog-sig-bits sig))
6562 (cond ((and bus
6563 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
6564 (setq highbit (string-to-number (match-string 1 bus))
6565 lowbit (string-to-number
6566 (match-string 2 bus))))
6567 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
6568 (setq highbit (string-to-number (match-string 1 bus))
6569 lowbit highbit))))
6570 ;; Combine bits in bus
6571 (if sv-highbit
6572 (setq sv-highbit (max highbit sv-highbit)
6573 sv-lowbit (min lowbit sv-lowbit))
6574 (setq sv-highbit highbit
6575 sv-lowbit lowbit)))
6576 (bus
6577 ;; String, probably something like `preproc:0
6578 (setq sv-busstring bus)))
6579 ;; Peek ahead to next signal
6580 (setq in-list (cdr in-list))
6581 (setq sig (car in-list))
6582 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
6583 ;; Combine with this signal
6584 (when (and sv-busstring
6585 (not (equal sv-busstring (verilog-sig-bits sig))))
6586 (when nil ;; Debugging
6587 (message (concat "Warning, can't merge into single bus "
6588 sv-name bus
6589 ", the AUTOs may be wrong")))
6590 (setq buswarn ", Couldn't Merge"))
6591 (if (verilog-sig-comment sig) (setq combo ", ..."))
6592 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
6593 sv-enum (or sv-enum (verilog-sig-enum sig))
6594 sv-signed (or sv-signed (verilog-sig-signed sig))
6595 sv-type (or sv-type (verilog-sig-type sig))
6596 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
6597 sv-modport (or sv-modport (verilog-sig-modport sig))))
6598 ;; Doesn't match next signal, add to queue, zero in prep for next
6599 ;; Note sig may also be nil for the last signal in the list
6600 (t
6601 (setq out-list
6602 (cons
6603 (list sv-name
6604 (or sv-busstring
6605 (if sv-highbit
6606 (concat "[" (int-to-string sv-highbit) ":"
6607 (int-to-string sv-lowbit) "]")))
6608 (concat sv-comment combo buswarn)
6609 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
6610 out-list)
6611 sv-name nil))))
6612 ;;
6613 out-list))
6614
6615 (defun verilog-sig-tieoff (sig &optional no-width)
6616 "Return tieoff expression for given SIG, with appropriate width.
6617 Ignore width if optional NO-WIDTH is set."
6618 (let* ((width (if no-width nil (verilog-sig-width sig))))
6619 (concat
6620 (if (and verilog-active-low-regexp
6621 (string-match verilog-active-low-regexp (verilog-sig-name sig)))
6622 "~" "")
6623 (cond ((not width)
6624 "0")
6625 ((string-match "^[0-9]+$" width)
6626 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
6627 (t
6628 (concat "{" width "{1'b0}}"))))))
6629
6630 ;;
6631 ;; Port/Wire/Etc Reading
6632 ;;
6633
6634 (defun verilog-read-inst-backward-name ()
6635 "Internal. Move point back to beginning of inst-name."
6636 (verilog-backward-open-paren)
6637 (let (done)
6638 (while (not done)
6639 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil) ; ] isn't word boundary
6640 (cond ((looking-at ")")
6641 (verilog-backward-open-paren))
6642 (t (setq done t)))))
6643 (while (looking-at "\\]")
6644 (verilog-backward-open-bracket)
6645 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil))
6646 (skip-chars-backward "a-zA-Z0-9`_$"))
6647
6648 (defun verilog-read-inst-module ()
6649 "Return module_name when point is inside instantiation."
6650 (save-excursion
6651 (verilog-read-inst-backward-name)
6652 ;; Skip over instantiation name
6653 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
6654 ;; Check for parameterized instantiations
6655 (when (looking-at ")")
6656 (verilog-backward-open-paren)
6657 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
6658 (skip-chars-backward "a-zA-Z0-9'_$")
6659 (looking-at "[a-zA-Z0-9`_\$]+")
6660 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6661 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
6662
6663 (defun verilog-read-inst-name ()
6664 "Return instance_name when point is inside instantiation."
6665 (save-excursion
6666 (verilog-read-inst-backward-name)
6667 (looking-at "[a-zA-Z0-9`_\$]+")
6668 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6669 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
6670
6671 (defun verilog-read-module-name ()
6672 "Return module name when after its ( or ;."
6673 (save-excursion
6674 (re-search-backward "[(;]")
6675 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil)
6676 (skip-chars-backward "a-zA-Z0-9`_$")
6677 (looking-at "[a-zA-Z0-9`_\$]+")
6678 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
6679 (verilog-symbol-detick
6680 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
6681
6682 (defun verilog-read-inst-param-value ()
6683 "Return list of parameters and values when point is inside instantiation."
6684 (save-excursion
6685 (verilog-read-inst-backward-name)
6686 ;; Skip over instantiation name
6687 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
6688 ;; If there are parameterized instantiations
6689 (when (looking-at ")")
6690 (let ((end-pt (point))
6691 params
6692 param-name paren-beg-pt param-value)
6693 (verilog-backward-open-paren)
6694 (while (verilog-re-search-forward-quick "\\." end-pt t)
6695 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_\$]\\)" nil nil)
6696 (skip-chars-backward "a-zA-Z0-9'_$")
6697 (looking-at "[a-zA-Z0-9`_\$]+")
6698 (setq param-name (buffer-substring-no-properties
6699 (match-beginning 0) (match-end 0)))
6700 (verilog-re-search-forward-quick "(" nil nil)
6701 (setq paren-beg-pt (point))
6702 (verilog-forward-close-paren)
6703 (setq param-value (verilog-string-remove-spaces
6704 (buffer-substring-no-properties
6705 paren-beg-pt (1- (point)))))
6706 (setq params (cons (list param-name param-value) params)))
6707 params))))
6708
6709 (defun verilog-read-auto-params (num-param &optional max-param)
6710 "Return parameter list inside auto.
6711 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
6712 (let ((olist))
6713 (save-excursion
6714 ;; /*AUTOPUNT("parameter", "parameter")*/
6715 (search-backward "(")
6716 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
6717 (setq olist (cons (match-string 1) olist))
6718 (goto-char (match-end 0))))
6719 (or (eq nil num-param)
6720 (<= num-param (length olist))
6721 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
6722 (if (eq max-param nil) (setq max-param num-param))
6723 (or (eq nil max-param)
6724 (>= max-param (length olist))
6725 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
6726 (nreverse olist)))
6727
6728 (defun verilog-read-decls ()
6729 "Compute signal declaration information for the current module at point.
6730 Return a array of [outputs inouts inputs wire reg assign const]."
6731 (let ((end-mod-point (or (verilog-get-end-of-defun t) (point-max)))
6732 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
6733 sigs-in sigs-out sigs-inout sigs-wire sigs-reg sigs-assign sigs-const
6734 sigs-gparam sigs-intf
6735 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
6736 modport)
6737 (save-excursion
6738 (verilog-beg-of-defun)
6739 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
6740 (while (< (point) end-mod-point)
6741 ;;(if dbg (setq dbg (cons (format "Pt %s Vec %s Kwd'%s'\n" (point) vec keywd) dbg)))
6742 (cond
6743 ((looking-at "//")
6744 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
6745 (setq enum (match-string 1)))
6746 (search-forward "\n"))
6747 ((looking-at "/\\*")
6748 (forward-char 2)
6749 (if (looking-at "[^*]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
6750 (setq enum (match-string 1)))
6751 (or (search-forward "*/")
6752 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
6753 ((looking-at "(\\*")
6754 (forward-char 2)
6755 (or (looking-at "\\s-*)") ; It's an "always @ (*)"
6756 (search-forward "*)")
6757 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
6758 ((eq ?\" (following-char))
6759 (or (re-search-forward "[^\\]\"" nil t) ;; don't forward-char first, since we look for a non backslash first
6760 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
6761 ((eq ?\; (following-char))
6762 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
6763 v2kargs-ok nil)
6764 (forward-char 1))
6765 ((eq ?= (following-char))
6766 (setq rvalue t newsig nil)
6767 (forward-char 1))
6768 ((and (eq ?, (following-char))
6769 (eq paren sig-paren))
6770 (setq rvalue nil)
6771 (forward-char 1))
6772 ;; ,'s can occur inside {} & funcs
6773 ((looking-at "[{(]")
6774 (setq paren (1+ paren))
6775 (forward-char 1))
6776 ((looking-at "[})]")
6777 (setq paren (1- paren))
6778 (forward-char 1)
6779 (when (< paren sig-paren)
6780 (setq expect-signal nil))) ; ) that ends variables inside v2k arg list
6781 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
6782 (goto-char (match-end 0))
6783 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
6784 (setcar (cdr (cdr (cdr newsig))) (match-string 1)))
6785 (vec ;; Multidimensional
6786 (setq multidim (cons vec multidim))
6787 (setq vec (verilog-string-replace-matches
6788 "\\s-+" "" nil nil (match-string 1))))
6789 (t ;; Bit width
6790 (setq vec (verilog-string-replace-matches
6791 "\\s-+" "" nil nil (match-string 1))))))
6792 ;; Normal or escaped identifier -- note we remember the \ if escaped
6793 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
6794 (goto-char (match-end 0))
6795 (setq keywd (match-string 1))
6796 (when (string-match "^\\\\" (match-string 1))
6797 (setq keywd (concat keywd " "))) ;; Escaped ID needs space at end
6798 ;; Add any :: package names to same identifier
6799 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
6800 (goto-char (match-end 0))
6801 (setq keywd (concat keywd "::" (match-string 1)))
6802 (when (string-match "^\\\\" (match-string 1))
6803 (setq keywd (concat keywd " ")))) ;; Escaped ID needs space at end
6804 (cond ((equal keywd "input")
6805 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
6806 expect-signal 'sigs-in io t modport nil))
6807 ((equal keywd "output")
6808 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
6809 expect-signal 'sigs-out io t modport nil))
6810 ((equal keywd "inout")
6811 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed nil multidim nil sig-paren paren
6812 expect-signal 'sigs-inout io t modport nil))
6813 ((equal keywd "parameter")
6814 (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6815 expect-signal 'sigs-gparam io t modport nil))
6816 ((member keywd '("wire" "tri" "tri0" "tri1" "triand" "trior" "wand" "wor"))
6817 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6818 expect-signal 'sigs-wire modport nil)))
6819 ((member keywd '("reg" "trireg"
6820 "byte" "shortint" "int" "longint" "integer" "time"
6821 "bit" "logic"))
6822 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6823 expect-signal 'sigs-reg modport nil)))
6824 ((equal keywd "assign")
6825 (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6826 expect-signal 'sigs-assign modport nil))
6827 ((member keywd '("supply0" "supply1" "supply"
6828 "localparam" "genvar"))
6829 (unless io (setq vec nil enum nil rvalue nil signed nil typedefed nil multidim nil sig-paren paren
6830 expect-signal 'sigs-const modport nil)))
6831 ((equal keywd "signed")
6832 (setq signed "signed"))
6833 ((member keywd '("class" "clocking" "covergroup" "function"
6834 "property" "randsequence" "sequence" "task"))
6835 (setq functask (1+ functask)))
6836 ((member keywd '("endclass" "endclocking" "endgroup" "endfunction"
6837 "endproperty" "endsequence" "endtask"))
6838 (setq functask (1- functask)))
6839 ;; Ifdef? Ignore name of define
6840 ((member keywd '("`ifdef" "`ifndef"))
6841 (setq rvalue t))
6842 ;; Type?
6843 ((verilog-typedef-name-p keywd)
6844 (setq typedefed keywd))
6845 ;; Interface with optional modport in v2k arglist?
6846 ;; Skip over parsing modport, and take the interface name as the type
6847 ((and v2kargs-ok
6848 (eq paren 1)
6849 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z0-9`_$]+\\)\\|\\)\\s-*[a-zA-Z0-9`_$]+"))
6850 (when (match-end 2) (goto-char (match-end 2)))
6851 (setq vec nil enum nil rvalue nil newsig nil signed nil typedefed keywd multidim nil sig-paren paren
6852 expect-signal 'sigs-intf io t modport (match-string 2)))
6853 ;; New signal, maybe?
6854 ((and expect-signal
6855 (eq functask 0)
6856 (not rvalue)
6857 (not (member keywd verilog-keywords)))
6858 ;; Add new signal to expect-signal's variable
6859 (setq newsig (list keywd vec nil nil enum signed typedefed multidim modport))
6860 (set expect-signal (cons newsig
6861 (symbol-value expect-signal))))))
6862 (t
6863 (forward-char 1)))
6864 (skip-syntax-forward " "))
6865 ;; Return arguments
6866 (vector (nreverse sigs-out)
6867 (nreverse sigs-inout)
6868 (nreverse sigs-in)
6869 (nreverse sigs-wire)
6870 (nreverse sigs-reg)
6871 (nreverse sigs-assign)
6872 (nreverse sigs-const)
6873 (nreverse sigs-gparam)
6874 (nreverse sigs-intf)))))
6875
6876 (eval-when-compile
6877 ;; Prevent compile warnings; these are let's, not globals
6878 ;; Do not remove the eval-when-compile
6879 ;; - we want a error when we are debugging this code if they are refed.
6880 (defvar sigs-in)
6881 (defvar sigs-inout)
6882 (defvar sigs-out)
6883 (defvar sigs-intf))
6884
6885
6886 (defsubst verilog-modi-get-decls (modi)
6887 (verilog-modi-cache-results modi 'verilog-read-decls))
6888
6889 (defsubst verilog-modi-get-sub-decls (modi)
6890 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
6891
6892
6893 ;; Signal reading for given module
6894 ;; Note these all take modi's - as returned from the
6895 ;; verilog-modi-current function.
6896 (defsubst verilog-decls-get-outputs (decls)
6897 (aref decls 0))
6898 (defsubst verilog-decls-get-inouts (decls)
6899 (aref decls 1))
6900 (defsubst verilog-decls-get-inputs (decls)
6901 (aref decls 2))
6902 (defsubst verilog-decls-get-wires (decls)
6903 (aref decls 3))
6904 (defsubst verilog-decls-get-regs (decls)
6905 (aref decls 4))
6906 (defsubst verilog-decls-get-assigns (decls)
6907 (aref decls 5))
6908 (defsubst verilog-decls-get-consts (decls)
6909 (aref decls 6))
6910 (defsubst verilog-decls-get-gparams (decls)
6911 (aref decls 7))
6912 (defsubst verilog-decls-get-interfaces (decls)
6913 (aref decls 8))
6914 (defsubst verilog-subdecls-get-outputs (subdecls)
6915 (aref subdecls 0))
6916 (defsubst verilog-subdecls-get-inouts (subdecls)
6917 (aref subdecls 1))
6918 (defsubst verilog-subdecls-get-inputs (subdecls)
6919 (aref subdecls 2))
6920 (defsubst verilog-subdecls-get-interfaces (subdecls)
6921 (aref subdecls 3))
6922
6923
6924 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
6925 "For `verilog-read-sub-decls-line', add a signal."
6926 (let (portdata)
6927 (when sig
6928 (setq port (verilog-symbol-detick-denumber port))
6929 (setq sig (verilog-symbol-detick-denumber sig))
6930 (if sig (setq sig (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil sig)))
6931 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
6932 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
6933 (unless (or (not sig)
6934 (equal sig "")) ;; Ignore .foo(1'b1) assignments
6935 (cond ((setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
6936 (setq sigs-inout (cons (list sig vec (concat "To/From " comment) nil nil
6937 (verilog-sig-signed portdata)
6938 (verilog-sig-type portdata)
6939 multidim)
6940 sigs-inout)))
6941 ((setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
6942 (setq sigs-out (cons (list sig vec (concat "From " comment) nil nil
6943 (verilog-sig-signed portdata)
6944 (verilog-sig-type portdata)
6945 multidim)
6946 sigs-out)))
6947 ((setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
6948 (setq sigs-in (cons (list sig vec (concat "To " comment) nil nil
6949 (verilog-sig-signed portdata)
6950 (verilog-sig-type portdata)
6951 multidim)
6952 sigs-in)))
6953 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
6954 (setq sigs-intf (cons (list sig vec (concat "To/From " comment) nil nil
6955 (verilog-sig-signed portdata)
6956 (verilog-sig-type portdata)
6957 multidim)
6958 sigs-intf)))
6959 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
6960 )))))
6961
6962 (defun verilog-read-sub-decls-expr (submoddecls comment port expr)
6963 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
6964 ;;(message "vrsde: '%s'" expr)
6965 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
6966 (setq expr (verilog-string-replace-matches "/\\*\\(\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
6967 ;; Remove front operators
6968 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
6969 ;;
6970 (cond
6971 ;; {..., a, b} requires us to recurse on a,b
6972 ((string-match "^\\s-*{\\([^{}]*\\)}\\s-*$" expr)
6973 (unless verilog-auto-ignore-concat
6974 (let ((mlst (split-string (match-string 1 expr) ","))
6975 mstr)
6976 (while (setq mstr (pop mlst))
6977 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
6978 (t
6979 (let (sig vec multidim)
6980 (cond ;; Find \signal. Final space is part of escaped signal name
6981 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
6982 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
6983 (setq sig (match-string 1 expr)
6984 expr (substring expr (match-end 0))))
6985 ;; Find signal
6986 ((string-match "^\\s-*\\([^[({).\\]+\\)" expr)
6987 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
6988 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
6989 expr (substring expr (match-end 0)))))
6990 ;; Find [vector] or [multi][multi][multi][vector]
6991 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
6992 ;;(message "vrsde-v: '%s'" (match-string 1 expr))
6993 (when vec (setq multidim (cons vec multidim)))
6994 (setq vec (match-string 1 expr)
6995 expr (substring expr (match-end 0))))
6996 ;; If found signal, and nothing unrecognized, add the signal
6997 ;;(message "vrsde-rem: '%s'" expr)
6998 (when (and sig (string-match "^\\s-*$" expr))
6999 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))))
7000
7001 (defun verilog-read-sub-decls-line (submoddecls comment)
7002 "For `verilog-read-sub-decls', read lines of port defs until none match anymore.
7003 Return the list of signals found, using submodi to look up each port."
7004 (let (done port)
7005 (save-excursion
7006 (forward-line 1)
7007 (while (not done)
7008 ;; Get port name
7009 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
7010 (setq port (match-string 1))
7011 (goto-char (match-end 0)))
7012 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
7013 (setq port (concat (match-string 1) " ")) ;; escaped id's need trailing space
7014 (goto-char (match-end 0)))
7015 ((looking-at "\\s-*\\.[^(]*(")
7016 (setq port nil) ;; skip this line
7017 (goto-char (match-end 0)))
7018 (t
7019 (setq port nil done t))) ;; Unknown, ignore rest of line
7020 ;; Get signal name. Point is at the first-non-space after (
7021 ;; We intentionally ignore (non-escaped) signals with .s in them
7022 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
7023 (when port
7024 (cond ((looking-at "\\([^[({).\\]*\\)\\s-*)")
7025 (verilog-read-sub-decls-sig
7026 submoddecls comment port
7027 (verilog-string-remove-spaces (match-string 1)) ; sig
7028 nil nil)) ; vec multidim
7029 ;;
7030 ((looking-at "\\([^[({).\\]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
7031 (verilog-read-sub-decls-sig
7032 submoddecls comment port
7033 (verilog-string-remove-spaces (match-string 1)) ; sig
7034 (match-string 2) nil)) ; vec multidim
7035 ;; Fastpath was above looking-at's.
7036 ;; For something more complicated invoke a parser
7037 ((looking-at "[^)]+")
7038 (verilog-read-sub-decls-expr
7039 submoddecls comment port
7040 (buffer-substring
7041 (point) (1- (progn (search-backward "(") ; start at (
7042 (forward-sexp 1) (point)))))))) ; expr
7043 ;;
7044 (forward-line 1)))))
7045
7046 (defun verilog-read-sub-decls ()
7047 "Internally parse signals going to modules under this module.
7048 Return a array of [ outputs inouts inputs ] signals for modules that are
7049 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
7050 is a output, then SIG will be included in the list.
7051
7052 This only works on instantiations created with /*AUTOINST*/ converted by
7053 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
7054 component library to determine connectivity of the design.
7055
7056 One work around for this problem is to manually create // Inputs and //
7057 Outputs comments above subcell signals, for example:
7058
7059 module ModuleName (
7060 // Outputs
7061 .out (out),
7062 // Inputs
7063 .in (in));"
7064 (save-excursion
7065 (let ((end-mod-point (verilog-get-end-of-defun t))
7066 st-point end-inst-point
7067 ;; below 3 modified by verilog-read-sub-decls-line
7068 sigs-out sigs-inout sigs-in sigs-intf)
7069 (verilog-beg-of-defun)
7070 (while (verilog-re-search-forward "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
7071 (save-excursion
7072 (goto-char (match-beginning 0))
7073 (unless (verilog-inside-comment-p)
7074 ;; Attempt to snarf a comment
7075 (let* ((submod (verilog-read-inst-module))
7076 (inst (verilog-read-inst-name))
7077 (comment (concat inst " of " submod ".v"))
7078 submodi submoddecls)
7079 (when (setq submodi (verilog-modi-lookup submod t))
7080 (setq submoddecls (verilog-modi-get-decls submodi))
7081 ;; This could have used a list created by verilog-auto-inst
7082 ;; However I want it to be runnable even on user's manually added signals
7083 (verilog-backward-open-paren)
7084 (setq end-inst-point (save-excursion (forward-sexp 1) (point))
7085 st-point (point))
7086 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
7087 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
7088 (goto-char st-point)
7089 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
7090 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
7091 (goto-char st-point)
7092 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
7093 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-inout
7094 (goto-char st-point)
7095 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
7096 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-in
7097 )))))
7098 ;; Combine duplicate bits
7099 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
7100 (vector (verilog-signals-combine-bus (nreverse sigs-out))
7101 (verilog-signals-combine-bus (nreverse sigs-inout))
7102 (verilog-signals-combine-bus (nreverse sigs-in))
7103 (verilog-signals-combine-bus (nreverse sigs-intf))))))
7104
7105 (defun verilog-read-inst-pins ()
7106 "Return an array of [ pins ] for the current instantiation at point.
7107 For example if declare A A (.B(SIG)) then B will be included in the list."
7108 (save-excursion
7109 (let ((end-mod-point (point)) ;; presume at /*AUTOINST*/ point
7110 pins pin)
7111 (verilog-backward-open-paren)
7112 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
7113 (setq pin (match-string 1))
7114 (unless (verilog-inside-comment-p)
7115 (setq pins (cons (list pin) pins))
7116 (when (looking-at "(")
7117 (forward-sexp 1))))
7118 (vector pins))))
7119
7120 (defun verilog-read-arg-pins ()
7121 "Return an array of [ pins ] for the current argument declaration at point."
7122 (save-excursion
7123 (let ((end-mod-point (point)) ;; presume at /*AUTOARG*/ point
7124 pins pin)
7125 (verilog-backward-open-paren)
7126 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
7127 (setq pin (match-string 1))
7128 (unless (verilog-inside-comment-p)
7129 (setq pins (cons (list pin) pins))))
7130 (vector pins))))
7131
7132 (defun verilog-read-auto-constants (beg end-mod-point)
7133 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
7134 ;; Insert new
7135 (save-excursion
7136 (let (sig-list tpl-end-pt)
7137 (goto-char beg)
7138 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
7139 (if (not (looking-at "\\s *("))
7140 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
7141 (search-forward "(" end-mod-point)
7142 (setq tpl-end-pt (save-excursion
7143 (backward-char 1)
7144 (forward-sexp 1) ;; Moves to paren that closes argdecl's
7145 (backward-char 1)
7146 (point)))
7147 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
7148 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
7149 sig-list)))
7150
7151 (defun verilog-read-auto-lisp (start end)
7152 "Look for and evaluate a AUTO_LISP between START and END."
7153 (save-excursion
7154 (goto-char start)
7155 (while (re-search-forward "\\<AUTO_LISP(" end t)
7156 (backward-char)
7157 (let* ((beg-pt (prog1 (point)
7158 (forward-sexp 1))) ;; Closing paren
7159 (end-pt (point)))
7160 (eval-region beg-pt end-pt nil)))))
7161
7162 (eval-when-compile
7163 ;; Prevent compile warnings; these are let's, not globals
7164 ;; Do not remove the eval-when-compile
7165 ;; - we want a error when we are debugging this code if they are refed.
7166 (defvar sigs-in)
7167 (defvar sigs-out)
7168 (defvar got-sig)
7169 (defvar got-rvalue)
7170 (defvar uses-delayed)
7171 (defvar vector-skip-list))
7172
7173 (defun verilog-read-always-signals-recurse
7174 (exit-keywd rvalue ignore-next)
7175 "Recursive routine for parentheses/bracket matching.
7176 EXIT-KEYWD is expression to stop at, nil if top level.
7177 RVALUE is true if at right hand side of equal.
7178 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
7179 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ;; true if after a ; we are looking for rvalue
7180 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-rvalue end-else-check)
7181 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue ignore-next))))
7182 (while (not (or (eobp) gotend))
7183 (cond
7184 ((looking-at "//")
7185 (search-forward "\n"))
7186 ((looking-at "/\\*")
7187 (or (search-forward "*/")
7188 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
7189 ((looking-at "(\\*")
7190 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
7191 (search-forward "*)")
7192 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
7193 (t (setq keywd (buffer-substring-no-properties
7194 (point)
7195 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
7196 (forward-char 1))
7197 (point)))
7198 sig-last-tolk sig-tolk
7199 sig-tolk nil)
7200 ;;(if dbg (setq dbg (concat dbg (format "\tPt=%S %S\trv=%S in=%S ee=%S gs=%S\n" (point) keywd rvalue ignore-next end-else-check got-sig))))
7201 (cond
7202 ((equal keywd "\"")
7203 (or (re-search-forward "[^\\]\"" nil t)
7204 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
7205 ;; else at top level loop, keep parsing
7206 ((and end-else-check (equal keywd "else"))
7207 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
7208 ;; no forward movement, want to see else in lower loop
7209 (setq end-else-check nil))
7210 ;; End at top level loop
7211 ((and end-else-check (looking-at "[^ \t\n\f]"))
7212 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
7213 (setq gotend t))
7214 ;; Final statement?
7215 ((and exit-keywd (equal keywd exit-keywd))
7216 (setq gotend t)
7217 (forward-char (length keywd)))
7218 ;; Standard tokens...
7219 ((equal keywd ";")
7220 (setq ignore-next nil rvalue semi-rvalue)
7221 ;; Final statement at top level loop?
7222 (when (not exit-keywd)
7223 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
7224 (setq end-else-check t))
7225 (forward-char 1))
7226 ((equal keywd "'")
7227 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
7228 (goto-char (match-end 0))
7229 (forward-char 1)))
7230 ((equal keywd ":") ;; Case statement, begin/end label, x?y:z
7231 (cond ((equal "endcase" exit-keywd) ;; case x: y=z; statement next
7232 (setq ignore-next nil rvalue nil))
7233 ((equal "?" exit-keywd) ;; x?y:z rvalue
7234 ) ;; NOP
7235 ((equal "]" exit-keywd) ;; [x:y] rvalue
7236 ) ;; NOP
7237 (got-sig ;; label: statement
7238 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
7239 ((not rvalue) ;; begin label
7240 (setq ignore-next t rvalue nil)))
7241 (forward-char 1))
7242 ((equal keywd "=")
7243 (if (and (eq (char-before) ?< )
7244 (not rvalue))
7245 (setq uses-delayed 1))
7246 (setq ignore-next nil rvalue t)
7247 (forward-char 1))
7248 ((equal keywd "?")
7249 (forward-char 1)
7250 (verilog-read-always-signals-recurse ":" rvalue nil))
7251 ((equal keywd "[")
7252 (forward-char 1)
7253 (verilog-read-always-signals-recurse "]" t nil))
7254 ((equal keywd "(")
7255 (forward-char 1)
7256 (cond (sig-last-tolk ;; Function call; zap last signal
7257 (setq got-sig nil)))
7258 (cond ((equal last-keywd "for")
7259 (verilog-read-always-signals-recurse ";" nil nil)
7260 (verilog-read-always-signals-recurse ";" t nil)
7261 (verilog-read-always-signals-recurse ")" nil nil))
7262 (t (verilog-read-always-signals-recurse ")" t nil))))
7263 ((equal keywd "begin")
7264 (skip-syntax-forward "w_")
7265 (verilog-read-always-signals-recurse "end" nil nil)
7266 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
7267 (setq ignore-next nil rvalue semi-rvalue)
7268 (if (not exit-keywd) (setq end-else-check t)))
7269 ((or (equal keywd "case")
7270 (equal keywd "casex")
7271 (equal keywd "casez"))
7272 (skip-syntax-forward "w_")
7273 (verilog-read-always-signals-recurse "endcase" t nil)
7274 (setq ignore-next nil rvalue semi-rvalue)
7275 (if (not exit-keywd) (setq gotend t))) ;; top level begin/end
7276 ((string-match "^[$`a-zA-Z_]" keywd) ;; not exactly word constituent
7277 (cond ((or (equal keywd "`ifdef")
7278 (equal keywd "`ifndef"))
7279 (setq ignore-next t))
7280 ((or ignore-next
7281 (member keywd verilog-keywords)
7282 (string-match "^\\$" keywd)) ;; PLI task
7283 (setq ignore-next nil))
7284 (t
7285 (setq keywd (verilog-symbol-detick-denumber keywd))
7286 (when got-sig
7287 (if got-rvalue (setq sigs-in (cons got-sig sigs-in))
7288 (setq sigs-out (cons got-sig sigs-out)))
7289 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S rv=%S\n" got-sig got-rvalue))))
7290 )
7291 (setq got-rvalue rvalue
7292 got-sig (if (or (not keywd)
7293 (assoc keywd (if got-rvalue sigs-in sigs-out)))
7294 nil (list keywd nil nil))
7295 sig-tolk t)))
7296 (skip-chars-forward "a-zA-Z0-9$_.%`"))
7297 (t
7298 (forward-char 1)))
7299 ;; End of non-comment token
7300 (setq last-keywd keywd)))
7301 (skip-syntax-forward " "))
7302 ;; Append the final pending signal
7303 (when got-sig
7304 (if got-rvalue (setq sigs-in (cons got-sig sigs-in))
7305 (setq sigs-out (cons got-sig sigs-out)))
7306 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S rv=%S\n" got-sig got-rvalue))))
7307 (setq got-sig nil))
7308 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
7309 ))
7310
7311 (defun verilog-read-always-signals ()
7312 "Parse always block at point and return list of (outputs inout inputs)."
7313 ;; Insert new
7314 (save-excursion
7315 (let* (;;(dbg "")
7316 sigs-in sigs-out
7317 uses-delayed) ;; Found signal/rvalue; push if not function
7318 (search-forward ")")
7319 (verilog-read-always-signals-recurse nil nil nil)
7320 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg ""))
7321 ;; Return what was found
7322 (list sigs-out nil sigs-in uses-delayed))))
7323
7324 (defun verilog-read-instants ()
7325 "Parse module at point and return list of ( ( file instance ) ... )."
7326 (verilog-beg-of-defun)
7327 (let* ((end-mod-point (verilog-get-end-of-defun t))
7328 (state nil)
7329 (instants-list nil))
7330 (save-excursion
7331 (while (< (point) end-mod-point)
7332 ;; Stay at level 0, no comments
7333 (while (progn
7334 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
7335 (or (> (car state) 0) ; in parens
7336 (nth 5 state) ; comment
7337 ))
7338 (forward-line 1))
7339 (beginning-of-line)
7340 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
7341 ;;(if (looking-at "^\\(.+\\)$")
7342 (let ((module (match-string 1))
7343 (instant (match-string 2)))
7344 (if (not (member module verilog-keywords))
7345 (setq instants-list (cons (list module instant) instants-list)))))
7346 (forward-line 1)))
7347 instants-list))
7348
7349
7350 (defun verilog-read-auto-template (module)
7351 "Look for a auto_template for the instantiation of the given MODULE.
7352 If found returns the signal name connections. Return REGEXP and
7353 list of ( (signal_name connection_name)... )."
7354 (save-excursion
7355 ;; Find beginning
7356 (let ((tpl-regexp "\\([0-9]+\\)")
7357 (lineno 0)
7358 (templateno 0)
7359 tpl-sig-list tpl-wild-list tpl-end-pt rep)
7360 (cond ((or
7361 (re-search-backward (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
7362 (progn
7363 (goto-char (point-min))
7364 (re-search-forward (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
7365 (goto-char (match-end 0))
7366 ;; Parse "REGEXP"
7367 ;; We reserve @"..." for future lisp expressions that evaluate once-per-AUTOINST
7368 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
7369 (setq tpl-regexp (match-string 1))
7370 (goto-char (match-end 0)))
7371 (search-forward "(")
7372 ;; Parse lines in the template
7373 (when verilog-auto-inst-template-numbers
7374 (save-excursion
7375 (goto-char (point-min))
7376 (while (search-forward "AUTO_TEMPLATE" nil t)
7377 (setq templateno (1+ templateno)))))
7378 (setq tpl-end-pt (save-excursion
7379 (backward-char 1)
7380 (forward-sexp 1) ;; Moves to paren that closes argdecl's
7381 (backward-char 1)
7382 (point)))
7383 ;;
7384 (while (< (point) tpl-end-pt)
7385 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
7386 (setq tpl-sig-list (cons (list
7387 (match-string-no-properties 1)
7388 (match-string-no-properties 2)
7389 templateno lineno)
7390 tpl-sig-list))
7391 (goto-char (match-end 0)))
7392 ;; Regexp form??
7393 ((looking-at
7394 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
7395 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
7396 (setq rep (match-string-no-properties 3))
7397 (goto-char (match-end 0))
7398 (setq tpl-wild-list
7399 (cons (list
7400 (concat "^"
7401 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
7402 (match-string 1))
7403 "$")
7404 rep
7405 templateno lineno)
7406 tpl-wild-list)))
7407 ((looking-at "[ \t\f]+")
7408 (goto-char (match-end 0)))
7409 ((looking-at "\n")
7410 (setq lineno (1+ lineno))
7411 (goto-char (match-end 0)))
7412 ((looking-at "//")
7413 (search-forward "\n"))
7414 ((looking-at "/\\*")
7415 (forward-char 2)
7416 (or (search-forward "*/")
7417 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
7418 (t
7419 (error "%s: AUTO_TEMPLATE parsing error: %s"
7420 (verilog-point-text)
7421 (progn (looking-at ".*$") (match-string 0))))))
7422 ;; Return
7423 (vector tpl-regexp
7424 (list tpl-sig-list tpl-wild-list)))
7425 ;; If no template found
7426 (t (vector tpl-regexp nil))))))
7427 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
7428
7429 (defun verilog-set-define (defname defvalue &optional buffer enumname)
7430 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
7431 Optionally associate it with the specified enumeration ENUMNAME."
7432 (with-current-buffer (or buffer (current-buffer))
7433 (let ((mac (intern (concat "vh-" defname))))
7434 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
7435 ;; Need to define to a constant if no value given
7436 (set (make-local-variable mac)
7437 (if (equal defvalue "") "1" defvalue)))
7438 (if enumname
7439 (let ((enumvar (intern (concat "venum-" enumname))))
7440 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
7441 (unless (boundp enumvar) (set enumvar nil))
7442 (make-local-variable enumvar)
7443 (add-to-list enumvar defname)))))
7444
7445 (defun verilog-read-defines (&optional filename recurse subcall)
7446 "Read `defines and parameters for the current file, or optional FILENAME.
7447 If the filename is provided, `verilog-library-flags' will be used to
7448 resolve it. If optional RECURSE is non-nil, recurse through `includes.
7449
7450 Parameters must be simple assignments to constants, or have their own
7451 \"parameter\" label rather than a list of parameters. Thus:
7452
7453 parameter X = 5, Y = 10; // Ok
7454 parameter X = {1'b1, 2'h2}; // Ok
7455 parameter X = {1'b1, 2'h2}, Y = 10; // Bad, make into 2 parameter lines
7456
7457 Defines must be simple text substitutions, one on a line, starting
7458 at the beginning of the line. Any ifdefs or multiline comments around the
7459 define are ignored.
7460
7461 Defines are stored inside Emacs variables using the name vh-{definename}.
7462
7463 This function is useful for setting vh-* variables. The file variables
7464 feature can be used to set defines that `verilog-mode' can see; put at the
7465 *END* of your file something like:
7466
7467 // Local Variables:
7468 // vh-macro:\"macro_definition\"
7469 // End:
7470
7471 If macros are defined earlier in the same file and you want their values,
7472 you can read them automatically (provided `enable-local-eval' is on):
7473
7474 // Local Variables:
7475 // eval:(verilog-read-defines)
7476 // eval:(verilog-read-defines \"group_standard_includes.v\")
7477 // End:
7478
7479 Note these are only read when the file is first visited, you must use
7480 \\[find-alternate-file] RET to have these take effect after editing them!
7481
7482 If you want to disable the \"Process `eval' or hook local variables\"
7483 warning message, you need to add to your .emacs file:
7484
7485 (setq enable-local-eval t)"
7486 (let ((origbuf (current-buffer)))
7487 (save-excursion
7488 (unless subcall (verilog-getopt-flags))
7489 (when filename
7490 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
7491 (if fns
7492 (set-buffer (find-file-noselect (car fns)))
7493 (error (concat (verilog-point-text)
7494 ": Can't find verilog-read-defines file: " filename)))))
7495 (when recurse
7496 (goto-char (point-min))
7497 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
7498 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string-no-properties 1))))
7499 (unless (verilog-inside-comment-p)
7500 (verilog-read-defines inc recurse t)))))
7501 ;; Read `defines
7502 ;; note we don't use verilog-re... it's faster this way, and that
7503 ;; function has problems when comments are at the end of the define
7504 (goto-char (point-min))
7505 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
7506 (let ((defname (match-string-no-properties 1))
7507 (defvalue (match-string-no-properties 2)))
7508 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
7509 (verilog-set-define defname defvalue origbuf)))
7510 ;; Hack: Read parameters
7511 (goto-char (point-min))
7512 (while (re-search-forward
7513 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-+" nil t)
7514 (let (enumname)
7515 ;; The primary way of getting defines is verilog-read-decls
7516 ;; However, that isn't called yet for included files, so we'll add another scheme
7517 (if (looking-at "[^\n]*synopsys\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
7518 (setq enumname (match-string-no-properties 1)))
7519 (forward-comment 999)
7520 (while (looking-at "\\s-*,?\\s-*\\([a-zA-Z0-9_$]+\\)\\s-*=\\s-*\\([^;,]*\\),?\\s-*")
7521 (verilog-set-define (match-string-no-properties 1) (match-string-no-properties 2) origbuf enumname)
7522 (goto-char (match-end 0))
7523 (forward-comment 999)))))))
7524
7525 (defun verilog-read-includes ()
7526 "Read `includes for the current file.
7527 This will find all of the `includes which are at the beginning of lines,
7528 ignoring any ifdefs or multiline comments around them.
7529 `verilog-read-defines' is then performed on the current and each included
7530 file.
7531
7532 It is often useful put at the *END* of your file something like:
7533
7534 // Local Variables:
7535 // eval:(verilog-read-defines)
7536 // eval:(verilog-read-includes)
7537 // End:
7538
7539 Note includes are only read when the file is first visited, you must use
7540 \\[find-alternate-file] RET to have these take effect after editing them!
7541
7542 It is good to get in the habit of including all needed files in each .v
7543 file that needs it, rather than waiting for compile time. This will aid
7544 this process, Verilint, and readability. To prevent defining the same
7545 variable over and over when many modules are compiled together, put a test
7546 around the inside each include file:
7547
7548 foo.v (a include):
7549 `ifdef _FOO_V // include if not already included
7550 `else
7551 `define _FOO_V
7552 ... contents of file
7553 `endif // _FOO_V"
7554 ;;slow: (verilog-read-defines nil t))
7555 (save-excursion
7556 (verilog-getopt-flags)
7557 (goto-char (point-min))
7558 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
7559 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
7560 (verilog-read-defines inc nil t)))))
7561
7562 (defun verilog-read-signals (&optional start end)
7563 "Return a simple list of all possible signals in the file.
7564 Bounded by optional region from START to END. Overly aggressive but fast.
7565 Some macros and such are also found and included. For dinotrace.el."
7566 (let (sigs-all keywd)
7567 (progn;save-excursion
7568 (goto-char (or start (point-min)))
7569 (setq end (or end (point-max)))
7570 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
7571 (forward-char -1)
7572 (cond
7573 ((looking-at "//")
7574 (search-forward "\n"))
7575 ((looking-at "/\\*")
7576 (search-forward "*/"))
7577 ((looking-at "(\\*")
7578 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
7579 (search-forward "*)")))
7580 ((eq ?\" (following-char))
7581 (re-search-forward "[^\\]\"")) ;; don't forward-char first, since we look for a non backslash first
7582 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
7583 (goto-char (match-end 0))
7584 (setq keywd (match-string-no-properties 1))
7585 (or (member keywd verilog-keywords)
7586 (member keywd sigs-all)
7587 (setq sigs-all (cons keywd sigs-all))))
7588 (t (forward-char 1))))
7589 ;; Return list
7590 sigs-all)))
7591
7592 ;;
7593 ;; Argument file parsing
7594 ;;
7595
7596 (defun verilog-getopt (arglist)
7597 "Parse -f, -v etc arguments in ARGLIST list or string."
7598 (unless (listp arglist) (setq arglist (list arglist)))
7599 (let ((space-args '())
7600 arg next-param)
7601 ;; Split on spaces, so users can pass whole command lines
7602 (while arglist
7603 (setq arg (car arglist)
7604 arglist (cdr arglist))
7605 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
7606 (setq space-args (append space-args
7607 (list (match-string-no-properties 1 arg))))
7608 (setq arg (match-string 2 arg))))
7609 ;; Parse arguments
7610 (while space-args
7611 (setq arg (car space-args)
7612 space-args (cdr space-args))
7613 (cond
7614 ;; Need another arg
7615 ((equal arg "-f")
7616 (setq next-param arg))
7617 ((equal arg "-v")
7618 (setq next-param arg))
7619 ((equal arg "-y")
7620 (setq next-param arg))
7621 ;; +libext+(ext1)+(ext2)...
7622 ((string-match "^\\+libext\\+\\(.*\\)" arg)
7623 (setq arg (match-string 1 arg))
7624 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
7625 (verilog-add-list-unique `verilog-library-extensions
7626 (match-string 1 arg))
7627 (setq arg (match-string 2 arg))))
7628 ;;
7629 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; -Ddefine=val
7630 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ;; -Ddefine
7631 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; +define+val
7632 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ;; +define+define
7633 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
7634 ;;
7635 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ;; +incdir+dir
7636 (string-match "^-I\\(.*\\)" arg)) ;; -Idir
7637 (verilog-add-list-unique `verilog-library-directories
7638 (match-string 1 (substitute-in-file-name arg))))
7639 ;; Ignore
7640 ((equal "+librescan" arg))
7641 ((string-match "^-U\\(.*\\)" arg)) ;; -Udefine
7642 ;; Second parameters
7643 ((equal next-param "-f")
7644 (setq next-param nil)
7645 (verilog-getopt-file (substitute-in-file-name arg)))
7646 ((equal next-param "-v")
7647 (setq next-param nil)
7648 (verilog-add-list-unique `verilog-library-files
7649 (substitute-in-file-name arg)))
7650 ((equal next-param "-y")
7651 (setq next-param nil)
7652 (verilog-add-list-unique `verilog-library-directories
7653 (substitute-in-file-name arg)))
7654 ;; Filename
7655 ((string-match "^[^-+]" arg)
7656 (verilog-add-list-unique `verilog-library-files
7657 (substitute-in-file-name arg)))
7658 ;; Default - ignore; no warning
7659 ))))
7660 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
7661
7662 (defun verilog-getopt-file (filename)
7663 "Read Verilog options from the specified FILENAME."
7664 (save-excursion
7665 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
7666 (orig-buffer (current-buffer))
7667 line)
7668 (if fns
7669 (set-buffer (find-file-noselect (car fns)))
7670 (error (concat (verilog-point-text)
7671 ": Can't find verilog-getopt-file -f file: " filename)))
7672 (goto-char (point-min))
7673 (while (not (eobp))
7674 (setq line (buffer-substring (point)
7675 (save-excursion (end-of-line) (point))))
7676 (forward-line 1)
7677 (when (string-match "//" line)
7678 (setq line (substring line 0 (match-beginning 0))))
7679 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
7680 (verilog-getopt line))))))
7681
7682 (defun verilog-getopt-flags ()
7683 "Convert `verilog-library-flags' into standard library variables."
7684 ;; If the flags are local, then all the outputs should be local also
7685 (when (local-variable-p `verilog-library-flags (current-buffer))
7686 (mapc 'make-local-variable '(verilog-library-extensions
7687 verilog-library-directories
7688 verilog-library-files
7689 verilog-library-flags)))
7690 ;; Allow user to customize
7691 (run-hooks 'verilog-before-getopt-flags-hook)
7692 ;; Process arguments
7693 (verilog-getopt verilog-library-flags)
7694 ;; Allow user to customize
7695 (run-hooks 'verilog-getopt-flags-hook))
7696
7697 (defun verilog-add-list-unique (varref object)
7698 "Append to VARREF list the given OBJECT,
7699 unless it is already a member of the variable's list."
7700 (unless (member object (symbol-value varref))
7701 (set varref (append (symbol-value varref) (list object))))
7702 varref)
7703 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
7704
7705 \f
7706 ;;
7707 ;; Cached directory support
7708 ;;
7709
7710 (defvar verilog-dir-cache-preserving nil
7711 "If set, the directory cache is enabled, and file system changes are ignored.
7712 See `verilog-dir-exists-p' and `verilog-dir-files'.")
7713
7714 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
7715 (defvar verilog-dir-cache-list nil
7716 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
7717 (defvar verilog-dir-cache-lib-filenames nil
7718 "Cached data for `verilog-library-filenames'.")
7719
7720 (defmacro verilog-preserve-dir-cache (&rest body)
7721 "Execute the BODY forms, allowing directory cache preservation within BODY.
7722 This means that changes inside BODY made to the file system will not be
7723 seen by the `verilog-dir-files' and related functions."
7724 `(let ((verilog-dir-cache-preserving t)
7725 verilog-dir-cache-list
7726 verilog-dir-cache-lib-filenames)
7727 (progn ,@body)))
7728
7729 (defun verilog-dir-files (dirname)
7730 "Return all filenames in the DIRNAME directory.
7731 Relative paths depend on the `default-directory'.
7732 Results are cached if inside `verilog-preserve-dir-cache'."
7733 (unless verilog-dir-cache-preserving
7734 (setq verilog-dir-cache-list nil)) ;; Cache disabled
7735 ;; We don't use expand-file-name on the dirname to make key, as it's slow
7736 (let* ((cache-key (list dirname default-directory))
7737 (fass (assoc cache-key verilog-dir-cache-list))
7738 exp-dirname data)
7739 (cond (fass ;; Return data from cache hit
7740 (nth 1 fass))
7741 (t
7742 (setq exp-dirname (expand-file-name dirname)
7743 data (and (file-directory-p exp-dirname)
7744 (directory-files exp-dirname nil nil nil)))
7745 ;; Note we also encache nil for non-existing dirs.
7746 (setq verilog-dir-cache-list (cons (list cache-key data)
7747 verilog-dir-cache-list))
7748 data))))
7749 ;; Miss-and-hit test:
7750 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
7751 ;; (prin1 (verilog-dir-files ".")) nil)
7752
7753 (defun verilog-dir-file-exists-p (filename)
7754 "Return true if FILENAME exists.
7755 Like `file-exists-p' but results are cached if inside
7756 `verilog-preserve-dir-cache'."
7757 (let* ((dirname (file-name-directory filename))
7758 ;; Correct for file-name-nondirectory returning same if no slash.
7759 (dirnamed (if (or (not dirname) (equal dirname filename))
7760 default-directory dirname))
7761 (flist (verilog-dir-files dirnamed)))
7762 (and flist
7763 (member (file-name-nondirectory filename) flist)
7764 t)))
7765 ;;(verilog-dir-file-exists-p "verilog-mode.el")
7766 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
7767
7768 \f
7769 ;;
7770 ;; Module name lookup
7771 ;;
7772
7773 (defun verilog-module-inside-filename-p (module filename)
7774 "Return point if MODULE is specified inside FILENAME, else nil.
7775 Allows version control to check out the file if need be."
7776 (and (or (file-exists-p filename)
7777 (and (fboundp 'vc-backend)
7778 (vc-backend filename)))
7779 (let (pt)
7780 (with-current-buffer (find-file-noselect filename)
7781 (save-excursion
7782 (goto-char (point-min))
7783 (while (and
7784 ;; It may be tempting to look for verilog-defun-re,
7785 ;; don't, it slows things down a lot!
7786 (verilog-re-search-forward-quick "\\<module\\>" nil t)
7787 (verilog-re-search-forward-quick "[(;]" nil t))
7788 (if (equal module (verilog-read-module-name))
7789 (setq pt (point))))
7790 pt)))))
7791
7792 (defun verilog-is-number (symbol)
7793 "Return true if SYMBOL is number-like."
7794 (or (string-match "^[0-9 \t:]+$" symbol)
7795 (string-match "^[---]*[0-9]+$" symbol)
7796 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
7797
7798 (defun verilog-symbol-detick (symbol wing-it)
7799 "Return an expanded SYMBOL name without any defines.
7800 If the variable vh-{symbol} is defined, return that value.
7801 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
7802 (while (and symbol (string-match "^`" symbol))
7803 (setq symbol (substring symbol 1))
7804 (setq symbol
7805 (if (boundp (intern (concat "vh-" symbol)))
7806 ;; Emacs has a bug where boundp on a buffer-local
7807 ;; variable in only one buffer returns t in another.
7808 ;; This can confuse, so check for nil.
7809 (let ((val (eval (intern (concat "vh-" symbol)))))
7810 (if (eq val nil)
7811 (if wing-it symbol nil)
7812 val))
7813 (if wing-it symbol nil))))
7814 symbol)
7815 ;;(verilog-symbol-detick "`mod" nil)
7816
7817 (defun verilog-symbol-detick-denumber (symbol)
7818 "Return SYMBOL with defines converted and any numbers dropped to nil."
7819 (when (string-match "^`" symbol)
7820 ;; This only will work if the define is a simple signal, not
7821 ;; something like a[b]. Sorry, it should be substituted into the parser
7822 (setq symbol
7823 (verilog-string-replace-matches
7824 "\[[^0-9: \t]+\]" "" nil nil
7825 (or (verilog-symbol-detick symbol nil)
7826 (if verilog-auto-sense-defines-constant
7827 "0"
7828 symbol)))))
7829 (if (verilog-is-number symbol)
7830 nil
7831 symbol))
7832
7833 (defun verilog-symbol-detick-text (text)
7834 "Return TEXT without any known defines.
7835 If the variable vh-{symbol} is defined, substitute that value."
7836 (let ((ok t) symbol val)
7837 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
7838 (setq symbol (match-string 1 text))
7839 ;;(message symbol)
7840 (cond ((and
7841 (boundp (intern (concat "vh-" symbol)))
7842 ;; Emacs has a bug where boundp on a buffer-local
7843 ;; variable in only one buffer returns t in another.
7844 ;; This can confuse, so check for nil.
7845 (setq val (eval (intern (concat "vh-" symbol)))))
7846 (setq text (replace-match val nil nil text)))
7847 (t (setq ok nil)))))
7848 text)
7849 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
7850
7851 (defun verilog-expand-dirnames (&optional dirnames)
7852 "Return a list of existing directories given a list of wildcarded DIRNAMES.
7853 Or, just the existing dirnames themselves if there are no wildcards."
7854 ;; Note this function is performance critical.
7855 ;; Do not call anything that requires disk access that cannot be cached.
7856 (interactive)
7857 (unless dirnames (error "`verilog-library-directories' should include at least '.'"))
7858 (setq dirnames (reverse dirnames)) ; not nreverse
7859 (let ((dirlist nil)
7860 pattern dirfile dirfiles dirname root filename rest basefile)
7861 (while dirnames
7862 (setq dirname (substitute-in-file-name (car dirnames))
7863 dirnames (cdr dirnames))
7864 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ;; root
7865 "\\([^/\\]*[*?][^/\\]*\\)" ;; filename with *?
7866 "\\(.*\\)") ;; rest
7867 dirname)
7868 (setq root (match-string 1 dirname)
7869 filename (match-string 2 dirname)
7870 rest (match-string 3 dirname)
7871 pattern filename)
7872 ;; now replace those * and ? with .+ and .
7873 ;; use ^ and /> to get only whole file names
7874 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
7875 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
7876 pattern (concat "^" pattern "$")
7877 dirfiles (verilog-dir-files root))
7878 (while dirfiles
7879 (setq basefile (car dirfiles)
7880 dirfile (expand-file-name (concat root basefile rest))
7881 dirfiles (cdr dirfiles))
7882 (if (and (string-match pattern basefile)
7883 ;; Don't allow abc/*/rtl to match abc/rtl via ..
7884 (not (equal basefile "."))
7885 (not (equal basefile ".."))
7886 (file-directory-p dirfile))
7887 (setq dirlist (cons dirfile dirlist)))))
7888 ;; Defaults
7889 (t
7890 (if (file-directory-p dirname)
7891 (setq dirlist (cons dirname dirlist))))))
7892 dirlist))
7893 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
7894
7895 (defun verilog-library-filenames (filename &optional current check-ext)
7896 "Return a search path to find the given FILENAME or module name.
7897 Uses the optional CURRENT filename or buffer-file-name, plus
7898 `verilog-library-directories' and `verilog-library-extensions'
7899 variables to build the path. With optional CHECK-EXT also check
7900 `verilog-library-extensions'."
7901 (unless current (setq current (buffer-file-name)))
7902 (unless verilog-dir-cache-preserving
7903 (setq verilog-dir-cache-lib-filenames nil))
7904 (let* ((cache-key (list filename current check-ext))
7905 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
7906 chkdirs chkdir chkexts fn outlist)
7907 (cond (fass ;; Return data from cache hit
7908 (nth 1 fass))
7909 (t
7910 ;; Note this expand can't be easily cached, as we need to
7911 ;; pick up buffer-local variables for newly read sub-module files
7912 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
7913 (while chkdirs
7914 (setq chkdir (expand-file-name (car chkdirs)
7915 (file-name-directory current))
7916 chkexts (if check-ext verilog-library-extensions `("")))
7917 (while chkexts
7918 (setq fn (expand-file-name (concat filename (car chkexts))
7919 chkdir))
7920 ;;(message "Check for %s" fn)
7921 (if (verilog-dir-file-exists-p fn)
7922 (setq outlist (cons (expand-file-name
7923 fn (file-name-directory current))
7924 outlist)))
7925 (setq chkexts (cdr chkexts)))
7926 (setq chkdirs (cdr chkdirs)))
7927 (setq outlist (nreverse outlist))
7928 (setq verilog-dir-cache-lib-filenames
7929 (cons (list cache-key outlist)
7930 verilog-dir-cache-lib-filenames))
7931 outlist))))
7932
7933 (defun verilog-module-filenames (module current)
7934 "Return a search path to find the given MODULE name.
7935 Uses the CURRENT filename, `verilog-library-extensions',
7936 `verilog-library-directories' and `verilog-library-files'
7937 variables to build the path."
7938 ;; Return search locations for it
7939 (append (list current) ; first, current buffer
7940 (verilog-library-filenames module current t)
7941 verilog-library-files)) ; finally, any libraries
7942
7943 ;;
7944 ;; Module Information
7945 ;;
7946 ;; Many of these functions work on "modi" a module information structure
7947 ;; A modi is: [module-name-string file-name begin-point]
7948
7949 (defvar verilog-cache-enabled t
7950 "If true, enable caching of signals, etc. Set to nil for debugging to make things SLOW!")
7951
7952 (defvar verilog-modi-cache-list nil
7953 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
7954 For speeding up verilog-modi-get-* commands.
7955 Buffer-local.")
7956
7957 (make-variable-buffer-local 'verilog-modi-cache-list)
7958
7959 (defvar verilog-modi-cache-preserve-tick nil
7960 "Modification tick after which the cache is still considered valid.
7961 Use `verilog-preserve-modi-cache' to set it.")
7962 (defvar verilog-modi-cache-preserve-buffer nil
7963 "Modification tick after which the cache is still considered valid.
7964 Use `verilog-preserve-modi-cache' to set it.")
7965
7966 (defun verilog-modi-current ()
7967 "Return the modi structure for the module currently at point."
7968 (let* (name pt)
7969 ;; read current module's name
7970 (save-excursion
7971 (verilog-re-search-backward-quick verilog-defun-re nil nil)
7972 (verilog-re-search-forward-quick "(" nil nil)
7973 (setq name (verilog-read-module-name))
7974 (setq pt (point)))
7975 ;; return
7976 (vector name (or (buffer-file-name) (current-buffer)) pt)))
7977
7978 (defvar verilog-modi-lookup-last-mod nil "Cache of last module looked up.")
7979 (defvar verilog-modi-lookup-last-modi nil "Cache of last modi returned.")
7980 (defvar verilog-modi-lookup-last-current nil "Cache of last `current-buffer' looked up.")
7981 (defvar verilog-modi-lookup-last-tick nil "Cache of last `buffer-modified-tick' looked up.")
7982
7983 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
7984 "Find the file and point at which MODULE is defined.
7985 If ALLOW-CACHE is set, check and remember cache of previous lookups.
7986 Return modi if successful, else print message unless IGNORE-ERROR is true."
7987 (let* ((current (or (buffer-file-name) (current-buffer))))
7988 (cond ((and verilog-modi-lookup-last-modi
7989 verilog-cache-enabled
7990 allow-cache
7991 (equal verilog-modi-lookup-last-mod module)
7992 (equal verilog-modi-lookup-last-current current)
7993 (equal verilog-modi-lookup-last-tick (buffer-modified-tick)))
7994 ;; ok as is
7995 )
7996 (t (let* ((realmod (verilog-symbol-detick module t))
7997 (orig-filenames (verilog-module-filenames realmod current))
7998 (filenames orig-filenames)
7999 pt)
8000 (while (and filenames (not pt))
8001 (if (not (setq pt (verilog-module-inside-filename-p realmod (car filenames))))
8002 (setq filenames (cdr filenames))))
8003 (cond (pt (setq verilog-modi-lookup-last-modi
8004 (vector realmod (car filenames) pt)))
8005 (t (setq verilog-modi-lookup-last-modi nil)
8006 (or ignore-error
8007 (error (concat (verilog-point-text)
8008 ": Can't locate " module " module definition"
8009 (if (not (equal module realmod))
8010 (concat " (Expanded macro to " realmod ")")
8011 "")
8012 "\n Check the verilog-library-directories variable."
8013 "\n I looked in (if not listed, doesn't exist):\n\t"
8014 (mapconcat 'concat orig-filenames "\n\t"))))))
8015 (setq verilog-modi-lookup-last-mod module
8016 verilog-modi-lookup-last-current current
8017 verilog-modi-lookup-last-tick (buffer-modified-tick)))))
8018 verilog-modi-lookup-last-modi))
8019
8020 (defsubst verilog-modi-name (modi)
8021 (aref modi 0))
8022 (defsubst verilog-modi-file-or-buffer (modi)
8023 (aref modi 1))
8024 (defsubst verilog-modi-point (modi)
8025 (aref modi 2))
8026
8027 (defun verilog-modi-filename (modi)
8028 "Filename of MODI, or name of buffer if it's never been saved."
8029 (if (bufferp (verilog-modi-file-or-buffer modi))
8030 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
8031 (buffer-name (verilog-modi-file-or-buffer modi)))
8032 (verilog-modi-file-or-buffer modi)))
8033
8034 (defun verilog-modi-goto (modi)
8035 "Move point/buffer to specified MODI."
8036 (or modi (error "Passed unfound modi to goto, check earlier"))
8037 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
8038 (verilog-modi-file-or-buffer modi)
8039 (find-file-noselect (verilog-modi-file-or-buffer modi))))
8040 (or (equal major-mode `verilog-mode) ;; Put into Verilog mode to get syntax
8041 (verilog-mode))
8042 (goto-char (verilog-modi-point modi)))
8043
8044 (defun verilog-goto-defun-file (module)
8045 "Move point to the file at which a given MODULE is defined."
8046 (interactive "sGoto File for Module: ")
8047 (let* ((modi (verilog-modi-lookup module nil)))
8048 (when modi
8049 (verilog-modi-goto modi)
8050 (switch-to-buffer (current-buffer)))))
8051
8052 (defun verilog-modi-cache-results (modi function)
8053 "Run on MODI the given FUNCTION. Locate the module in a file.
8054 Cache the output of function so next call may have faster access."
8055 (let (fass)
8056 (save-excursion ;; Cache is buffer-local so can't avoid this.
8057 (verilog-modi-goto modi)
8058 (if (and (setq fass (assoc (list modi function)
8059 verilog-modi-cache-list))
8060 ;; Destroy caching when incorrect; Modified or file changed
8061 (not (and verilog-cache-enabled
8062 (or (equal (buffer-modified-tick) (nth 1 fass))
8063 (and verilog-modi-cache-preserve-tick
8064 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
8065 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
8066 (equal (visited-file-modtime) (nth 2 fass)))))
8067 (setq verilog-modi-cache-list nil
8068 fass nil))
8069 (cond (fass
8070 ;; Return data from cache hit
8071 (nth 3 fass))
8072 (t
8073 ;; Read from file
8074 ;; Clear then restore any highlighting to make emacs19 happy
8075 (let ((fontlocked (when (and (boundp 'font-lock-mode)
8076 font-lock-mode)
8077 (font-lock-mode 0)
8078 t))
8079 func-returns)
8080 (setq func-returns (funcall function))
8081 (when fontlocked (font-lock-mode t))
8082 ;; Cache for next time
8083 (setq verilog-modi-cache-list
8084 (cons (list (list modi function)
8085 (buffer-modified-tick)
8086 (visited-file-modtime)
8087 func-returns)
8088 verilog-modi-cache-list))
8089 func-returns))))))
8090
8091 (defun verilog-modi-cache-add (modi function element sig-list)
8092 "Add function return results to the module cache.
8093 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
8094 function now contains the additional SIG-LIST parameters."
8095 (let (fass)
8096 (save-excursion
8097 (verilog-modi-goto modi)
8098 (if (setq fass (assoc (list modi function)
8099 verilog-modi-cache-list))
8100 (let ((func-returns (nth 3 fass)))
8101 (aset func-returns element
8102 (append sig-list (aref func-returns element))))))))
8103
8104 (defmacro verilog-preserve-modi-cache (&rest body)
8105 "Execute the BODY forms, allowing cache preservation within BODY.
8106 This means that changes to the buffer will not result in the cache being
8107 flushed. If the changes affect the modsig state, they must call the
8108 modsig-cache-add-* function, else the results of later calls may be
8109 incorrect. Without this, changes are assumed to be adding/removing signals
8110 and invalidating the cache."
8111 `(let ((verilog-modi-cache-preserve-tick (buffer-modified-tick))
8112 (verilog-modi-cache-preserve-buffer (current-buffer)))
8113 (progn ,@body)))
8114
8115
8116 (defun verilog-signals-matching-enum (in-list enum)
8117 "Return all signals in IN-LIST matching the given ENUM."
8118 (let (out-list)
8119 (while in-list
8120 (if (equal (verilog-sig-enum (car in-list)) enum)
8121 (setq out-list (cons (car in-list) out-list)))
8122 (setq in-list (cdr in-list)))
8123 ;; New scheme
8124 (let* ((enumvar (intern (concat "venum-" enum)))
8125 (enumlist (and (boundp enumvar) (eval enumvar))))
8126 (while enumlist
8127 (add-to-list 'out-list (list (car enumlist)))
8128 (setq enumlist (cdr enumlist))))
8129 (nreverse out-list)))
8130
8131 (defun verilog-signals-matching-regexp (in-list regexp)
8132 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
8133 (if (or (not regexp) (equal regexp ""))
8134 in-list
8135 (let (out-list)
8136 (while in-list
8137 (if (string-match regexp (verilog-sig-name (car in-list)))
8138 (setq out-list (cons (car in-list) out-list)))
8139 (setq in-list (cdr in-list)))
8140 (nreverse out-list))))
8141
8142 (defun verilog-signals-not-matching-regexp (in-list regexp)
8143 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
8144 (if (or (not regexp) (equal regexp ""))
8145 in-list
8146 (let (out-list)
8147 (while in-list
8148 (if (not (string-match regexp (verilog-sig-name (car in-list))))
8149 (setq out-list (cons (car in-list) out-list)))
8150 (setq in-list (cdr in-list)))
8151 (nreverse out-list))))
8152
8153 (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
8154 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
8155 if non-nil."
8156 (if (or (not regexp) (equal regexp ""))
8157 in-list
8158 (let (out-list to-match)
8159 (while in-list
8160 ;; Note verilog-insert-one-definition matches on this order
8161 (setq to-match (concat
8162 decl-type
8163 " " (verilog-sig-signed (car in-list))
8164 " " (verilog-sig-multidim (car in-list))
8165 (verilog-sig-bits (car in-list))))
8166 (if (string-match regexp to-match)
8167 (setq out-list (cons (car in-list) out-list)))
8168 (setq in-list (cdr in-list)))
8169 (nreverse out-list))))
8170
8171 ;; Combined
8172 (defun verilog-decls-get-signals (decls)
8173 (append
8174 (verilog-decls-get-outputs decls)
8175 (verilog-decls-get-inouts decls)
8176 (verilog-decls-get-inputs decls)
8177 (verilog-decls-get-wires decls)
8178 (verilog-decls-get-regs decls)
8179 (verilog-decls-get-assigns decls)
8180 (verilog-decls-get-consts decls)
8181 (verilog-decls-get-gparams decls)))
8182
8183 (defun verilog-decls-get-ports (decls)
8184 (append
8185 (verilog-decls-get-outputs decls)
8186 (verilog-decls-get-inouts decls)
8187 (verilog-decls-get-inputs decls)))
8188
8189 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
8190 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
8191 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
8192 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
8193 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
8194 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
8195 (defsubst verilog-modi-cache-add-wires (modi sig-list)
8196 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
8197 (defsubst verilog-modi-cache-add-regs (modi sig-list)
8198 (verilog-modi-cache-add modi 'verilog-read-decls 4 sig-list))
8199
8200 (defun verilog-signals-from-signame (signame-list)
8201 "Return signals in standard form from SIGNAME-LIST, a simple list of signal names."
8202 (mapcar (function (lambda (name) (list name nil nil)))
8203 signame-list))
8204 \f
8205 ;;
8206 ;; Auto creation utilities
8207 ;;
8208
8209 (defun verilog-auto-re-search-do (search-for func)
8210 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
8211 (goto-char (point-min))
8212 (while (verilog-re-search-forward search-for nil t)
8213 (funcall func)))
8214
8215 (defun verilog-insert-one-definition (sig type indent-pt)
8216 "Print out a definition for SIG of the given TYPE,
8217 with appropriate INDENT-PT indentation."
8218 (indent-to indent-pt)
8219 ;; Note verilog-signals-matching-dir-re matches on this order
8220 (insert type)
8221 (when (verilog-sig-modport sig)
8222 (insert "." (verilog-sig-modport sig)))
8223 (when (verilog-sig-signed sig)
8224 (insert " " (verilog-sig-signed sig)))
8225 (when (verilog-sig-multidim sig)
8226 (insert " " (verilog-sig-multidim-string sig)))
8227 (when (verilog-sig-bits sig)
8228 (insert " " (verilog-sig-bits sig)))
8229 (indent-to (max 24 (+ indent-pt 16)))
8230 (unless (= (char-syntax (preceding-char)) ?\ )
8231 (insert " ")) ; Need space between "]name" if indent-to did nothing
8232 (insert (verilog-sig-name sig)))
8233
8234 (defun verilog-insert-definition (sigs direction indent-pt v2k &optional dont-sort)
8235 "Print out a definition for a list of SIGS of the given DIRECTION,
8236 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
8237 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output."
8238 (or dont-sort
8239 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
8240 (while sigs
8241 (let ((sig (car sigs)))
8242 (verilog-insert-one-definition
8243 sig
8244 ;; Want "type x" or "output type x", not "wire type x"
8245 (cond ((verilog-sig-type sig)
8246 (concat
8247 (if (not (member direction '("wire" "interface")))
8248 (concat direction " "))
8249 (verilog-sig-type sig)))
8250 (t direction))
8251 indent-pt)
8252 (insert (if v2k "," ";"))
8253 (if (or (not (verilog-sig-comment sig))
8254 (equal "" (verilog-sig-comment sig)))
8255 (insert "\n")
8256 (indent-to (max 48 (+ indent-pt 40)))
8257 (insert (concat "// " (verilog-sig-comment sig) "\n")))
8258 (setq sigs (cdr sigs)))))
8259
8260 (eval-when-compile
8261 (if (not (boundp 'indent-pt))
8262 (defvar indent-pt nil "Local used by insert-indent")))
8263
8264 (defun verilog-insert-indent (&rest stuff)
8265 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
8266 Presumes that any newlines end a list element."
8267 (let ((need-indent t))
8268 (while stuff
8269 (if need-indent (indent-to indent-pt))
8270 (setq need-indent nil)
8271 (insert (car stuff))
8272 (setq need-indent (string-match "\n$" (car stuff))
8273 stuff (cdr stuff)))))
8274 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
8275
8276 (defun verilog-repair-open-comma ()
8277 "Insert comma if previous argument is other than a open parenthesis or endif."
8278 ;; We can't just search backward for ) as it might be inside another expression.
8279 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
8280 (save-excursion
8281 (verilog-backward-syntactic-ws)
8282 (when (and (not (save-excursion ;; Not beginning (, or existing ,
8283 (backward-char 1)
8284 (looking-at "[(,]")))
8285 (not (save-excursion ;; Not `endif, or user define
8286 (backward-char 1)
8287 (skip-chars-backward "[a-zA-Z0-9_`]")
8288 (looking-at "`"))))
8289 (insert ","))))
8290
8291 (defun verilog-repair-close-comma ()
8292 "If point is at a comma followed by a close parenthesis, fix it.
8293 This repairs those mis-inserted by a AUTOARG."
8294 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
8295 (save-excursion
8296 (verilog-forward-close-paren)
8297 (backward-char 1)
8298 (verilog-backward-syntactic-ws)
8299 (backward-char 1)
8300 (when (looking-at ",")
8301 (delete-char 1))))
8302
8303 (defun verilog-get-list (start end)
8304 "Return the elements of a comma separated list between START and END."
8305 (interactive)
8306 (let ((my-list (list))
8307 my-string)
8308 (save-excursion
8309 (while (< (point) end)
8310 (when (re-search-forward "\\([^,{]+\\)" end t)
8311 (setq my-string (verilog-string-remove-spaces (match-string 1)))
8312 (setq my-list (nconc my-list (list my-string) ))
8313 (goto-char (match-end 0))))
8314 my-list)))
8315
8316 (defun verilog-make-width-expression (range-exp)
8317 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
8318 ;; strip off the []
8319 (cond ((not range-exp)
8320 "1")
8321 (t
8322 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
8323 (setq range-exp (match-string 1 range-exp)))
8324 (cond ((not range-exp)
8325 "1")
8326 ;; [#:#] We can compute a numeric result
8327 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
8328 range-exp)
8329 (int-to-string
8330 (1+ (abs (- (string-to-number (match-string 1 range-exp))
8331 (string-to-number (match-string 2 range-exp)))))))
8332 ;; [PARAM-1:0] can just return PARAM
8333 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
8334 (match-string 1 range-exp))
8335 ;; [arbitrary] need math
8336 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
8337 (concat "(1+(" (match-string 1 range-exp) ")"
8338 (if (equal "0" (match-string 2 range-exp))
8339 "" ;; Don't bother with -(0)
8340 (concat "-(" (match-string 2 range-exp) ")"))
8341 ")"))
8342 (t nil)))))
8343 ;;(verilog-make-width-expression "`A:`B")
8344
8345 (defun verilog-simplify-range-expression (range-exp)
8346 "Return a simplified range expression with constants eliminated from RANGE-EXP."
8347 (let ((out range-exp)
8348 (last-pass ""))
8349 (while (not (equal last-pass out))
8350 (setq last-pass out)
8351 (while (string-match "(\\<\\([0-9A-Z-az_]+\\)\\>)" out)
8352 (setq out (replace-match "\\1" nil nil out)))
8353 (while (string-match "\\<\\([0-9]+\\)\\>\\s *\\+\\s *\\<\\([0-9]+\\)\\>" out)
8354 (setq out (replace-match
8355 (int-to-string (+ (string-to-number (match-string 1 out))
8356 (string-to-number (match-string 2 out))))
8357 nil nil out)))
8358 (while (string-match "\\<\\([0-9]+\\)\\>\\s *\\-\\s *\\<\\([0-9]+\\)\\>" out)
8359 (setq out (replace-match
8360 (int-to-string (- (string-to-number (match-string 1 out))
8361 (string-to-number (match-string 2 out))))
8362 nil nil out))))
8363 out))
8364 ;;(verilog-simplify-range-expression "1")
8365 ;;(verilog-simplify-range-expression "(((16)+1)-3)")
8366
8367 (defun verilog-typedef-name-p (variable-name)
8368 "Return true if the VARIABLE-NAME is a type definition."
8369 (when verilog-typedef-regexp
8370 (string-match verilog-typedef-regexp variable-name)))
8371 \f
8372 ;;
8373 ;; Auto deletion
8374 ;;
8375
8376 (defun verilog-delete-autos-lined ()
8377 "Delete autos that occupy multiple lines, between begin and end comments."
8378 (let ((pt (point)))
8379 (forward-line 1)
8380 (when (and
8381 (looking-at "\\s-*// Beginning")
8382 (search-forward "// End of automatic" nil t))
8383 ;; End exists
8384 (end-of-line)
8385 (delete-region pt (point))
8386 (forward-line 1))))
8387
8388 (defun verilog-delete-empty-auto-pair ()
8389 "Delete begin/end auto pair at point, if empty."
8390 (forward-line 0)
8391 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
8392 "\\s-*// End of automatics\n"))
8393 (delete-region (point) (save-excursion (forward-line 2) (point)))))
8394
8395 (defun verilog-forward-close-paren ()
8396 "Find the close parenthesis that match the current point.
8397 Ignore other close parenthesis with matching open parens."
8398 (let ((parens 1))
8399 (while (> parens 0)
8400 (unless (verilog-re-search-forward-quick "[()]" nil t)
8401 (error "%s: Mismatching ()" (verilog-point-text)))
8402 (cond ((= (preceding-char) ?\( )
8403 (setq parens (1+ parens)))
8404 ((= (preceding-char) ?\) )
8405 (setq parens (1- parens)))))))
8406
8407 (defun verilog-backward-open-paren ()
8408 "Find the open parenthesis that match the current point.
8409 Ignore other open parenthesis with matching close parens."
8410 (let ((parens 1))
8411 (while (> parens 0)
8412 (unless (verilog-re-search-backward-quick "[()]" nil t)
8413 (error "%s: Mismatching ()" (verilog-point-text)))
8414 (cond ((= (following-char) ?\) )
8415 (setq parens (1+ parens)))
8416 ((= (following-char) ?\( )
8417 (setq parens (1- parens)))))))
8418
8419 (defun verilog-backward-open-bracket ()
8420 "Find the open bracket that match the current point.
8421 Ignore other open bracket with matching close bracket."
8422 (let ((parens 1))
8423 (while (> parens 0)
8424 (unless (verilog-re-search-backward-quick "[][]" nil t)
8425 (error "%s: Mismatching []" (verilog-point-text)))
8426 (cond ((= (following-char) ?\] )
8427 (setq parens (1+ parens)))
8428 ((= (following-char) ?\[ )
8429 (setq parens (1- parens)))))))
8430
8431 (defun verilog-delete-to-paren ()
8432 "Delete the automatic inst/sense/arg created by autos.
8433 Deletion stops at the matching end parenthesis."
8434 (delete-region (point)
8435 (save-excursion
8436 (verilog-backward-open-paren)
8437 (forward-sexp 1) ;; Moves to paren that closes argdecl's
8438 (backward-char 1)
8439 (point))))
8440
8441 (defun verilog-auto-star-safe ()
8442 "Return if a .* AUTOINST is safe to delete or expand.
8443 It was created by the AUTOS themselves, or by the user."
8444 (and verilog-auto-star-expand
8445 (looking-at "[ \t\n\f,]*\\([)]\\|// \\(Outputs\\|Inouts\\|Inputs\\|Interfaces\\)\\)")))
8446
8447 (defun verilog-delete-auto-star-all ()
8448 "Delete a .* AUTOINST, if it is safe."
8449 (when (verilog-auto-star-safe)
8450 (verilog-delete-to-paren)))
8451
8452 (defun verilog-delete-auto-star-implicit ()
8453 "Delete all .* implicit connections created by `verilog-auto-star'.
8454 This function will be called automatically at save unless
8455 `verilog-auto-star-save' is set, any non-templated expanded pins will be
8456 removed."
8457 (interactive)
8458 (let (paren-pt indent have-close-paren)
8459 (save-excursion
8460 (goto-char (point-min))
8461 ;; We need to match these even outside of comments.
8462 ;; For reasonable performance, we don't check if inside comments, sorry.
8463 (while (re-search-forward "// Implicit \\.\\*" nil t)
8464 (setq paren-pt (point))
8465 (beginning-of-line)
8466 (setq have-close-paren
8467 (save-excursion
8468 (when (search-forward ");" paren-pt t)
8469 (setq indent (current-indentation))
8470 t)))
8471 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
8472 (when have-close-paren
8473 ;; Delete extra commentary
8474 (save-excursion
8475 (while (progn
8476 (forward-line -1)
8477 (looking-at "\\s *//\\s *\\(Outputs\\|Inouts\\|Inputs\\|Interfaces\\)\n"))
8478 (delete-region (match-beginning 0) (match-end 0))))
8479 ;; If it is simple, we can put the ); on the same line as the last text
8480 (let ((rtn-pt (point)))
8481 (save-excursion
8482 (while (progn (backward-char 1)
8483 (looking-at "[ \t\n\f]")))
8484 (when (looking-at ",")
8485 (delete-region (+ 1 (point)) rtn-pt))))
8486 (when (bolp)
8487 (indent-to indent))
8488 (insert ");\n")
8489 ;; Still need to kill final comma - always is one as we put one after the .*
8490 (re-search-backward ",")
8491 (delete-char 1))))))
8492
8493 (defun verilog-delete-auto ()
8494 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
8495 Use \\[verilog-auto] to re-insert the updated AUTOs.
8496
8497 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
8498 called before and after this function, respectively."
8499 (interactive)
8500 (save-excursion
8501 (if (buffer-file-name)
8502 (find-file-noselect (buffer-file-name))) ;; To check we have latest version
8503 ;; Allow user to customize
8504 (run-hooks 'verilog-before-delete-auto-hook)
8505
8506 ;; Remove those that have multi-line insertions, possibly with parameters
8507 (verilog-auto-re-search-do
8508 (concat "/\\*"
8509 (eval-when-compile
8510 (verilog-regexp-words
8511 `("AUTOASCIIENUM" "AUTOCONCATCOMMENT" "AUTODEFINEVALUE"
8512 "AUTOINOUT" "AUTOINOUTCOMP" "AUTOINOUTMODULE"
8513 "AUTOINPUT" "AUTOINSERTLISP" "AUTOOUTPUT" "AUTOOUTPUTEVERY"
8514 "AUTOREG" "AUTOREGINPUT" "AUTORESET" "AUTOTIEOFF"
8515 "AUTOUNUSED" "AUTOWIRE")))
8516 ;; Optional parens or quoted parameter or .* for (((...)))
8517 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
8518 "\\*/")
8519 'verilog-delete-autos-lined)
8520 ;; Remove those that are in parenthesis
8521 (verilog-auto-re-search-do
8522 (concat "/\\*"
8523 (eval-when-compile
8524 (verilog-regexp-words
8525 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
8526 "AUTOSENSE")))
8527 "\\*/")
8528 'verilog-delete-to-paren)
8529 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
8530 (verilog-auto-re-search-do "\\.\\*"
8531 'verilog-delete-auto-star-all)
8532 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
8533 (goto-char (point-min))
8534 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)[ \tLT0-9]*$" nil t)
8535 (replace-match ""))
8536
8537 ;; Final customize
8538 (run-hooks 'verilog-delete-auto-hook)))
8539 \f
8540 ;;
8541 ;; Auto inject
8542 ;;
8543
8544 (defun verilog-inject-auto ()
8545 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
8546
8547 Any always @ blocks with sensitivity lists that match computed lists will
8548 be replaced with /*AS*/ comments.
8549
8550 Any cells will get /*AUTOINST*/ added to the end of the pin list.
8551 Pins with have identical names will be deleted.
8552
8553 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
8554 support adding new ports. You may wish to delete older ports yourself.
8555
8556 For example:
8557
8558 module ExampInject (i, o);
8559 input i;
8560 input j;
8561 output o;
8562 always @ (i or j)
8563 o = i | j;
8564 InstModule instName
8565 (.foobar(baz),
8566 j(j));
8567 endmodule
8568
8569 Typing \\[verilog-inject-auto] will make this into:
8570
8571 module ExampInject (i, o/*AUTOARG*/
8572 // Inputs
8573 j);
8574 input i;
8575 output o;
8576 always @ (/*AS*/i or j)
8577 o = i | j;
8578 InstModule instName
8579 (.foobar(baz),
8580 /*AUTOINST*/
8581 // Outputs
8582 j(j));
8583 endmodule"
8584 (interactive)
8585 (verilog-auto t))
8586
8587 (defun verilog-inject-arg ()
8588 "Inject AUTOARG into new code. See `verilog-inject-auto'."
8589 ;; Presume one module per file.
8590 (save-excursion
8591 (goto-char (point-min))
8592 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
8593 (let ((endmodp (save-excursion
8594 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
8595 (point))))
8596 ;; See if there's already a comment .. inside a comment so not verilog-re-search
8597 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
8598 (verilog-re-search-forward-quick ";" nil t)
8599 (backward-char 1)
8600 (verilog-backward-syntactic-ws)
8601 (backward-char 1) ; Moves to paren that closes argdecl's
8602 (when (looking-at ")")
8603 (insert "/*AUTOARG*/")))))))
8604
8605 (defun verilog-inject-sense ()
8606 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
8607 (save-excursion
8608 (goto-char (point-min))
8609 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
8610 (let* ((start-pt (point))
8611 (modi (verilog-modi-current))
8612 (moddecls (verilog-modi-get-decls modi))
8613 pre-sigs
8614 got-sigs)
8615 (backward-char 1)
8616 (forward-sexp 1)
8617 (backward-char 1) ;; End )
8618 (when (not (verilog-re-search-backward "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
8619 (setq pre-sigs (verilog-signals-from-signame
8620 (verilog-read-signals start-pt (point)))
8621 got-sigs (verilog-auto-sense-sigs moddecls nil))
8622 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
8623 (verilog-signals-not-in got-sigs pre-sigs)))
8624 (delete-region start-pt (point))
8625 (insert "/*AS*/")))))))
8626
8627 (defun verilog-inject-inst ()
8628 "Inject AUTOINST into new code. See `verilog-inject-auto'."
8629 (save-excursion
8630 (goto-char (point-min))
8631 ;; It's hard to distinguish modules; we'll instead search for pins.
8632 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_\$]+\\s *(\\s *[a-zA-Z0-9`_\$]+\\s *)" nil t)
8633 (verilog-backward-open-paren) ;; Inst start
8634 (cond
8635 ((= (preceding-char) ?\#) ;; #(...) parameter section, not pin. Skip.
8636 (forward-char 1)
8637 (verilog-forward-close-paren)) ;; Parameters done
8638 (t
8639 (forward-char 1)
8640 (let ((indent-pt (+ (current-column)))
8641 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
8642 (cond ((verilog-re-search-forward "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
8643 (goto-char end-pt)) ;; Already there, continue search with next instance
8644 (t
8645 ;; Delete identical interconnect
8646 (let ((case-fold-search nil)) ;; So we don't convert upper-to-lower, etc
8647 (while (verilog-re-search-forward "\\.\\s *\\([a-zA-Z0-9`_\$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
8648 (delete-region (match-beginning 0) (match-end 0))
8649 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ;; Keep it correct
8650 (while (or (looking-at "[ \t\n\f,]+")
8651 (looking-at "//[^\n]*"))
8652 (delete-region (match-beginning 0) (match-end 0))
8653 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
8654 (verilog-forward-close-paren)
8655 (backward-char 1)
8656 ;; Not verilog-re-search, as we don't want to strip comments
8657 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
8658 (delete-region (match-beginning 0) (match-end 0)))
8659 (insert "\n")
8660 (indent-to indent-pt)
8661 (insert "/*AUTOINST*/")))))))))
8662 \f
8663 ;;
8664 ;; Auto save
8665 ;;
8666
8667 (defun verilog-auto-save-check ()
8668 "On saving see if we need auto update."
8669 (cond ((not verilog-auto-save-policy)) ; disabled
8670 ((not (save-excursion
8671 (save-match-data
8672 (let ((case-fold-search nil))
8673 (goto-char (point-min))
8674 (re-search-forward "AUTO" nil t))))))
8675 ((eq verilog-auto-save-policy 'force)
8676 (verilog-auto))
8677 ((not (buffer-modified-p)))
8678 ((eq verilog-auto-update-tick (buffer-modified-tick))) ; up-to-date
8679 ((eq verilog-auto-save-policy 'detect)
8680 (verilog-auto))
8681 (t
8682 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
8683 (verilog-auto))
8684 ;; Don't ask again if didn't update
8685 (set (make-local-variable 'verilog-auto-update-tick) (buffer-modified-tick))))
8686 (when (not verilog-auto-star-save)
8687 (verilog-delete-auto-star-implicit))
8688 nil) ;; Always return nil -- we don't write the file ourselves
8689
8690 (defun verilog-auto-read-locals ()
8691 "Return file local variable segment at bottom of file."
8692 (save-excursion
8693 (goto-char (point-max))
8694 (if (re-search-backward "Local Variables:" nil t)
8695 (buffer-substring-no-properties (point) (point-max))
8696 "")))
8697
8698 (defun verilog-auto-reeval-locals (&optional force)
8699 "Read file local variable segment at bottom of file if it has changed.
8700 If FORCE, always reread it."
8701 (make-local-variable 'verilog-auto-last-file-locals)
8702 (let ((curlocal (verilog-auto-read-locals)))
8703 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
8704 (setq verilog-auto-last-file-locals curlocal)
8705 ;; Note this may cause this function to be recursively invoked,
8706 ;; because hack-local-variables may call (verilog-mode)
8707 ;; The above when statement will prevent it from recursing forever.
8708 (hack-local-variables)
8709 t)))
8710 \f
8711 ;;
8712 ;; Auto creation
8713 ;;
8714
8715 (defun verilog-auto-arg-ports (sigs message indent-pt)
8716 "Print a list of ports for a AUTOINST.
8717 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
8718 (when sigs
8719 (when verilog-auto-arg-sort
8720 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
8721 (insert "\n")
8722 (indent-to indent-pt)
8723 (insert message)
8724 (insert "\n")
8725 (let ((space ""))
8726 (indent-to indent-pt)
8727 (while sigs
8728 (cond ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
8729 (insert "\n")
8730 (indent-to indent-pt))
8731 (t (insert space)))
8732 (insert (verilog-sig-name (car sigs)) ",")
8733 (setq sigs (cdr sigs)
8734 space " ")))))
8735
8736 (defun verilog-auto-arg ()
8737 "Expand AUTOARG statements.
8738 Replace the argument declarations at the beginning of the
8739 module with ones automatically derived from input and output
8740 statements. This can be dangerous if the module is instantiated
8741 using position-based connections, so use only name-based when
8742 instantiating the resulting module. Long lines are split based
8743 on the `fill-column', see \\[set-fill-column].
8744
8745 Limitations:
8746 Concatenation and outputting partial busses is not supported.
8747
8748 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
8749
8750 For example:
8751
8752 module ExampArg (/*AUTOARG*/);
8753 input i;
8754 output o;
8755 endmodule
8756
8757 Typing \\[verilog-auto] will make this into:
8758
8759 module ExampArg (/*AUTOARG*/
8760 // Outputs
8761 o,
8762 // Inputs
8763 i
8764 );
8765 input i;
8766 output o;
8767 endmodule
8768
8769 The argument declarations may be printed in declaration order to best suit
8770 order based instantiations, or alphabetically, based on the
8771 `verilog-auto-arg-sort' variable.
8772
8773 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
8774 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
8775 conservative guess on adding a comma for the first signal, if you have
8776 any ifdefs or complicated expressions before the AUTOARG you will need
8777 to choose the comma yourself.
8778
8779 Avoid declaring ports manually, as it makes code harder to maintain."
8780 (save-excursion
8781 (let* ((modi (verilog-modi-current))
8782 (moddecls (verilog-modi-get-decls modi))
8783 (skip-pins (aref (verilog-read-arg-pins) 0)))
8784 (verilog-repair-open-comma)
8785 (verilog-auto-arg-ports (verilog-signals-not-in
8786 (verilog-decls-get-outputs moddecls)
8787 skip-pins)
8788 "// Outputs"
8789 verilog-indent-level-declaration)
8790 (verilog-auto-arg-ports (verilog-signals-not-in
8791 (verilog-decls-get-inouts moddecls)
8792 skip-pins)
8793 "// Inouts"
8794 verilog-indent-level-declaration)
8795 (verilog-auto-arg-ports (verilog-signals-not-in
8796 (verilog-decls-get-inputs moddecls)
8797 skip-pins)
8798 "// Inputs"
8799 verilog-indent-level-declaration)
8800 (verilog-repair-close-comma)
8801 (unless (eq (char-before) ?/ )
8802 (insert "\n"))
8803 (indent-to verilog-indent-level-declaration))))
8804
8805 (defun verilog-auto-inst-port-map (port-st)
8806 nil)
8807
8808 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
8809 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
8810 (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
8811 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
8812 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
8813 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
8814 (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
8815 (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
8816
8817 (defun verilog-auto-inst-port (port-st indent-pt tpl-list tpl-num for-star par-values)
8818 "Print out a instantiation connection for this PORT-ST.
8819 Insert to INDENT-PT, use template TPL-LIST.
8820 @ are instantiation numbers, replaced with TPL-NUM.
8821 @\"(expression @)\" are evaluated, with @ as a variable.
8822 If FOR-STAR add comment it is a .* expansion.
8823 If PAR-VALUES replace final strings with these parameter values."
8824 (let* ((port (verilog-sig-name port-st))
8825 (tpl-ass (or (assoc port (car tpl-list))
8826 (verilog-auto-inst-port-map port-st)))
8827 ;; vl-* are documented for user use
8828 (vl-name (verilog-sig-name port-st))
8829 (vl-width (verilog-sig-width port-st))
8830 (vl-modport (verilog-sig-modport port-st))
8831 (vl-mbits (if (verilog-sig-multidim port-st)
8832 (verilog-sig-multidim-string port-st) ""))
8833 (vl-bits (if (or verilog-auto-inst-vector
8834 (not (assoc port vector-skip-list))
8835 (not (equal (verilog-sig-bits port-st)
8836 (verilog-sig-bits (assoc port vector-skip-list)))))
8837 (or (verilog-sig-bits port-st) "")
8838 ""))
8839 (case-fold-search nil)
8840 (check-values par-values)
8841 tpl-net)
8842 ;; Replace parameters in bit-width
8843 (when (and check-values
8844 (not (equal vl-bits "")))
8845 (while check-values
8846 (setq vl-bits (verilog-string-replace-matches
8847 (concat "\\<" (nth 0 (car check-values)) "\\>")
8848 (concat "(" (nth 1 (car check-values)) ")")
8849 t t vl-bits)
8850 check-values (cdr check-values)))
8851 (setq vl-bits (verilog-simplify-range-expression vl-bits))) ; Not in the loop for speed
8852 ;; Default net value if not found
8853 (setq tpl-net (concat port
8854 (if vl-modport (concat "." vl-modport) "")
8855 (if (verilog-sig-multidim port-st)
8856 (concat "/*" (verilog-sig-multidim-string port-st)
8857 vl-bits "*/")
8858 (concat vl-bits))))
8859 ;; Find template
8860 (cond (tpl-ass ; Template of exact port name
8861 (setq tpl-net (nth 1 tpl-ass)))
8862 ((nth 1 tpl-list) ; Wildcards in template, search them
8863 (let ((wildcards (nth 1 tpl-list)))
8864 (while wildcards
8865 (when (string-match (nth 0 (car wildcards)) port)
8866 (setq tpl-ass (car wildcards) ; so allow @ parsing
8867 tpl-net (replace-match (nth 1 (car wildcards))
8868 t nil port)))
8869 (setq wildcards (cdr wildcards))))))
8870 ;; Parse Templated variable
8871 (when tpl-ass
8872 ;; Evaluate @"(lispcode)"
8873 (when (string-match "@\".*[^\\]\"" tpl-net)
8874 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
8875 (setq tpl-net
8876 (concat
8877 (substring tpl-net 0 (match-beginning 0))
8878 (save-match-data
8879 (let* ((expr (match-string 1 tpl-net))
8880 (value
8881 (progn
8882 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
8883 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
8884 (prin1 (eval (car (read-from-string expr)))
8885 (lambda (ch) ())))))
8886 (if (numberp value) (setq value (number-to-string value)))
8887 value))
8888 (substring tpl-net (match-end 0))))))
8889 ;; Replace @ and [] magic variables in final output
8890 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
8891 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
8892 ;; Insert it
8893 (indent-to indent-pt)
8894 (insert "." port)
8895 (indent-to verilog-auto-inst-column)
8896 (insert "(" tpl-net "),")
8897 (cond (tpl-ass
8898 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
8899 verilog-auto-inst-column))
8900 (insert " // Templated")
8901 (when verilog-auto-inst-template-numbers
8902 (insert " T" (int-to-string (nth 2 tpl-ass))
8903 " L" (int-to-string (nth 3 tpl-ass)))))
8904 (for-star
8905 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
8906 verilog-auto-inst-column))
8907 (insert " // Implicit .\*"))) ;For some reason the . or * must be escaped...
8908 (insert "\n")))
8909 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
8910 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
8911 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
8912
8913 (defun verilog-auto-inst-first ()
8914 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
8915 ;; Do we need a trailing comma?
8916 ;; There maybe a ifdef or something similar before us. What a mess. Thus
8917 ;; to avoid trouble we only insert on preceding ) or *.
8918 ;; Insert first port on new line
8919 (insert "\n") ;; Must insert before search, so point will move forward if insert comma
8920 (save-excursion
8921 (verilog-re-search-backward "[^ \t\n\f]" nil nil)
8922 (when (looking-at ")\\|\\*") ;; Generally don't insert, unless we are fairly sure
8923 (forward-char 1)
8924 (insert ","))))
8925
8926 (defun verilog-auto-star ()
8927 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
8928
8929 If `verilog-auto-star-expand' is set, .* pins are treated if they were
8930 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
8931 will also ignore any .* that are not last in your pin list (this prevents
8932 it from deleting pins following the .* when it expands the AUTOINST.)
8933
8934 On writing your file, unless `verilog-auto-star-save' is set, any
8935 non-templated expanded pins will be removed. You may do this at any time
8936 with \\[verilog-delete-auto-star-implicit].
8937
8938 If you are converting a module to use .* for the first time, you may wish
8939 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
8940
8941 See `verilog-auto-inst' for examples, templates, and more information."
8942 (when (verilog-auto-star-safe)
8943 (verilog-auto-inst)))
8944
8945 (defun verilog-auto-inst ()
8946 "Expand AUTOINST statements, as part of \\[verilog-auto].
8947 Replace the pin connections to an instantiation with ones
8948 automatically derived from the module header of the instantiated netlist.
8949
8950 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
8951 and delete them before saving unless `verilog-auto-star-save' is set.
8952 See `verilog-auto-star' for more information.
8953
8954 Limitations:
8955 Module names must be resolvable to filenames by adding a
8956 `verilog-library-extensions', and being found in the same directory, or
8957 by changing the variable `verilog-library-flags' or
8958 `verilog-library-directories'. Macros `modname are translated through the
8959 vh-{name} Emacs variable, if that is not found, it just ignores the `.
8960
8961 In templates you must have one signal per line, ending in a ), or ));,
8962 and have proper () nesting, including a final ); to end the template.
8963
8964 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
8965
8966 SystemVerilog multidimensional input/output has only experimental support.
8967
8968 Parameters referenced by the instantiation will remain symbolic, unless
8969 `verilog-auto-inst-param-value' is set.
8970
8971 For example, first take the submodule InstModule.v:
8972
8973 module InstModule (o,i)
8974 output [31:0] o;
8975 input i;
8976 wire [31:0] o = {32{i}};
8977 endmodule
8978
8979 This is then used in a upper level module:
8980
8981 module ExampInst (o,i)
8982 output o;
8983 input i;
8984 InstModule instName
8985 (/*AUTOINST*/);
8986 endmodule
8987
8988 Typing \\[verilog-auto] will make this into:
8989
8990 module ExampInst (o,i)
8991 output o;
8992 input i;
8993 InstModule instName
8994 (/*AUTOINST*/
8995 // Outputs
8996 .ov (ov[31:0]),
8997 // Inputs
8998 .i (i));
8999 endmodule
9000
9001 Where the list of inputs and outputs came from the inst module.
9002 \f
9003 Exceptions:
9004
9005 Unless you are instantiating a module multiple times, or the module is
9006 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
9007 It just makes for unmaintainable code. To sanitize signal names, try
9008 vrename from URL `http://www.veripool.org'.
9009
9010 When you need to violate this suggestion there are two ways to list
9011 exceptions, placing them before the AUTOINST, or using templates.
9012
9013 Any ports defined before the /*AUTOINST*/ are not included in the list of
9014 automatics. This is similar to making a template as described below, but
9015 is restricted to simple connections just like you normally make. Also note
9016 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
9017 you have the appropriate // Input or // Output comment, and exactly the
9018 same line formatting as AUTOINST itself uses.
9019
9020 InstModule instName
9021 (// Inputs
9022 .i (my_i_dont_mess_with_it),
9023 /*AUTOINST*/
9024 // Outputs
9025 .ov (ov[31:0]));
9026
9027 \f
9028 Templates:
9029
9030 For multiple instantiations based upon a single template, create a
9031 commented out template:
9032
9033 /* InstModule AUTO_TEMPLATE (
9034 .sig3 (sigz[]),
9035 );
9036 */
9037
9038 Templates go ABOVE the instantiation(s). When an instantiation is
9039 expanded `verilog-mode' simply searches up for the closest template.
9040 Thus you can have multiple templates for the same module, just alternate
9041 between the template for an instantiation and the instantiation itself.
9042
9043 The module name must be the same as the name of the module in the
9044 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
9045 words and capitalized. Only signals that must be different for each
9046 instantiation need to be listed.
9047
9048 Inside a template, a [] in a connection name (with nothing else inside
9049 the brackets) will be replaced by the same bus subscript as it is being
9050 connected to, or the [] will be removed if it is a single bit signal.
9051 Generally it is a good idea to do this for all connections in a template,
9052 as then they will work for any width signal, and with AUTOWIRE. See
9053 PTL_BUS becoming PTL_BUSNEW below.
9054
9055 If you have a complicated template, set `verilog-auto-inst-template-numbers'
9056 to see which regexps are matching. Don't leave that mode set after
9057 debugging is completed though, it will result in lots of extra differences
9058 and merge conflicts.
9059
9060 For example:
9061
9062 /* InstModule AUTO_TEMPLATE (
9063 .ptl_bus (ptl_busnew[]),
9064 );
9065 */
9066 InstModule ms2m (/*AUTOINST*/);
9067
9068 Typing \\[verilog-auto] will make this into:
9069
9070 InstModule ms2m (/*AUTOINST*/
9071 // Outputs
9072 .NotInTemplate (NotInTemplate),
9073 .ptl_bus (ptl_busnew[3:0]), // Templated
9074 ....
9075 \f
9076 @ Templates:
9077
9078 It is common to instantiate a cell multiple times, so templates make it
9079 trivial to substitute part of the cell name into the connection name.
9080
9081 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
9082 .sig1 (sigx[@]),
9083 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
9084 );
9085 */
9086
9087 If no regular expression is provided immediately after the AUTO_TEMPLATE
9088 keyword, then the @ character in any connection names will be replaced
9089 with the instantiation number; the first digits found in the cell's
9090 instantiation name.
9091
9092 If a regular expression is provided, the @ character will be replaced
9093 with the first \(\) grouping that matches against the cell name. Using a
9094 regexp of \"\\([0-9]+\\)\" provides identical values for @ as when no
9095 regexp is provided. If you use multiple layers of parenthesis,
9096 \"test\\([^0-9]+\\)_\\([0-9]+\\)\" would replace @ with non-number
9097 characters after test and before _, whereas
9098 \"\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)\" would replace @ with the entire
9099 match.
9100
9101 For example:
9102
9103 /* InstModule AUTO_TEMPLATE (
9104 .ptl_mapvalidx (ptl_mapvalid[@]),
9105 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
9106 );
9107 */
9108 InstModule ms2m (/*AUTOINST*/);
9109
9110 Typing \\[verilog-auto] will make this into:
9111
9112 InstModule ms2m (/*AUTOINST*/
9113 // Outputs
9114 .ptl_mapvalidx (ptl_mapvalid[2]),
9115 .ptl_mapvalidp1x (ptl_mapvalid[3]));
9116
9117 Note the @ character was replaced with the 2 from \"ms2m\".
9118
9119 Alternatively, using a regular expression for @:
9120
9121 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
9122 .ptl_mapvalidx (@_ptl_mapvalid),
9123 .ptl_mapvalidp1x (ptl_mapvalid_@),
9124 );
9125 */
9126 InstModule ms2_FOO (/*AUTOINST*/);
9127 InstModule ms2_BAR (/*AUTOINST*/);
9128
9129 Typing \\[verilog-auto] will make this into:
9130
9131 InstModule ms2_FOO (/*AUTOINST*/
9132 // Outputs
9133 .ptl_mapvalidx (FOO_ptl_mapvalid),
9134 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
9135 InstModule ms2_BAR (/*AUTOINST*/
9136 // Outputs
9137 .ptl_mapvalidx (BAR_ptl_mapvalid),
9138 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
9139
9140 \f
9141 Regexp Templates:
9142
9143 A template entry of the form
9144
9145 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
9146
9147 will apply an Emacs style regular expression search for any port beginning
9148 in pci_req followed by numbers and ending in _l and connecting that to
9149 the pci_req_jtag_[] net, with the bus subscript coming from what matches
9150 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
9151
9152 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
9153 does the same thing. (Note a @ in the connection/replacement text is
9154 completely different -- still use \\1 there!) Thus this is the same as
9155 the above template:
9156
9157 .pci_req@_l (pci_req_jtag_[\\1]),
9158
9159 Here's another example to remove the _l, useful when naming conventions
9160 specify _ alone to mean active low. Note the use of [] to keep the bus
9161 subscript:
9162
9163 .\\(.*\\)_l (\\1_[]),
9164 \f
9165 Lisp Templates:
9166
9167 First any regular expression template is expanded.
9168
9169 If the syntax @\"( ... )\" is found in a connection, the expression in
9170 quotes will be evaluated as a Lisp expression, with @ replaced by the
9171 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
9172 4 into the brackets. Quote all double-quotes inside the expression with
9173 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
9174 regexp template backslash the backslash quote (\\\\\"...\\\\\").
9175
9176 There are special variables defined that are useful in these
9177 Lisp functions:
9178
9179 vl-name Name portion of the input/output port.
9180 vl-bits Bus bits portion of the input/output port ('[2:0]').
9181 vl-mbits Multidimensional array bits for port ('[2:0][3:0]').
9182 vl-width Width of the input/output port ('3' for [2:0]).
9183 May be a (...) expression if bits isn't a constant.
9184 vl-dir Direction of the pin input/output/inout/interface.
9185 vl-modport The modport, if an interface with a modport.
9186 vl-cell-type Module name/type of the cell ('InstModule').
9187 vl-cell-name Instance name of the cell ('instName').
9188
9189 Normal Lisp variables may be used in expressions. See
9190 `verilog-read-defines' which can set vh-{definename} variables for use
9191 here. Also, any comments of the form:
9192
9193 /*AUTO_LISP(setq foo 1)*/
9194
9195 will evaluate any Lisp expression inside the parenthesis between the
9196 beginning of the buffer and the point of the AUTOINST. This allows
9197 functions to be defined or variables to be changed between instantiations.
9198 (See also `verilog-auto-insert-lisp' if you want the output from your
9199 lisp function to be inserted.)
9200
9201 Note that when using lisp expressions errors may occur when @ is not a
9202 number; you may need to use the standard Emacs Lisp functions
9203 `number-to-string' and `string-to-number'.
9204
9205 After the evaluation is completed, @ substitution and [] substitution
9206 occur.
9207
9208 For more information see the \\[verilog-faq] and forums at URL
9209 `http://www.veripool.org'."
9210 (save-excursion
9211 ;; Find beginning
9212 (let* ((pt (point))
9213 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
9214 (indent-pt (save-excursion (verilog-backward-open-paren)
9215 (1+ (current-column))))
9216 (verilog-auto-inst-column (max verilog-auto-inst-column
9217 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
9218 (modi (verilog-modi-current))
9219 (moddecls (verilog-modi-get-decls modi))
9220 (vector-skip-list (unless verilog-auto-inst-vector
9221 (verilog-decls-get-signals moddecls)))
9222 submod submodi submoddecls
9223 inst skip-pins tpl-list tpl-num did-first par-values)
9224
9225 ;; Find module name that is instantiated
9226 (setq submod (verilog-read-inst-module)
9227 inst (verilog-read-inst-name)
9228 vl-cell-type submod
9229 vl-cell-name inst
9230 skip-pins (aref (verilog-read-inst-pins) 0))
9231
9232 ;; Parse any AUTO_LISP() before here
9233 (verilog-read-auto-lisp (point-min) pt)
9234
9235 ;; Read parameters (after AUTO_LISP)
9236 (setq par-values (and verilog-auto-inst-param-value
9237 (verilog-read-inst-param-value)))
9238
9239 ;; Lookup position, etc of submodule
9240 ;; Note this may raise an error
9241 (when (setq submodi (verilog-modi-lookup submod t))
9242 (setq submoddecls (verilog-modi-get-decls submodi))
9243 ;; If there's a number in the instantiation, it may be a argument to the
9244 ;; automatic variable instantiation program.
9245 (let* ((tpl-info (verilog-read-auto-template submod))
9246 (tpl-regexp (aref tpl-info 0)))
9247 (setq tpl-num (if (string-match tpl-regexp inst)
9248 (match-string 1 inst)
9249 "")
9250 tpl-list (aref tpl-info 1)))
9251 ;; Find submodule's signals and dump
9252 (let ((sig-list (verilog-signals-not-in
9253 (verilog-decls-get-interfaces submoddecls)
9254 skip-pins))
9255 (vl-dir "interface"))
9256 (when sig-list
9257 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
9258 (indent-to indent-pt)
9259 ;; Note these are searched for in verilog-read-sub-decls.
9260 (insert "// Interfaces\n")
9261 (mapc (lambda (port)
9262 (verilog-auto-inst-port port indent-pt
9263 tpl-list tpl-num for-star par-values))
9264 sig-list)))
9265 (let ((sig-list (verilog-signals-not-in
9266 (verilog-decls-get-outputs submoddecls)
9267 skip-pins))
9268 (vl-dir "output"))
9269 (when sig-list
9270 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
9271 (indent-to indent-pt)
9272 (insert "// Outputs\n")
9273 (mapc (lambda (port)
9274 (verilog-auto-inst-port port indent-pt
9275 tpl-list tpl-num for-star par-values))
9276 sig-list)))
9277 (let ((sig-list (verilog-signals-not-in
9278 (verilog-decls-get-inouts submoddecls)
9279 skip-pins))
9280 (vl-dir "inout"))
9281 (when sig-list
9282 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
9283 (indent-to indent-pt)
9284 (insert "// Inouts\n")
9285 (mapc (lambda (port)
9286 (verilog-auto-inst-port port indent-pt
9287 tpl-list tpl-num for-star par-values))
9288 sig-list)))
9289 (let ((sig-list (verilog-signals-not-in
9290 (verilog-decls-get-inputs submoddecls)
9291 skip-pins))
9292 (vl-dir "input"))
9293 (when sig-list
9294 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
9295 (indent-to indent-pt)
9296 (insert "// Inputs\n")
9297 (mapc (lambda (port)
9298 (verilog-auto-inst-port port indent-pt
9299 tpl-list tpl-num for-star par-values))
9300 sig-list)))
9301 ;; Kill extra semi
9302 (save-excursion
9303 (cond (did-first
9304 (re-search-backward "," pt t)
9305 (delete-char 1)
9306 (insert ");")
9307 (search-forward "\n") ;; Added by inst-port
9308 (delete-char -1)
9309 (if (search-forward ")" nil t) ;; From user, moved up a line
9310 (delete-char -1))
9311 (if (search-forward ";" nil t) ;; Don't error if user had syntax error and forgot it
9312 (delete-char -1)))))))))
9313
9314 (defun verilog-auto-inst-param ()
9315 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
9316 Replace the parameter connections to an instantiation with ones
9317 automatically derived from the module header of the instantiated netlist.
9318
9319 See \\[verilog-auto-inst] for limitations, and templates to customize the
9320 output.
9321
9322 For example, first take the submodule InstModule.v:
9323
9324 module InstModule (o,i)
9325 parameter PAR;
9326 endmodule
9327
9328 This is then used in a upper level module:
9329
9330 module ExampInst (o,i)
9331 parameter PAR;
9332 InstModule #(/*AUTOINSTPARAM*/)
9333 instName (/*AUTOINST*/);
9334 endmodule
9335
9336 Typing \\[verilog-auto] will make this into:
9337
9338 module ExampInst (o,i)
9339 output o;
9340 input i;
9341 InstModule #(/*AUTOINSTPARAM*/
9342 // Parameters
9343 .PAR (PAR));
9344 instName (/*AUTOINST*/);
9345 endmodule
9346
9347 Where the list of parameter connections come from the inst module.
9348 \f
9349 Templates:
9350
9351 You can customize the parameter connections using AUTO_TEMPLATEs,
9352 just as you would with \\[verilog-auto-inst]."
9353 (save-excursion
9354 ;; Find beginning
9355 (let* ((pt (point))
9356 (indent-pt (save-excursion (verilog-backward-open-paren)
9357 (1+ (current-column))))
9358 (verilog-auto-inst-column (max verilog-auto-inst-column
9359 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
9360 (modi (verilog-modi-current))
9361 (moddecls (verilog-modi-get-decls modi))
9362 (vector-skip-list (unless verilog-auto-inst-vector
9363 (verilog-decls-get-signals moddecls)))
9364 submod submodi submoddecls
9365 inst skip-pins tpl-list tpl-num did-first)
9366 ;; Find module name that is instantiated
9367 (setq submod (save-excursion
9368 ;; Get to the point where AUTOINST normally is to read the module
9369 (verilog-re-search-forward-quick "[(;]" nil nil)
9370 (verilog-read-inst-module))
9371 inst (save-excursion
9372 ;; Get to the point where AUTOINST normally is to read the module
9373 (verilog-re-search-forward-quick "[(;]" nil nil)
9374 (verilog-read-inst-name))
9375 vl-cell-type submod
9376 vl-cell-name inst
9377 skip-pins (aref (verilog-read-inst-pins) 0))
9378
9379 ;; Parse any AUTO_LISP() before here
9380 (verilog-read-auto-lisp (point-min) pt)
9381
9382 ;; Lookup position, etc of submodule
9383 ;; Note this may raise an error
9384 (when (setq submodi (verilog-modi-lookup submod t))
9385 (setq submoddecls (verilog-modi-get-decls submodi))
9386 ;; If there's a number in the instantiation, it may be a argument to the
9387 ;; automatic variable instantiation program.
9388 (let* ((tpl-info (verilog-read-auto-template submod))
9389 (tpl-regexp (aref tpl-info 0)))
9390 (setq tpl-num (if (string-match tpl-regexp inst)
9391 (match-string 1 inst)
9392 "")
9393 tpl-list (aref tpl-info 1)))
9394 ;; Find submodule's signals and dump
9395 (let ((sig-list (verilog-signals-not-in
9396 (verilog-decls-get-gparams submoddecls)
9397 skip-pins))
9398 (vl-dir "parameter"))
9399 (when sig-list
9400 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
9401 (indent-to indent-pt)
9402 ;; Note these are searched for in verilog-read-sub-decls.
9403 (insert "// Parameters\n")
9404 (mapc (lambda (port)
9405 (verilog-auto-inst-port port indent-pt
9406 tpl-list tpl-num nil nil))
9407 sig-list)))
9408 ;; Kill extra semi
9409 (save-excursion
9410 (cond (did-first
9411 (re-search-backward "," pt t)
9412 (delete-char 1)
9413 (insert ")")
9414 (search-forward "\n") ;; Added by inst-port
9415 (delete-char -1)
9416 (if (search-forward ")" nil t) ;; From user, moved up a line
9417 (delete-char -1)))))))))
9418
9419 (defun verilog-auto-reg ()
9420 "Expand AUTOREG statements, as part of \\[verilog-auto].
9421 Make reg statements for any output that isn't already declared,
9422 and isn't a wire output from a block.
9423
9424 Limitations:
9425 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
9426
9427 This does NOT work on memories, declare those yourself.
9428
9429 An example:
9430
9431 module ExampReg (o,i)
9432 output o;
9433 input i;
9434 /*AUTOREG*/
9435 always o = i;
9436 endmodule
9437
9438 Typing \\[verilog-auto] will make this into:
9439
9440 module ExampReg (o,i)
9441 output o;
9442 input i;
9443 /*AUTOREG*/
9444 // Beginning of automatic regs (for this module's undeclared outputs)
9445 reg o;
9446 // End of automatics
9447 always o = i;
9448 endmodule"
9449 (save-excursion
9450 ;; Point must be at insertion point.
9451 (let* ((indent-pt (current-indentation))
9452 (modi (verilog-modi-current))
9453 (moddecls (verilog-modi-get-decls modi))
9454 (modsubdecls (verilog-modi-get-sub-decls modi))
9455 (sig-list (verilog-signals-not-in
9456 (verilog-decls-get-outputs moddecls)
9457 (append (verilog-decls-get-wires moddecls)
9458 (verilog-decls-get-regs moddecls)
9459 (verilog-decls-get-assigns moddecls)
9460 (verilog-decls-get-consts moddecls)
9461 (verilog-decls-get-gparams moddecls)
9462 (verilog-subdecls-get-outputs modsubdecls)
9463 (verilog-subdecls-get-inouts modsubdecls)))))
9464 (forward-line 1)
9465 (when sig-list
9466 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
9467 (verilog-insert-definition sig-list "reg" indent-pt nil)
9468 (verilog-modi-cache-add-regs modi sig-list)
9469 (verilog-insert-indent "// End of automatics\n")))))
9470
9471 (defun verilog-auto-reg-input ()
9472 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
9473 Make reg statements instantiation inputs that aren't already declared.
9474 This is useful for making a top level shell for testing the module that is
9475 to be instantiated.
9476
9477 Limitations:
9478 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
9479
9480 This does NOT work on memories, declare those yourself.
9481
9482 An example (see `verilog-auto-inst' for what else is going on here):
9483
9484 module ExampRegInput (o,i)
9485 output o;
9486 input i;
9487 /*AUTOREGINPUT*/
9488 InstModule instName
9489 (/*AUTOINST*/);
9490 endmodule
9491
9492 Typing \\[verilog-auto] will make this into:
9493
9494 module ExampRegInput (o,i)
9495 output o;
9496 input i;
9497 /*AUTOREGINPUT*/
9498 // Beginning of automatic reg inputs (for undeclared ...
9499 reg [31:0] iv; // From inst of inst.v
9500 // End of automatics
9501 InstModule instName
9502 (/*AUTOINST*/
9503 // Outputs
9504 .o (o[31:0]),
9505 // Inputs
9506 .iv (iv));
9507 endmodule"
9508 (save-excursion
9509 ;; Point must be at insertion point.
9510 (let* ((indent-pt (current-indentation))
9511 (modi (verilog-modi-current))
9512 (moddecls (verilog-modi-get-decls modi))
9513 (modsubdecls (verilog-modi-get-sub-decls modi))
9514 (sig-list (verilog-signals-combine-bus
9515 (verilog-signals-not-in
9516 (append (verilog-subdecls-get-inputs modsubdecls)
9517 (verilog-subdecls-get-inouts modsubdecls))
9518 (verilog-decls-get-signals moddecls)))))
9519 (forward-line 1)
9520 (when sig-list
9521 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
9522 (verilog-insert-definition sig-list "reg" indent-pt nil)
9523 (verilog-modi-cache-add-regs modi sig-list)
9524 (verilog-insert-indent "// End of automatics\n")))))
9525
9526 (defun verilog-auto-wire ()
9527 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
9528 Make wire statements for instantiations outputs that aren't
9529 already declared.
9530
9531 Limitations:
9532 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
9533 and all busses must have widths, such as those from AUTOINST, or using []
9534 in AUTO_TEMPLATEs.
9535
9536 This does NOT work on memories or SystemVerilog .name connections,
9537 declare those yourself.
9538
9539 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
9540 determine how to bus together. This occurs when you have ports with
9541 non-numeric or non-sequential bus subscripts. If Verilog mode
9542 mis-guessed, you'll have to declare them yourself.
9543
9544 An example (see `verilog-auto-inst' for what else is going on here):
9545
9546 module ExampWire (o,i)
9547 output o;
9548 input i;
9549 /*AUTOWIRE*/
9550 InstModule instName
9551 (/*AUTOINST*/);
9552 endmodule
9553
9554 Typing \\[verilog-auto] will make this into:
9555
9556 module ExampWire (o,i)
9557 output o;
9558 input i;
9559 /*AUTOWIRE*/
9560 // Beginning of automatic wires
9561 wire [31:0] ov; // From inst of inst.v
9562 // End of automatics
9563 InstModule instName
9564 (/*AUTOINST*/
9565 // Outputs
9566 .ov (ov[31:0]),
9567 // Inputs
9568 .i (i));
9569 wire o = | ov;
9570 endmodule"
9571 (save-excursion
9572 ;; Point must be at insertion point.
9573 (let* ((indent-pt (current-indentation))
9574 (modi (verilog-modi-current))
9575 (moddecls (verilog-modi-get-decls modi))
9576 (modsubdecls (verilog-modi-get-sub-decls modi))
9577 (sig-list (verilog-signals-combine-bus
9578 (verilog-signals-not-in
9579 (append (verilog-subdecls-get-outputs modsubdecls)
9580 (verilog-subdecls-get-inouts modsubdecls))
9581 (verilog-decls-get-signals moddecls)))))
9582 (forward-line 1)
9583 (when sig-list
9584 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
9585 (verilog-insert-definition sig-list "wire" indent-pt nil)
9586 (verilog-modi-cache-add-wires modi sig-list)
9587 (verilog-insert-indent "// End of automatics\n")
9588 (when nil ;; Too slow on huge modules, plus makes everyone's module change
9589 (beginning-of-line)
9590 (setq pnt (point))
9591 (verilog-pretty-declarations quiet)
9592 (goto-char pnt)
9593 (verilog-pretty-expr t "//"))))))
9594
9595 (defun verilog-auto-output (&optional with-params)
9596 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
9597 Make output statements for any output signal from an /*AUTOINST*/ that
9598 isn't a input to another AUTOINST. This is useful for modules which
9599 only instantiate other modules.
9600
9601 Limitations:
9602 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
9603
9604 If placed inside the parenthesis of a module declaration, it creates
9605 Verilog 2001 style, else uses Verilog 1995 style.
9606
9607 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
9608 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
9609
9610 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
9611
9612 Signals matching `verilog-auto-output-ignore-regexp' are not included.
9613
9614 An example (see `verilog-auto-inst' for what else is going on here):
9615
9616 module ExampOutput (ov,i)
9617 input i;
9618 /*AUTOOUTPUT*/
9619 InstModule instName
9620 (/*AUTOINST*/);
9621 endmodule
9622
9623 Typing \\[verilog-auto] will make this into:
9624
9625 module ExampOutput (ov,i)
9626 input i;
9627 /*AUTOOUTPUT*/
9628 // Beginning of automatic outputs (from unused autoinst outputs)
9629 output [31:0] ov; // From inst of inst.v
9630 // End of automatics
9631 InstModule instName
9632 (/*AUTOINST*/
9633 // Outputs
9634 .ov (ov[31:0]),
9635 // Inputs
9636 .i (i));
9637 endmodule
9638
9639 You may also provide an optional regular expression, in which case only
9640 signals matching the regular expression will be included. For example the
9641 same expansion will result from only extracting outputs starting with ov:
9642
9643 /*AUTOOUTPUT(\"^ov\")*/"
9644 (save-excursion
9645 ;; Point must be at insertion point.
9646 (let* ((indent-pt (current-indentation))
9647 (regexp (and with-params
9648 (nth 0 (verilog-read-auto-params 1))))
9649 (v2k (verilog-in-paren))
9650 (modi (verilog-modi-current))
9651 (moddecls (verilog-modi-get-decls modi))
9652 (modsubdecls (verilog-modi-get-sub-decls modi))
9653 (sig-list (verilog-signals-not-in
9654 (verilog-subdecls-get-outputs modsubdecls)
9655 (append (verilog-decls-get-outputs moddecls)
9656 (verilog-decls-get-inouts moddecls)
9657 (verilog-subdecls-get-inputs modsubdecls)
9658 (verilog-subdecls-get-inouts modsubdecls)))))
9659 (when regexp
9660 (setq sig-list (verilog-signals-matching-regexp
9661 sig-list regexp)))
9662 (setq sig-list (verilog-signals-not-matching-regexp
9663 sig-list verilog-auto-output-ignore-regexp))
9664 (forward-line 1)
9665 (when v2k (verilog-repair-open-comma))
9666 (when sig-list
9667 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
9668 (verilog-insert-definition sig-list "output" indent-pt v2k)
9669 (verilog-modi-cache-add-outputs modi sig-list)
9670 (verilog-insert-indent "// End of automatics\n"))
9671 (when v2k (verilog-repair-close-comma)))))
9672
9673 (defun verilog-auto-output-every ()
9674 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
9675 Make output statements for any signals that aren't primary inputs or
9676 outputs already. This makes every signal in the design a output. This is
9677 useful to get Synopsys to preserve every signal in the design, since it
9678 won't optimize away the outputs.
9679
9680 An example:
9681
9682 module ExampOutputEvery (o,i,tempa,tempb)
9683 output o;
9684 input i;
9685 /*AUTOOUTPUTEVERY*/
9686 wire tempa = i;
9687 wire tempb = tempa;
9688 wire o = tempb;
9689 endmodule
9690
9691 Typing \\[verilog-auto] will make this into:
9692
9693 module ExampOutputEvery (o,i,tempa,tempb)
9694 output o;
9695 input i;
9696 /*AUTOOUTPUTEVERY*/
9697 // Beginning of automatic outputs (every signal)
9698 output tempb;
9699 output tempa;
9700 // End of automatics
9701 wire tempa = i;
9702 wire tempb = tempa;
9703 wire o = tempb;
9704 endmodule"
9705 (save-excursion
9706 ;;Point must be at insertion point
9707 (let* ((indent-pt (current-indentation))
9708 (v2k (verilog-in-paren))
9709 (modi (verilog-modi-current))
9710 (moddecls (verilog-modi-get-decls modi))
9711 (sig-list (verilog-signals-combine-bus
9712 (verilog-signals-not-in
9713 (verilog-decls-get-signals moddecls)
9714 (verilog-decls-get-ports moddecls)))))
9715 (forward-line 1)
9716 (when v2k (verilog-repair-open-comma))
9717 (when sig-list
9718 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
9719 (verilog-insert-definition sig-list "output" indent-pt v2k)
9720 (verilog-modi-cache-add-outputs modi sig-list)
9721 (verilog-insert-indent "// End of automatics\n"))
9722 (when v2k (verilog-repair-close-comma)))))
9723
9724 (defun verilog-auto-input (&optional with-params)
9725 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
9726 Make input statements for any input signal into an /*AUTOINST*/ that
9727 isn't declared elsewhere inside the module. This is useful for modules which
9728 only instantiate other modules.
9729
9730 Limitations:
9731 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
9732
9733 If placed inside the parenthesis of a module declaration, it creates
9734 Verilog 2001 style, else uses Verilog 1995 style.
9735
9736 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
9737 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
9738
9739 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
9740
9741 Signals matching `verilog-auto-input-ignore-regexp' are not included.
9742
9743 An example (see `verilog-auto-inst' for what else is going on here):
9744
9745 module ExampInput (ov,i)
9746 output [31:0] ov;
9747 /*AUTOINPUT*/
9748 InstModule instName
9749 (/*AUTOINST*/);
9750 endmodule
9751
9752 Typing \\[verilog-auto] will make this into:
9753
9754 module ExampInput (ov,i)
9755 output [31:0] ov;
9756 /*AUTOINPUT*/
9757 // Beginning of automatic inputs (from unused autoinst inputs)
9758 input i; // From inst of inst.v
9759 // End of automatics
9760 InstModule instName
9761 (/*AUTOINST*/
9762 // Outputs
9763 .ov (ov[31:0]),
9764 // Inputs
9765 .i (i));
9766 endmodule
9767
9768 You may also provide an optional regular expression, in which case only
9769 signals matching the regular expression will be included. For example the
9770 same expansion will result from only extracting inputs starting with i:
9771
9772 /*AUTOINPUT(\"^i\")*/"
9773 (save-excursion
9774 (let* ((indent-pt (current-indentation))
9775 (regexp (and with-params
9776 (nth 0 (verilog-read-auto-params 1))))
9777 (v2k (verilog-in-paren))
9778 (modi (verilog-modi-current))
9779 (moddecls (verilog-modi-get-decls modi))
9780 (modsubdecls (verilog-modi-get-sub-decls modi))
9781 (sig-list (verilog-signals-not-in
9782 (verilog-subdecls-get-inputs modsubdecls)
9783 (append (verilog-decls-get-inputs moddecls)
9784 (verilog-decls-get-inouts moddecls)
9785 (verilog-decls-get-wires moddecls)
9786 (verilog-decls-get-regs moddecls)
9787 (verilog-decls-get-consts moddecls)
9788 (verilog-decls-get-gparams moddecls)
9789 (verilog-subdecls-get-outputs modsubdecls)
9790 (verilog-subdecls-get-inouts modsubdecls)))))
9791 (when regexp
9792 (setq sig-list (verilog-signals-matching-regexp
9793 sig-list regexp)))
9794 (setq sig-list (verilog-signals-not-matching-regexp
9795 sig-list verilog-auto-input-ignore-regexp))
9796 (forward-line 1)
9797 (when v2k (verilog-repair-open-comma))
9798 (when sig-list
9799 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
9800 (verilog-insert-definition sig-list "input" indent-pt v2k)
9801 (verilog-modi-cache-add-inputs modi sig-list)
9802 (verilog-insert-indent "// End of automatics\n"))
9803 (when v2k (verilog-repair-close-comma)))))
9804
9805 (defun verilog-auto-inout (&optional with-params)
9806 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
9807 Make inout statements for any inout signal in an /*AUTOINST*/ that
9808 isn't declared elsewhere inside the module.
9809
9810 Limitations:
9811 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
9812
9813 If placed inside the parenthesis of a module declaration, it creates
9814 Verilog 2001 style, else uses Verilog 1995 style.
9815
9816 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
9817 instantiation, all bets are off. (For example due to a AUTO_TEMPLATE).
9818
9819 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
9820
9821 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
9822
9823 An example (see `verilog-auto-inst' for what else is going on here):
9824
9825 module ExampInout (ov,i)
9826 input i;
9827 /*AUTOINOUT*/
9828 InstModule instName
9829 (/*AUTOINST*/);
9830 endmodule
9831
9832 Typing \\[verilog-auto] will make this into:
9833
9834 module ExampInout (ov,i)
9835 input i;
9836 /*AUTOINOUT*/
9837 // Beginning of automatic inouts (from unused autoinst inouts)
9838 inout [31:0] ov; // From inst of inst.v
9839 // End of automatics
9840 InstModule instName
9841 (/*AUTOINST*/
9842 // Inouts
9843 .ov (ov[31:0]),
9844 // Inputs
9845 .i (i));
9846 endmodule
9847
9848 You may also provide an optional regular expression, in which case only
9849 signals matching the regular expression will be included. For example the
9850 same expansion will result from only extracting inouts starting with i:
9851
9852 /*AUTOINOUT(\"^i\")*/"
9853 (save-excursion
9854 ;; Point must be at insertion point.
9855 (let* ((indent-pt (current-indentation))
9856 (regexp (and with-params
9857 (nth 0 (verilog-read-auto-params 1))))
9858 (v2k (verilog-in-paren))
9859 (modi (verilog-modi-current))
9860 (moddecls (verilog-modi-get-decls modi))
9861 (modsubdecls (verilog-modi-get-sub-decls modi))
9862 (sig-list (verilog-signals-not-in
9863 (verilog-subdecls-get-inouts modsubdecls)
9864 (append (verilog-decls-get-outputs moddecls)
9865 (verilog-decls-get-inouts moddecls)
9866 (verilog-decls-get-inputs moddecls)
9867 (verilog-subdecls-get-inputs modsubdecls)
9868 (verilog-subdecls-get-outputs modsubdecls)))))
9869 (when regexp
9870 (setq sig-list (verilog-signals-matching-regexp
9871 sig-list regexp)))
9872 (setq sig-list (verilog-signals-not-matching-regexp
9873 sig-list verilog-auto-inout-ignore-regexp))
9874 (forward-line 1)
9875 (when v2k (verilog-repair-open-comma))
9876 (when sig-list
9877 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
9878 (verilog-insert-definition sig-list "inout" indent-pt v2k)
9879 (verilog-modi-cache-add-inouts modi sig-list)
9880 (verilog-insert-indent "// End of automatics\n"))
9881 (when v2k (verilog-repair-close-comma)))))
9882
9883 (defun verilog-auto-inout-module (&optional complement)
9884 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
9885 Take input/output/inout statements from the specified module and insert
9886 into the current module. This is useful for making null templates and
9887 shell modules which need to have identical I/O with another module.
9888 Any I/O which are already defined in this module will not be redefined.
9889
9890 Limitations:
9891 If placed inside the parenthesis of a module declaration, it creates
9892 Verilog 2001 style, else uses Verilog 1995 style.
9893
9894 Concatenation and outputting partial busses is not supported.
9895
9896 Module names must be resolvable to filenames. See `verilog-auto-inst'.
9897
9898 Signals are not inserted in the same order as in the original module,
9899 though they will appear to be in the same order to a AUTOINST
9900 instantiating either module.
9901
9902 An example:
9903
9904 module ExampShell (/*AUTOARG*/)
9905 /*AUTOINOUTMODULE(\"ExampMain\")*/
9906 endmodule
9907
9908 module ExampMain (i,o,io)
9909 input i;
9910 output o;
9911 inout io;
9912 endmodule
9913
9914 Typing \\[verilog-auto] will make this into:
9915
9916 module ExampShell (/*AUTOARG*/i,o,io)
9917 /*AUTOINOUTMODULE(\"ExampMain\")*/
9918 // Beginning of automatic in/out/inouts (from specific module)
9919 output o;
9920 inout io;
9921 input i;
9922 // End of automatics
9923 endmodule
9924
9925 You may also provide an optional regular expression, in which case only
9926 signals matching the regular expression will be included. For example the
9927 same expansion will result from only extracting signals starting with i:
9928
9929 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
9930
9931 You may also provide an optional second regular expression, in
9932 which case only signals which have that pin direction and data
9933 type will be included. This matches against everything before
9934 the signal name in the declaration, for example against
9935 \"input\" (single bit), \"output logic\" (direction and type) or
9936 \"output [1:0]\" (direction and implicit type). You also
9937 probably want to skip spaces in your regexp.
9938
9939 For example, the below will result in matching the output \"o\"
9940 against the previous example's module:
9941
9942 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/"
9943 (save-excursion
9944 (let* ((params (verilog-read-auto-params 1 3))
9945 (submod (nth 0 params))
9946 (regexp (nth 1 params))
9947 (direction-re (nth 2 params))
9948 submodi)
9949 ;; Lookup position, etc of co-module
9950 ;; Note this may raise an error
9951 (when (setq submodi (verilog-modi-lookup submod t))
9952 (let* ((indent-pt (current-indentation))
9953 (v2k (verilog-in-paren))
9954 (modi (verilog-modi-current))
9955 (moddecls (verilog-modi-get-decls modi))
9956 (submoddecls (verilog-modi-get-decls submodi))
9957 (sig-list-i (verilog-signals-not-in
9958 (if complement
9959 (verilog-decls-get-outputs submoddecls)
9960 (verilog-decls-get-inputs submoddecls))
9961 (append (verilog-decls-get-inputs moddecls))))
9962 (sig-list-o (verilog-signals-not-in
9963 (if complement
9964 (verilog-decls-get-inputs submoddecls)
9965 (verilog-decls-get-outputs submoddecls))
9966 (append (verilog-decls-get-outputs moddecls))))
9967 (sig-list-io (verilog-signals-not-in
9968 (verilog-decls-get-inouts submoddecls)
9969 (append (verilog-decls-get-inouts moddecls))))
9970 (sig-list-if (verilog-signals-not-in
9971 (verilog-decls-get-interfaces submoddecls)
9972 (append (verilog-decls-get-interfaces moddecls)))))
9973 (forward-line 1)
9974 (setq sig-list-i (verilog-signals-matching-dir-re
9975 (verilog-signals-matching-regexp sig-list-i regexp)
9976 "input" direction-re)
9977 sig-list-o (verilog-signals-matching-dir-re
9978 (verilog-signals-matching-regexp sig-list-o regexp)
9979 "output" direction-re)
9980 sig-list-io (verilog-signals-matching-dir-re
9981 (verilog-signals-matching-regexp sig-list-io regexp)
9982 "inout" direction-re)
9983 sig-list-if (verilog-signals-matching-dir-re
9984 (verilog-signals-matching-regexp sig-list-if regexp)
9985 "interface" direction-re))
9986 (when v2k (verilog-repair-open-comma))
9987 (when (or sig-list-i sig-list-o sig-list-io)
9988 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
9989 ;; Don't sort them so a upper AUTOINST will match the main module
9990 (verilog-insert-definition sig-list-o "output" indent-pt v2k t)
9991 (verilog-insert-definition sig-list-io "inout" indent-pt v2k t)
9992 (verilog-insert-definition sig-list-i "input" indent-pt v2k t)
9993 (verilog-insert-definition sig-list-if "interface" indent-pt v2k t)
9994 (verilog-modi-cache-add-inputs modi sig-list-i)
9995 (verilog-modi-cache-add-outputs modi sig-list-o)
9996 (verilog-modi-cache-add-inouts modi sig-list-io)
9997 (verilog-insert-indent "// End of automatics\n"))
9998 (when v2k (verilog-repair-close-comma)))))))
9999
10000 (defun verilog-auto-inout-comp ()
10001 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
10002 Take input/output/inout statements from the specified module and
10003 insert the inverse into the current module (inputs become outputs
10004 and vice-versa.) This is useful for making test and stimulus
10005 modules which need to have complementing I/O with another module.
10006 Any I/O which are already defined in this module will not be
10007 redefined.
10008
10009 Limitations:
10010 If placed inside the parenthesis of a module declaration, it creates
10011 Verilog 2001 style, else uses Verilog 1995 style.
10012
10013 Concatenation and outputting partial busses is not supported.
10014
10015 Module names must be resolvable to filenames. See `verilog-auto-inst'.
10016
10017 Signals are not inserted in the same order as in the original module,
10018 though they will appear to be in the same order to a AUTOINST
10019 instantiating either module.
10020
10021 An example:
10022
10023 module ExampShell (/*AUTOARG*/)
10024 /*AUTOINOUTCOMP(\"ExampMain\")*/
10025 endmodule
10026
10027 module ExampMain (i,o,io)
10028 input i;
10029 output o;
10030 inout io;
10031 endmodule
10032
10033 Typing \\[verilog-auto] will make this into:
10034
10035 module ExampShell (/*AUTOARG*/i,o,io)
10036 /*AUTOINOUTCOMP(\"ExampMain\")*/
10037 // Beginning of automatic in/out/inouts (from specific module)
10038 output i;
10039 inout io;
10040 input o;
10041 // End of automatics
10042 endmodule
10043
10044 You may also provide an optional regular expression, in which case only
10045 signals matching the regular expression will be included. For example the
10046 same expansion will result from only extracting signals starting with i:
10047
10048 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
10049 (verilog-auto-inout-module t))
10050
10051 (defun verilog-auto-insert-lisp ()
10052 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
10053 The Lisp code provided is called, and the Lisp code calls
10054 `insert` to insert text into the current file beginning on the
10055 line after the AUTOINSERTLISP.
10056
10057 See also AUTO_LISP, which takes a Lisp expression and evaluates
10058 it during `verilog-auto-inst' but does not insert any text.
10059
10060 An example:
10061
10062 module ExampInsertLisp;
10063 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
10064 endmodule
10065
10066 // For this example we declare the function in the
10067 // module's file itself. Often you'd define it instead
10068 // in a site-start.el or .emacs file.
10069 /*
10070 Local Variables:
10071 eval:
10072 (defun my-verilog-insert-hello (who)
10073 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
10074 End:
10075 */
10076
10077 Typing \\[verilog-auto] will call my-verilog-insert-hello and
10078 expand the above into:
10079
10080 // Beginning of automatic insert lisp
10081 initial $write(\"hello world\");
10082 // End of automatics
10083
10084 You can also call an external program and insert the returned
10085 text:
10086
10087 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
10088 // Beginning of automatic insert lisp
10089 //hello
10090 // End of automatics"
10091 (save-excursion
10092 ;; Point is at end of /*AUTO...*/
10093 (let* ((indent-pt (current-indentation))
10094 (cmd-end-pt (save-excursion (search-backward ")")
10095 (forward-char)
10096 (point))) ;; Closing paren
10097 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
10098 (backward-sexp 1)
10099 (point))) ;; Beginning paren
10100 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
10101 (forward-line 1)
10102 ;; Some commands don't move point (like insert-file) so we always
10103 ;; add the begin/end comments, then delete it if not needed
10104 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
10105 (verilog-insert-indent "// End of automatics\n")
10106 (forward-line -1)
10107 (eval (read cmd))
10108 (forward-line -1)
10109 (verilog-delete-empty-auto-pair))))
10110
10111 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
10112 "Return list of signals for current AUTOSENSE block."
10113 (let* ((sigss (verilog-read-always-signals))
10114 (sig-list (verilog-signals-not-params
10115 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
10116 (append (and (not verilog-auto-sense-include-inputs)
10117 (verilog-alw-get-outputs sigss))
10118 (verilog-decls-get-consts moddecls)
10119 (verilog-decls-get-gparams moddecls)
10120 presense-sigs)))))
10121 sig-list))
10122
10123 (defun verilog-auto-sense ()
10124 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
10125 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
10126 with one automatically derived from all inputs declared in the always
10127 statement. Signals that are generated within the same always block are NOT
10128 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
10129 Long lines are split based on the `fill-column', see \\[set-fill-column].
10130
10131 Limitations:
10132 Verilog does not allow memories (multidimensional arrays) in sensitivity
10133 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
10134
10135 Constant signals:
10136 AUTOSENSE cannot always determine if a `define is a constant or a signal
10137 (it could be in a include file for example). If a `define or other signal
10138 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
10139 declaration anywhere in the module (parenthesis are required):
10140
10141 /* AUTO_CONSTANT ( `this_is_really_constant_dont_autosense_it ) */
10142
10143 Better yet, use a parameter, which will be understood to be constant
10144 automatically.
10145
10146 OOps!
10147 If AUTOSENSE makes a mistake, please report it. (First try putting
10148 a begin/end after your always!) As a workaround, if a signal that
10149 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
10150 If a signal should be in the sensitivity list wasn't, placing it before
10151 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
10152 autos are updated (or added if it occurs there already).
10153
10154 An example:
10155
10156 always @ (/*AS*/) begin
10157 /* AUTO_CONSTANT (`constant) */
10158 outin = ina | inb | `constant;
10159 out = outin;
10160 end
10161
10162 Typing \\[verilog-auto] will make this into:
10163
10164 always @ (/*AS*/ina or inb) begin
10165 /* AUTO_CONSTANT (`constant) */
10166 outin = ina | inb | `constant;
10167 out = outin;
10168 end
10169
10170 Note in Verilog 2001, you can often get the same result from the new @*
10171 operator. (This was added to the language in part due to AUTOSENSE!)
10172
10173 always @* begin
10174 outin = ina | inb | `constant;
10175 out = outin;
10176 end"
10177 (save-excursion
10178 ;; Find beginning
10179 (let* ((start-pt (save-excursion
10180 (verilog-re-search-backward "(" nil t)
10181 (point)))
10182 (indent-pt (save-excursion
10183 (or (and (goto-char start-pt) (1+ (current-column)))
10184 (current-indentation))))
10185 (modi (verilog-modi-current))
10186 (moddecls (verilog-modi-get-decls modi))
10187 (sig-memories (verilog-signals-memory
10188 (append
10189 (verilog-decls-get-regs moddecls)
10190 (verilog-decls-get-wires moddecls))))
10191 sig-list not-first presense-sigs)
10192 ;; Read signals in always, eliminate outputs from sense list
10193 (setq presense-sigs (verilog-signals-from-signame
10194 (save-excursion
10195 (verilog-read-signals start-pt (point)))))
10196 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
10197 (when sig-memories
10198 (let ((tlen (length sig-list)))
10199 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
10200 (if (not (eq tlen (length sig-list))) (insert " /*memory or*/ "))))
10201 (if (and presense-sigs ;; Add a "or" if not "(.... or /*AUTOSENSE*/"
10202 (save-excursion (goto-char (point))
10203 (verilog-re-search-backward "[a-zA-Z0-9$_.%`]+" start-pt t)
10204 (verilog-re-search-backward "\\s-" start-pt t)
10205 (while (looking-at "\\s-`endif")
10206 (verilog-re-search-backward "[a-zA-Z0-9$_.%`]+" start-pt t)
10207 (verilog-re-search-backward "\\s-" start-pt t))
10208 (not (looking-at "\\s-or\\b"))))
10209 (setq not-first t))
10210 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
10211 (while sig-list
10212 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
10213 (insert "\n")
10214 (indent-to indent-pt)
10215 (if not-first (insert "or ")))
10216 (not-first (insert " or ")))
10217 (insert (verilog-sig-name (car sig-list)))
10218 (setq sig-list (cdr sig-list)
10219 not-first t)))))
10220
10221 (defun verilog-auto-reset ()
10222 "Expand AUTORESET statements, as part of \\[verilog-auto].
10223 Replace the /*AUTORESET*/ comment with code to initialize all
10224 registers set elsewhere in the always block.
10225
10226 Limitations:
10227 AUTORESET will not clear memories.
10228
10229 AUTORESET uses <= if there are any <= assignments in the block,
10230 else it uses =.
10231
10232 /*AUTORESET*/ presumes that any signals mentioned between the previous
10233 begin/case/if statement and the AUTORESET comment are being reset manually
10234 and should not be automatically reset. This includes omitting any signals
10235 used on the right hand side of assignments.
10236
10237 By default, AUTORESET will include the width of the signal in the autos,
10238 this is a recent change. To control this behavior, see
10239 `verilog-auto-reset-widths'.
10240
10241 AUTORESET ties signals to deasserted, which is presumed to be zero.
10242 Signals that match `verilog-active-low-regexp' will be deasserted by tieing
10243 them to a one.
10244
10245 An example:
10246
10247 always @(posedge clk or negedge reset_l) begin
10248 if (!reset_l) begin
10249 c <= 1;
10250 /*AUTORESET*/
10251 end
10252 else begin
10253 a <= in_a;
10254 b <= in_b;
10255 c <= in_c;
10256 end
10257 end
10258
10259 Typing \\[verilog-auto] will make this into:
10260
10261 always @(posedge core_clk or negedge reset_l) begin
10262 if (!reset_l) begin
10263 c <= 1;
10264 /*AUTORESET*/
10265 // Beginning of autoreset for uninitialized flops
10266 a <= 0;
10267 b <= 0;
10268 // End of automatics
10269 end
10270 else begin
10271 a <= in_a;
10272 b <= in_b;
10273 c <= in_c;
10274 end
10275 end"
10276
10277 (interactive)
10278 (save-excursion
10279 ;; Find beginning
10280 (let* ((indent-pt (current-indentation))
10281 (modi (verilog-modi-current))
10282 (moddecls (verilog-modi-get-decls modi))
10283 (all-list (verilog-decls-get-signals moddecls))
10284 sigss sig-list prereset-sigs assignment-str)
10285 ;; Read signals in always, eliminate outputs from reset list
10286 (setq prereset-sigs (verilog-signals-from-signame
10287 (save-excursion
10288 (verilog-read-signals
10289 (save-excursion
10290 (verilog-re-search-backward "\\(@\\|\\<begin\\>\\|\\<if\\>\\|\\<case\\>\\)" nil t)
10291 (point))
10292 (point)))))
10293 (save-excursion
10294 (verilog-re-search-backward "@" nil t)
10295 (setq sigss (verilog-read-always-signals)))
10296 (setq assignment-str (if (verilog-alw-get-uses-delayed sigss)
10297 (concat " <= " verilog-assignment-delay)
10298 " = "))
10299 (setq sig-list (verilog-signals-not-in (verilog-alw-get-outputs sigss)
10300 prereset-sigs))
10301 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
10302 (when sig-list
10303 (insert "\n");
10304 (indent-to indent-pt)
10305 (insert "// Beginning of autoreset for uninitialized flops\n");
10306 (indent-to indent-pt)
10307 (while sig-list
10308 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ;; As sig-list has no widths
10309 (car sig-list))))
10310 (insert (verilog-sig-name sig)
10311 assignment-str
10312 (verilog-sig-tieoff sig (not verilog-auto-reset-widths))
10313 ";\n")
10314 (indent-to indent-pt)
10315 (setq sig-list (cdr sig-list))))
10316 (insert "// End of automatics")))))
10317
10318 (defun verilog-auto-tieoff ()
10319 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
10320 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
10321 signals to deasserted.
10322
10323 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
10324 input/output list as another module, but no internals. Specifically, it
10325 finds all outputs in the module, and if that input is not otherwise declared
10326 as a register or wire, creates a tieoff.
10327
10328 AUTORESET ties signals to deasserted, which is presumed to be zero.
10329 Signals that match `verilog-active-low-regexp' will be deasserted by tieing
10330 them to a one.
10331
10332 An example of making a stub for another module:
10333
10334 module ExampStub (/*AUTOINST*/);
10335 /*AUTOINOUTMODULE(\"Foo\")*/
10336 /*AUTOTIEOFF*/
10337 // verilator lint_off UNUSED
10338 wire _unused_ok = &{1'b0,
10339 /*AUTOUNUSED*/
10340 1'b0};
10341 // verilator lint_on UNUSED
10342 endmodule
10343
10344 Typing \\[verilog-auto] will make this into:
10345
10346 module ExampStub (/*AUTOINST*/...);
10347 /*AUTOINOUTMODULE(\"Foo\")*/
10348 // Beginning of autotieoff
10349 output [2:0] foo;
10350 // End of automatics
10351
10352 /*AUTOTIEOFF*/
10353 // Beginning of autotieoff
10354 wire [2:0] foo = 3'b0;
10355 // End of automatics
10356 ...
10357 endmodule"
10358 (interactive)
10359 (save-excursion
10360 ;; Find beginning
10361 (let* ((indent-pt (current-indentation))
10362 (modi (verilog-modi-current))
10363 (moddecls (verilog-modi-get-decls modi))
10364 (modsubdecls (verilog-modi-get-sub-decls modi))
10365 (sig-list (verilog-signals-not-in
10366 (verilog-decls-get-outputs moddecls)
10367 (append (verilog-decls-get-wires moddecls)
10368 (verilog-decls-get-regs moddecls)
10369 (verilog-decls-get-assigns moddecls)
10370 (verilog-decls-get-consts moddecls)
10371 (verilog-decls-get-gparams moddecls)
10372 (verilog-subdecls-get-outputs modsubdecls)
10373 (verilog-subdecls-get-inouts modsubdecls)))))
10374 (when sig-list
10375 (forward-line 1)
10376 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
10377 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
10378 (verilog-modi-cache-add-wires modi sig-list) ; Before we trash list
10379 (while sig-list
10380 (let ((sig (car sig-list)))
10381 (verilog-insert-one-definition sig "wire" indent-pt)
10382 (indent-to (max 48 (+ indent-pt 40)))
10383 (insert "= " (verilog-sig-tieoff sig)
10384 ";\n")
10385 (setq sig-list (cdr sig-list))))
10386 (verilog-insert-indent "// End of automatics\n")))))
10387
10388 (defun verilog-auto-unused ()
10389 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
10390 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
10391 input and inout signals.
10392
10393 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
10394 input/output list as another module, but no internals. Specifically, it
10395 finds all inputs and inouts in the module, and if that input is not otherwise
10396 used, adds it to a comma separated list.
10397
10398 The comma separated list is intended to be used to create a _unused_ok
10399 signal. Using the exact name \"_unused_ok\" for name of the temporary
10400 signal is recommended as it will insure maximum forward compatibility, it
10401 also makes lint warnings easy to understand; ignore any unused warnings
10402 with \"unused\" in the signal name.
10403
10404 To reduce simulation time, the _unused_ok signal should be forced to a
10405 constant to prevent wiggling. The easiest thing to do is use a
10406 reduction-and with 1'b0 as shown.
10407
10408 This way all unused signals are in one place, making it convenient to add
10409 your tool's specific pragmas around the assignment to disable any unused
10410 warnings.
10411
10412 You can add signals you do not want included in AUTOUNUSED with
10413 `verilog-auto-unused-ignore-regexp'.
10414
10415 An example of making a stub for another module:
10416
10417 module ExampStub (/*AUTOINST*/);
10418 /*AUTOINOUTMODULE(\"Examp\")*/
10419 /*AUTOTIEOFF*/
10420 // verilator lint_off UNUSED
10421 wire _unused_ok = &{1'b0,
10422 /*AUTOUNUSED*/
10423 1'b0};
10424 // verilator lint_on UNUSED
10425 endmodule
10426
10427 Typing \\[verilog-auto] will make this into:
10428
10429 ...
10430 // verilator lint_off UNUSED
10431 wire _unused_ok = &{1'b0,
10432 /*AUTOUNUSED*/
10433 // Beginning of automatics
10434 unused_input_a,
10435 unused_input_b,
10436 unused_input_c,
10437 // End of automatics
10438 1'b0};
10439 // verilator lint_on UNUSED
10440 endmodule"
10441 (interactive)
10442 (save-excursion
10443 ;; Find beginning
10444 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
10445 (modi (verilog-modi-current))
10446 (moddecls (verilog-modi-get-decls modi))
10447 (modsubdecls (verilog-modi-get-sub-decls modi))
10448 (sig-list (verilog-signals-not-in
10449 (append (verilog-decls-get-inputs moddecls)
10450 (verilog-decls-get-inouts moddecls))
10451 (append (verilog-subdecls-get-inputs modsubdecls)
10452 (verilog-subdecls-get-inouts modsubdecls)))))
10453 (setq sig-list (verilog-signals-not-matching-regexp
10454 sig-list verilog-auto-unused-ignore-regexp))
10455 (when sig-list
10456 (forward-line 1)
10457 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
10458 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
10459 (while sig-list
10460 (let ((sig (car sig-list)))
10461 (indent-to indent-pt)
10462 (insert (verilog-sig-name sig) ",\n")
10463 (setq sig-list (cdr sig-list))))
10464 (verilog-insert-indent "// End of automatics\n")))))
10465
10466 (defun verilog-enum-ascii (signm elim-regexp)
10467 "Convert an enum name SIGNM to an ascii string for insertion.
10468 Remove user provided prefix ELIM-REGEXP."
10469 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
10470 (let ((case-fold-search t))
10471 ;; All upper becomes all lower for readability
10472 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
10473
10474 (defun verilog-auto-ascii-enum ()
10475 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
10476 Create a register to contain the ASCII decode of a enumerated signal type.
10477 This will allow trace viewers to show the ASCII name of states.
10478
10479 First, parameters are built into a enumeration using the synopsys enum
10480 comment. The comment must be between the keyword and the symbol.
10481 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
10482
10483 Next, registers which that enum applies to are also tagged with the same
10484 enum. Synopsys also suggests labeling state vectors, but `verilog-mode'
10485 doesn't care.
10486
10487 Finally, a AUTOASCIIENUM command is used.
10488
10489 The first parameter is the name of the signal to be decoded.
10490 If and only if the first parameter width is 2^(number of states
10491 in enum) and does NOT match the width of the enum, the signal
10492 is assumed to be a one hot decode. Otherwise, it's a normal
10493 encoded state vector.
10494
10495 The second parameter is the name to store the ASCII code into. For the
10496 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
10497 a signal that is just for simulation, and the magic characters _ascii
10498 tell viewers like Dinotrace to display in ASCII format.
10499
10500 The final optional parameter is a string which will be removed from the
10501 state names.
10502
10503 An example:
10504
10505 //== State enumeration
10506 parameter [2:0] // synopsys enum state_info
10507 SM_IDLE = 3'b000,
10508 SM_SEND = 3'b001,
10509 SM_WAIT1 = 3'b010;
10510 //== State variables
10511 reg [2:0] /* synopsys enum state_info */
10512 state_r; /* synopsys state_vector state_r */
10513 reg [2:0] /* synopsys enum state_info */
10514 state_e1;
10515
10516 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
10517
10518 Typing \\[verilog-auto] will make this into:
10519
10520 ... same front matter ...
10521
10522 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
10523 // Beginning of automatic ASCII enum decoding
10524 reg [39:0] state_ascii_r; // Decode of state_r
10525 always @(state_r) begin
10526 case ({state_r})
10527 SM_IDLE: state_ascii_r = \"idle \";
10528 SM_SEND: state_ascii_r = \"send \";
10529 SM_WAIT1: state_ascii_r = \"wait1\";
10530 default: state_ascii_r = \"%Erro\";
10531 endcase
10532 end
10533 // End of automatics"
10534 (save-excursion
10535 (let* ((params (verilog-read-auto-params 2 3))
10536 (undecode-name (nth 0 params))
10537 (ascii-name (nth 1 params))
10538 (elim-regexp (nth 2 params))
10539 ;;
10540 (indent-pt (current-indentation))
10541 (modi (verilog-modi-current))
10542 (moddecls (verilog-modi-get-decls modi))
10543 ;;
10544 (sig-list-consts (append (verilog-decls-get-consts moddecls)
10545 (verilog-decls-get-gparams moddecls)))
10546 (sig-list-all (append (verilog-decls-get-regs moddecls)
10547 (verilog-decls-get-outputs moddecls)
10548 (verilog-decls-get-inouts moddecls)
10549 (verilog-decls-get-inputs moddecls)
10550 (verilog-decls-get-wires moddecls)))
10551 ;;
10552 (undecode-sig (or (assoc undecode-name sig-list-all)
10553 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
10554 (undecode-enum (or (verilog-sig-enum undecode-sig)
10555 (error "%s: Signal %s does not have a enum tag" (verilog-point-text) undecode-name)))
10556 ;;
10557 (enum-sigs (verilog-signals-not-in
10558 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
10559 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum))
10560 nil))
10561 ;;
10562 (one-hot (and ;; width(enum) != width(sig)
10563 (or (not (verilog-sig-bits (car enum-sigs)))
10564 (not (equal (verilog-sig-width (car enum-sigs))
10565 (verilog-sig-width undecode-sig))))
10566 ;; count(enums) == width(sig)
10567 (equal (number-to-string (length enum-sigs))
10568 (verilog-sig-width undecode-sig))))
10569 (enum-chars 0)
10570 (ascii-chars 0))
10571 ;;
10572 ;; Find number of ascii chars needed
10573 (let ((tmp-sigs enum-sigs))
10574 (while tmp-sigs
10575 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
10576 ascii-chars (max ascii-chars (length (verilog-enum-ascii
10577 (verilog-sig-name (car tmp-sigs))
10578 elim-regexp)))
10579 tmp-sigs (cdr tmp-sigs))))
10580 ;;
10581 (forward-line 1)
10582 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
10583 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
10584 (concat "Decode of " undecode-name) nil nil))))
10585 (verilog-insert-definition decode-sig-list "reg" indent-pt nil)
10586 (verilog-modi-cache-add-regs modi decode-sig-list))
10587 ;;
10588 (verilog-insert-indent "always @(" undecode-name ") begin\n")
10589 (setq indent-pt (+ indent-pt verilog-indent-level))
10590 (indent-to indent-pt)
10591 (insert "case ({" undecode-name "})\n")
10592 (setq indent-pt (+ indent-pt verilog-case-indent))
10593 ;;
10594 (let ((tmp-sigs enum-sigs)
10595 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
10596 (+ (if one-hot 9 1) (max 8 enum-chars))
10597 ascii-name ascii-chars))
10598 (errname (substring "%Error" 0 (min 6 ascii-chars))))
10599 (while tmp-sigs
10600 (verilog-insert-indent
10601 (concat
10602 (format chrfmt
10603 (concat (if one-hot "(")
10604 (if one-hot (verilog-sig-width undecode-sig))
10605 ;; We use a shift instead of var[index]
10606 ;; so that a non-one hot value will show as error.
10607 (if one-hot "'b1<<")
10608 (verilog-sig-name (car tmp-sigs))
10609 (if one-hot ")") ":")
10610 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
10611 elim-regexp))))
10612 (setq tmp-sigs (cdr tmp-sigs)))
10613 (verilog-insert-indent (format chrfmt "default:" errname)))
10614 ;;
10615 (setq indent-pt (- indent-pt verilog-case-indent))
10616 (verilog-insert-indent "endcase\n")
10617 (setq indent-pt (- indent-pt verilog-indent-level))
10618 (verilog-insert-indent "end\n"
10619 "// End of automatics\n"))))
10620
10621 (defun verilog-auto-templated-rel ()
10622 "Replace Templated relative line numbers with absolute line numbers.
10623 Internal use only. This hacks around the line numbers in AUTOINST Templates
10624 being different from the final output's line numbering."
10625 (let ((templateno 0) (template-line (list 0)))
10626 ;; Find line number each template is on
10627 (goto-char (point-min))
10628 (while (search-forward "AUTO_TEMPLATE" nil t)
10629 (setq templateno (1+ templateno))
10630 (setq template-line
10631 (cons (count-lines (point-min) (point)) template-line)))
10632 (setq template-line (nreverse template-line))
10633 ;; Replace T# L# with absolute line number
10634 (goto-char (point-min))
10635 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
10636 (replace-match
10637 (concat " Templated "
10638 (int-to-string (+ (nth (string-to-number (match-string 1))
10639 template-line)
10640 (string-to-number (match-string 2)))))
10641 t t))))
10642
10643 \f
10644 ;;
10645 ;; Auto top level
10646 ;;
10647
10648 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing a arg
10649 "Expand AUTO statements.
10650 Look for any /*AUTO...*/ commands in the code, as used in
10651 instantiations or argument headers. Update the list of signals
10652 following the /*AUTO...*/ command.
10653
10654 Use \\[verilog-delete-auto] to remove the AUTOs.
10655
10656 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
10657
10658 Use \\[verilog-faq] for a pointer to frequently asked questions.
10659
10660 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
10661 called before and after this function, respectively.
10662
10663 For example:
10664 module ModuleName (/*AUTOARG*/)
10665 /*AUTOINPUT*/
10666 /*AUTOOUTPUT*/
10667 /*AUTOWIRE*/
10668 /*AUTOREG*/
10669 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
10670
10671 You can also update the AUTOs from the shell using:
10672 emacs --batch <filenames.v> -f verilog-batch-auto
10673 Or fix indentation with:
10674 emacs --batch <filenames.v> -f verilog-batch-indent
10675 Likewise, you can delete or inject AUTOs with:
10676 emacs --batch <filenames.v> -f verilog-batch-delete-auto
10677 emacs --batch <filenames.v> -f verilog-batch-inject-auto
10678
10679 Using \\[describe-function], see also:
10680 `verilog-auto-arg' for AUTOARG module instantiations
10681 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
10682 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
10683 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
10684 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
10685 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
10686 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
10687 `verilog-auto-inst' for AUTOINST instantiation pins
10688 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
10689 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
10690 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
10691 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
10692 `verilog-auto-reg' for AUTOREG registers
10693 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
10694 `verilog-auto-reset' for AUTORESET flop resets
10695 `verilog-auto-sense' for AUTOSENSE always sensitivity lists
10696 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
10697 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
10698 `verilog-auto-wire' for AUTOWIRE instantiation wires
10699
10700 `verilog-read-defines' for reading `define values
10701 `verilog-read-includes' for reading `includes
10702
10703 If you have bugs with these autos, please file an issue at
10704 URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
10705 Wilson Snyder (wsnyder@wsnyder.org)."
10706 (interactive)
10707 (unless noninteractive (message "Updating AUTOs..."))
10708 (if (fboundp 'dinotrace-unannotate-all)
10709 (dinotrace-unannotate-all))
10710 (let ((oldbuf (if (not (buffer-modified-p))
10711 (buffer-string)))
10712 ;; Before version 20, match-string with font-lock returns a
10713 ;; vector that is not equal to the string. IE if on "input"
10714 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
10715 (fontlocked (when (and (boundp 'font-lock-mode)
10716 font-lock-mode)
10717 (font-lock-mode 0)
10718 t))
10719 ;; Cache directories; we don't write new files, so can't change
10720 (verilog-dir-cache-preserving t))
10721 (unwind-protect
10722 (save-excursion
10723 ;; If we're not in verilog-mode, change syntax table so parsing works right
10724 (unless (eq major-mode `verilog-mode) (verilog-mode))
10725 ;; Allow user to customize
10726 (run-hooks 'verilog-before-auto-hook)
10727 ;; Try to save the user from needing to revert-file to reread file local-variables
10728 (verilog-auto-reeval-locals)
10729 (verilog-read-auto-lisp (point-min) (point-max))
10730 (verilog-getopt-flags)
10731 ;; From here on out, we can cache anything we read from disk
10732 (verilog-preserve-dir-cache
10733 ;; These two may seem obvious to do always, but on large includes it can be way too slow
10734 (when verilog-auto-read-includes
10735 (verilog-read-includes)
10736 (verilog-read-defines nil nil t))
10737 ;; This particular ordering is important
10738 ;; INST: Lower modules correct, no internal dependencies, FIRST
10739 (verilog-preserve-modi-cache
10740 ;; Clear existing autos else we'll be screwed by existing ones
10741 (verilog-delete-auto)
10742 ;; Injection if appropriate
10743 (when inject
10744 (verilog-inject-inst)
10745 (verilog-inject-sense)
10746 (verilog-inject-arg))
10747 ;;
10748 ;; Do user inserts first, so their code can insert AUTOs
10749 ;; We may provide a AUTOINSERTLISPLAST if another cleanup pass is needed
10750 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
10751 'verilog-auto-insert-lisp)
10752 ;; Expand instances before need the signals the instances input/output
10753 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
10754 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
10755 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
10756 ;; Doesn't matter when done, but combine it with a common changer
10757 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
10758 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
10759 ;; Must be done before autoin/out as creates a reg
10760 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM([^)]*)\\*/" 'verilog-auto-ascii-enum)
10761 ;;
10762 ;; first in/outs from other files
10763 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE([^)]*)\\*/" 'verilog-auto-inout-module)
10764 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP([^)]*)\\*/" 'verilog-auto-inout-comp)
10765 ;; next in/outs which need previous sucked inputs first
10766 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((\"[^\"]*\")\\)\\*/"
10767 '(lambda () (verilog-auto-output t)))
10768 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\*/" 'verilog-auto-output)
10769 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((\"[^\"]*\")\\)\\*/"
10770 '(lambda () (verilog-auto-input t)))
10771 (verilog-auto-re-search-do "/\\*AUTOINPUT\\*/" 'verilog-auto-input)
10772 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((\"[^\"]*\")\\)\\*/"
10773 '(lambda () (verilog-auto-inout t)))
10774 (verilog-auto-re-search-do "/\\*AUTOINOUT\\*/" 'verilog-auto-inout)
10775 ;; Then tie off those in/outs
10776 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
10777 ;; Wires/regs must be after inputs/outputs
10778 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
10779 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
10780 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
10781 ;; outputevery needs AUTOOUTPUTs done first
10782 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\*/" 'verilog-auto-output-every)
10783 ;; After we've created all new variables
10784 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
10785 ;; Must be after all inputs outputs are generated
10786 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
10787 ;; Fix line numbers (comments only)
10788 (verilog-auto-templated-rel)))
10789 ;;
10790 (run-hooks 'verilog-auto-hook)
10791 ;;
10792 (set (make-local-variable 'verilog-auto-update-tick) (buffer-modified-tick))
10793 ;;
10794 ;; If end result is same as when started, clear modified flag
10795 (cond ((and oldbuf (equal oldbuf (buffer-string)))
10796 (set-buffer-modified-p nil)
10797 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
10798 (t (unless noninteractive (message "Updating AUTOs...done")))))
10799 ;; Unwind forms
10800 (progn
10801 ;; Restore font-lock
10802 (when fontlocked (font-lock-mode t))))))
10803 \f
10804
10805 ;;
10806 ;; Skeleton based code insertion
10807 ;;
10808 (defvar verilog-template-map
10809 (let ((map (make-sparse-keymap)))
10810 (define-key map "a" 'verilog-sk-always)
10811 (define-key map "b" 'verilog-sk-begin)
10812 (define-key map "c" 'verilog-sk-case)
10813 (define-key map "f" 'verilog-sk-for)
10814 (define-key map "g" 'verilog-sk-generate)
10815 (define-key map "h" 'verilog-sk-header)
10816 (define-key map "i" 'verilog-sk-initial)
10817 (define-key map "j" 'verilog-sk-fork)
10818 (define-key map "m" 'verilog-sk-module)
10819 (define-key map "p" 'verilog-sk-primitive)
10820 (define-key map "r" 'verilog-sk-repeat)
10821 (define-key map "s" 'verilog-sk-specify)
10822 (define-key map "t" 'verilog-sk-task)
10823 (define-key map "w" 'verilog-sk-while)
10824 (define-key map "x" 'verilog-sk-casex)
10825 (define-key map "z" 'verilog-sk-casez)
10826 (define-key map "?" 'verilog-sk-if)
10827 (define-key map ":" 'verilog-sk-else-if)
10828 (define-key map "/" 'verilog-sk-comment)
10829 (define-key map "A" 'verilog-sk-assign)
10830 (define-key map "F" 'verilog-sk-function)
10831 (define-key map "I" 'verilog-sk-input)
10832 (define-key map "O" 'verilog-sk-output)
10833 (define-key map "S" 'verilog-sk-state-machine)
10834 (define-key map "=" 'verilog-sk-inout)
10835 (define-key map "W" 'verilog-sk-wire)
10836 (define-key map "R" 'verilog-sk-reg)
10837 (define-key map "D" 'verilog-sk-define-signal)
10838 map)
10839 "Keymap used in Verilog mode for smart template operations.")
10840
10841
10842 ;;
10843 ;; Place the templates into Verilog Mode. They may be inserted under any key.
10844 ;; C-c C-t will be the default. If you use templates a lot, you
10845 ;; may want to consider moving the binding to another key in your .emacs
10846 ;; file.
10847 ;;
10848 ;(define-key verilog-mode-map "\C-ct" verilog-template-map)
10849 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
10850
10851 ;;; ---- statement skeletons ------------------------------------------
10852
10853 (define-skeleton verilog-sk-prompt-condition
10854 "Prompt for the loop condition."
10855 "[condition]: " str )
10856
10857 (define-skeleton verilog-sk-prompt-init
10858 "Prompt for the loop init statement."
10859 "[initial statement]: " str )
10860
10861 (define-skeleton verilog-sk-prompt-inc
10862 "Prompt for the loop increment statement."
10863 "[increment statement]: " str )
10864
10865 (define-skeleton verilog-sk-prompt-name
10866 "Prompt for the name of something."
10867 "[name]: " str)
10868
10869 (define-skeleton verilog-sk-prompt-clock
10870 "Prompt for the name of something."
10871 "name and edge of clock(s): " str)
10872
10873 (defvar verilog-sk-reset nil)
10874 (defun verilog-sk-prompt-reset ()
10875 "Prompt for the name of a state machine reset."
10876 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
10877
10878
10879 (define-skeleton verilog-sk-prompt-state-selector
10880 "Prompt for the name of a state machine selector."
10881 "name of selector (eg {a,b,c,d}): " str )
10882
10883 (define-skeleton verilog-sk-prompt-output
10884 "Prompt for the name of something."
10885 "output: " str)
10886
10887 (define-skeleton verilog-sk-prompt-msb
10888 "Prompt for least significant bit specification."
10889 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
10890
10891 (define-skeleton verilog-sk-prompt-lsb
10892 "Prompt for least significant bit specification."
10893 "lsb:" str )
10894
10895 (defvar verilog-sk-p nil)
10896 (define-skeleton verilog-sk-prompt-width
10897 "Prompt for a width specification."
10898 ()
10899 (progn
10900 (setq verilog-sk-p (point))
10901 (verilog-sk-prompt-msb)
10902 (if (> (point) verilog-sk-p) "] " " ")))
10903
10904 (defun verilog-sk-header ()
10905 "Insert a descriptive header at the top of the file.
10906 See also `verilog-header' for an alternative format."
10907 (interactive "*")
10908 (save-excursion
10909 (goto-char (point-min))
10910 (verilog-sk-header-tmpl)))
10911
10912 (define-skeleton verilog-sk-header-tmpl
10913 "Insert a comment block containing the module title, author, etc."
10914 "[Description]: "
10915 "// -*- Mode: Verilog -*-"
10916 "\n// Filename : " (buffer-name)
10917 "\n// Description : " str
10918 "\n// Author : " (user-full-name)
10919 "\n// Created On : " (current-time-string)
10920 "\n// Last Modified By: " (user-full-name)
10921 "\n// Last Modified On: " (current-time-string)
10922 "\n// Update Count : 0"
10923 "\n// Status : Unknown, Use with caution!"
10924 "\n")
10925
10926 (define-skeleton verilog-sk-module
10927 "Insert a module definition."
10928 ()
10929 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
10930 > _ \n
10931 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
10932
10933 (define-skeleton verilog-sk-primitive
10934 "Insert a task definition."
10935 ()
10936 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
10937 > _ \n
10938 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
10939
10940 (define-skeleton verilog-sk-task
10941 "Insert a task definition."
10942 ()
10943 > "task " '(verilog-sk-prompt-name) & ?; \n
10944 > _ \n
10945 > "begin" \n
10946 > \n
10947 > (- verilog-indent-level-behavioral) "end" \n
10948 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
10949
10950 (define-skeleton verilog-sk-function
10951 "Insert a function definition."
10952 ()
10953 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
10954 > _ \n
10955 > "begin" \n
10956 > \n
10957 > (- verilog-indent-level-behavioral) "end" \n
10958 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
10959
10960 (define-skeleton verilog-sk-always
10961 "Insert always block. Uses the minibuffer to prompt
10962 for sensitivity list."
10963 ()
10964 > "always @ ( /*AUTOSENSE*/ ) begin\n"
10965 > _ \n
10966 > (- verilog-indent-level-behavioral) "end" \n >
10967 )
10968
10969 (define-skeleton verilog-sk-initial
10970 "Insert an initial block."
10971 ()
10972 > "initial begin\n"
10973 > _ \n
10974 > (- verilog-indent-level-behavioral) "end" \n > )
10975
10976 (define-skeleton verilog-sk-specify
10977 "Insert specify block. "
10978 ()
10979 > "specify\n"
10980 > _ \n
10981 > (- verilog-indent-level-behavioral) "endspecify" \n > )
10982
10983 (define-skeleton verilog-sk-generate
10984 "Insert generate block. "
10985 ()
10986 > "generate\n"
10987 > _ \n
10988 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
10989
10990 (define-skeleton verilog-sk-begin
10991 "Insert begin end block. Uses the minibuffer to prompt for name."
10992 ()
10993 > "begin" '(verilog-sk-prompt-name) \n
10994 > _ \n
10995 > (- verilog-indent-level-behavioral) "end"
10996 )
10997
10998 (define-skeleton verilog-sk-fork
10999 "Insert a fork join block."
11000 ()
11001 > "fork\n"
11002 > "begin" \n
11003 > _ \n
11004 > (- verilog-indent-level-behavioral) "end" \n
11005 > "begin" \n
11006 > \n
11007 > (- verilog-indent-level-behavioral) "end" \n
11008 > (- verilog-indent-level-behavioral) "join" \n
11009 > )
11010
11011
11012 (define-skeleton verilog-sk-case
11013 "Build skeleton case statement, prompting for the selector expression,
11014 and the case items."
11015 "[selector expression]: "
11016 > "case (" str ") " \n
11017 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
11018 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
11019
11020 (define-skeleton verilog-sk-casex
11021 "Build skeleton casex statement, prompting for the selector expression,
11022 and the case items."
11023 "[selector expression]: "
11024 > "casex (" str ") " \n
11025 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
11026 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
11027
11028 (define-skeleton verilog-sk-casez
11029 "Build skeleton casez statement, prompting for the selector expression,
11030 and the case items."
11031 "[selector expression]: "
11032 > "casez (" str ") " \n
11033 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
11034 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
11035
11036 (define-skeleton verilog-sk-if
11037 "Insert a skeleton if statement."
11038 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
11039 > _ \n
11040 > (- verilog-indent-level-behavioral) "end " \n )
11041
11042 (define-skeleton verilog-sk-else-if
11043 "Insert a skeleton else if statement."
11044 > (verilog-indent-line) "else if ("
11045 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
11046 > _ \n
11047 > "end" (progn (electric-verilog-terminate-line) nil))
11048
11049 (define-skeleton verilog-sk-datadef
11050 "Common routine to get data definition."
11051 ()
11052 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
11053
11054 (define-skeleton verilog-sk-input
11055 "Insert an input definition."
11056 ()
11057 > "input [" '(verilog-sk-datadef))
11058
11059 (define-skeleton verilog-sk-output
11060 "Insert an output definition."
11061 ()
11062 > "output [" '(verilog-sk-datadef))
11063
11064 (define-skeleton verilog-sk-inout
11065 "Insert an inout definition."
11066 ()
11067 > "inout [" '(verilog-sk-datadef))
11068
11069 (defvar verilog-sk-signal nil)
11070 (define-skeleton verilog-sk-def-reg
11071 "Insert a reg definition."
11072 ()
11073 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations) )
11074
11075 (defun verilog-sk-define-signal ()
11076 "Insert a definition of signal under point at top of module."
11077 (interactive "*")
11078 (let* ((sig-re "[a-zA-Z0-9_]*")
11079 (v1 (buffer-substring
11080 (save-excursion
11081 (skip-chars-backward sig-re)
11082 (point))
11083 (save-excursion
11084 (skip-chars-forward sig-re)
11085 (point)))))
11086 (if (not (member v1 verilog-keywords))
11087 (save-excursion
11088 (setq verilog-sk-signal v1)
11089 (verilog-beg-of-defun)
11090 (verilog-end-of-statement)
11091 (verilog-forward-syntactic-ws)
11092 (verilog-sk-def-reg)
11093 (message "signal at point is %s" v1))
11094 (message "object at point (%s) is a keyword" v1))))
11095
11096 (define-skeleton verilog-sk-wire
11097 "Insert a wire definition."
11098 ()
11099 > "wire [" '(verilog-sk-datadef))
11100
11101 (define-skeleton verilog-sk-reg
11102 "Insert a reg definition."
11103 ()
11104 > "reg [" '(verilog-sk-datadef))
11105
11106 (define-skeleton verilog-sk-assign
11107 "Insert a skeleton assign statement."
11108 ()
11109 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
11110
11111 (define-skeleton verilog-sk-while
11112 "Insert a skeleton while loop statement."
11113 ()
11114 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
11115 > _ \n
11116 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
11117
11118 (define-skeleton verilog-sk-repeat
11119 "Insert a skeleton repeat loop statement."
11120 ()
11121 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
11122 > _ \n
11123 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
11124
11125 (define-skeleton verilog-sk-for
11126 "Insert a skeleton while loop statement."
11127 ()
11128 > "for ("
11129 '(verilog-sk-prompt-init) "; "
11130 '(verilog-sk-prompt-condition) "; "
11131 '(verilog-sk-prompt-inc)
11132 ") begin" \n
11133 > _ \n
11134 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
11135
11136 (define-skeleton verilog-sk-comment
11137 "Inserts three comment lines, making a display comment."
11138 ()
11139 > "/*\n"
11140 > "* " _ \n
11141 > "*/")
11142
11143 (define-skeleton verilog-sk-state-machine
11144 "Insert a state machine definition."
11145 "Name of state variable: "
11146 '(setq input "state")
11147 > "// State registers for " str | -23 \n
11148 '(setq verilog-sk-state str)
11149 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
11150 '(setq input nil)
11151 > \n
11152 > "// State FF for " verilog-sk-state \n
11153 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
11154 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
11155 > verilog-sk-state " = next_" verilog-sk-state ?; \n
11156 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
11157 > \n
11158 > "// Next State Logic for " verilog-sk-state \n
11159 > "always @ ( /*AUTOSENSE*/ ) begin\n"
11160 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
11161 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
11162 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
11163 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
11164 \f
11165
11166 ;;
11167 ;; Include file loading with mouse/return event
11168 ;;
11169 ;; idea & first impl.: M. Rouat (eldo-mode.el)
11170 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
11171
11172 (if (featurep 'xemacs)
11173 (require 'overlay))
11174
11175 (defconst verilog-include-file-regexp
11176 "^`include\\s-+\"\\([^\n\"]*\\)\""
11177 "Regexp that matches the include file.")
11178
11179 (defvar verilog-mode-mouse-map
11180 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
11181 (set-keymap-parent map verilog-mode-map)
11182 ;; mouse button bindings
11183 (define-key map "\r" 'verilog-load-file-at-point)
11184 (if (featurep 'xemacs)
11185 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
11186 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
11187 (if (featurep 'xemacs)
11188 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
11189 (define-key map [S-mouse-2] 'mouse-yank-at-click))
11190 map)
11191 "Map containing mouse bindings for `verilog-mode'.")
11192
11193
11194 (defun verilog-colorize-include-files (beg end old-len)
11195 "This function colorizes included files when the mouse passes over them.
11196 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
11197 (save-excursion
11198 (save-match-data
11199 (let (end-point)
11200 (goto-char end)
11201 (setq end-point (verilog-get-end-of-line))
11202 (goto-char beg)
11203 (beginning-of-line) ; scan entire line !
11204 ;; delete overlays existing on this line
11205 (let ((overlays (overlays-in (point) end-point)))
11206 (while overlays
11207 (if (and
11208 (overlay-get (car overlays) 'detachable)
11209 (overlay-get (car overlays) 'verilog-include-file))
11210 (delete-overlay (car overlays)))
11211 (setq overlays (cdr overlays)))) ; let
11212 ;; make new ones, could reuse deleted one ?
11213 (while (search-forward-regexp verilog-include-file-regexp end-point t)
11214 (let (ov)
11215 (goto-char (match-beginning 1))
11216 (setq ov (make-overlay (match-beginning 1) (match-end 1)))
11217 (overlay-put ov 'start-closed 't)
11218 (overlay-put ov 'end-closed 't)
11219 (overlay-put ov 'evaporate 't)
11220 (overlay-put ov 'verilog-include-file 't)
11221 (overlay-put ov 'mouse-face 'highlight)
11222 (overlay-put ov 'local-map verilog-mode-mouse-map)))))))
11223
11224
11225 (defun verilog-colorize-include-files-buffer ()
11226 "Colorize an include file."
11227 (interactive)
11228 ;; delete overlays
11229 (let ((overlays (overlays-in (point-min) (point-max))))
11230 (while overlays
11231 (if (and
11232 (overlay-get (car overlays) 'detachable)
11233 (overlay-get (car overlays) 'verilog-include-file))
11234 (delete-overlay (car overlays)))
11235 (setq overlays (cdr overlays)))) ; let
11236 ;; remake overlays
11237 (verilog-colorize-include-files (point-min) (point-max) nil))
11238
11239 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
11240 ;; so define this function to do more or less the same as ffap-at-mouse
11241 ;; but first resolve filename...
11242 (defun verilog-load-file-at-mouse (event)
11243 "Load file under button 2 click's EVENT.
11244 Files are checked based on `verilog-library-directories'."
11245 (interactive "@e")
11246 (save-excursion ;; implement a Verilog specific ffap-at-mouse
11247 (mouse-set-point event)
11248 (beginning-of-line)
11249 (if (looking-at verilog-include-file-regexp)
11250 (if (and (car (verilog-library-filenames
11251 (match-string 1) (buffer-file-name)))
11252 (file-readable-p (car (verilog-library-filenames
11253 (match-string 1) (buffer-file-name)))))
11254 (find-file (car (verilog-library-filenames
11255 (match-string 1) (buffer-file-name))))
11256 (progn
11257 (message
11258 "File '%s' isn't readable, use shift-mouse2 to paste in this field"
11259 (match-string 1)))))))
11260
11261 ;; ffap isn't useable for Verilog mode. It uses library paths.
11262 ;; so define this function to do more or less the same as ffap
11263 ;; but first resolve filename...
11264 (defun verilog-load-file-at-point ()
11265 "Load file under point.
11266 Files are checked based on `verilog-library-directories'."
11267 (interactive)
11268 (save-excursion ;; implement a Verilog specific ffap
11269 (beginning-of-line)
11270 (if (looking-at verilog-include-file-regexp)
11271 (if (and
11272 (car (verilog-library-filenames
11273 (match-string 1) (buffer-file-name)))
11274 (file-readable-p (car (verilog-library-filenames
11275 (match-string 1) (buffer-file-name)))))
11276 (find-file (car (verilog-library-filenames
11277 (match-string 1) (buffer-file-name))))))))
11278
11279
11280 ;;
11281 ;; Bug reporting
11282 ;;
11283
11284 (defun verilog-faq ()
11285 "Tell the user their current version, and where to get the FAQ etc."
11286 (interactive)
11287 (with-output-to-temp-buffer "*verilog-mode help*"
11288 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
11289 (princ "\n")
11290 (princ "For new releases, see http://www.verilog.com\n")
11291 (princ "\n")
11292 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
11293 (princ "\n")
11294 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
11295 (princ "\n")))
11296
11297 (autoload 'reporter-submit-bug-report "reporter")
11298 (defvar reporter-prompt-for-summary-p)
11299
11300 (defun verilog-submit-bug-report ()
11301 "Submit via mail a bug report on verilog-mode.el."
11302 (interactive)
11303 (let ((reporter-prompt-for-summary-p t))
11304 (reporter-submit-bug-report
11305 "mac@verilog.com, wsnyder@wsnyder.org"
11306 (concat "verilog-mode v" verilog-mode-version)
11307 '(
11308 verilog-align-ifelse
11309 verilog-auto-endcomments
11310 verilog-auto-hook
11311 verilog-auto-indent-on-newline
11312 verilog-auto-inst-vector
11313 verilog-auto-inst-template-numbers
11314 verilog-auto-lineup
11315 verilog-auto-newline
11316 verilog-auto-save-policy
11317 verilog-auto-sense-defines-constant
11318 verilog-auto-sense-include-inputs
11319 verilog-before-auto-hook
11320 verilog-case-indent
11321 verilog-cexp-indent
11322 verilog-compiler
11323 verilog-coverage
11324 verilog-highlight-translate-off
11325 verilog-indent-begin-after-if
11326 verilog-indent-declaration-macros
11327 verilog-indent-level
11328 verilog-indent-level-behavioral
11329 verilog-indent-level-declaration
11330 verilog-indent-level-directive
11331 verilog-indent-level-module
11332 verilog-indent-lists
11333 verilog-library-flags
11334 verilog-library-directories
11335 verilog-library-extensions
11336 verilog-library-files
11337 verilog-linter
11338 verilog-minimum-comment-distance
11339 verilog-mode-hook
11340 verilog-simulator
11341 verilog-tab-always-indent
11342 verilog-tab-to-comment
11343 )
11344 nil nil
11345 (concat "Hi Mac,
11346
11347 I want to report a bug.
11348
11349 Before I go further, I want to say that Verilog mode has changed my life.
11350 I save so much time, my files are colored nicely, my co workers respect
11351 my coding ability... until now. I'd really appreciate anything you
11352 could do to help me out with this minor deficiency in the product.
11353
11354 I've taken a look at the Verilog-Mode FAQ at
11355 http://www.veripool.org/verilog-mode-faq.html.
11356
11357 And, I've considered filing the bug on the issue tracker at
11358 http://www.veripool.org/verilog-mode-bugs
11359 since I realize that public bugs are easier for you to track,
11360 and for others to search, but would prefer to email.
11361
11362 So, to reproduce the bug, start a fresh Emacs via " invocation-name "
11363 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
11364 the code included below.
11365
11366 Given those lines, I expected [[Fill in here]] to happen;
11367 but instead, [[Fill in here]] happens!.
11368
11369 == The code: =="))))
11370
11371 (provide 'verilog-mode)
11372
11373 ;; Local Variables:
11374 ;; checkdoc-permit-comma-termination-flag:t
11375 ;; checkdoc-force-docstrings-flag:nil
11376 ;; End:
11377
11378 ;; arch-tag: 87923725-57b3-41b5-9494-be21118c6a6f
11379 ;;; verilog-mode.el ends here