Skip to content

Commit

Permalink
Code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlarumbe committed Oct 18, 2023
1 parent c1aad89 commit 885917c
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 312 deletions.
5 changes: 3 additions & 2 deletions verilog-ext-beautify.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
;; Indent, align parameters and ports:
;; - Interactively for module at point
;; - Interactively for current buffer
;; - In batch for files of current directory
;; - In batch for list of files and files of current directory

;;; Code:

Expand Down Expand Up @@ -136,7 +136,8 @@ FILES is a list of strings containing the filepaths."
(dolist (file files)
(with-temp-file file
(insert-file-contents file)
(verilog-mode)
(verilog-ext-with-no-hooks
(verilog-mode))
(verilog-ext-beautify-current-buffer)))))

(defun verilog-ext-beautify-dir-files (dir)
Expand Down
2 changes: 1 addition & 1 deletion verilog-ext-block-end-comments.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

;;; Commentary:

;; Block end comments to names mode
;; Convert block end comments to names.

;;; Code:

Expand Down
219 changes: 105 additions & 114 deletions verilog-ext-compile.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@

;;; Commentary:

;; Compilation related SystemVerilog functions.
;; Compilation-related SystemVerilog functions.
;;
;; This file provides functions to perform compilations with syntax highlighting
;; and jump to error based on `compilation-mode'.
;; and jump to error based on `compilation-mode':
;;
;; Some usage examples:
;; - (verilog-ext-compile-verilator (concat "verilator --lint-only " buffer-file-name))
;; - (verilog-ext-compile-iverilog (concat "iverilog " buffer-file-name))
;; - (verilog-ext-compile-verible (concat "verible-verilog-lint " buffer-file-name))
;; - (verilog-ext-compile-slang (concat "slang --color-diagnostics=false " buffer-file-name))
;; - (verilog-ext-compile-svlint (concat "svlint -1 " buffer-file-name))
;; - (verilog-ext-compile-surelog (concat "surelog -parseonly " buffer-file-name))
;; - Interactive functions examples:
;; - `verilog-ext-compile-makefile': prompt to available Makefile targets and compile
;; - `verilog-ext-compile-project': compile using :compile-cmd of `verilog-ext-project-alist'
;;
;; - Non-interactive function usage examples:
;; - (verilog-ext-compile-verilator (concat "verilator --lint-only " buffer-file-name))
;; - (verilog-ext-compile-iverilog (concat "iverilog " buffer-file-name))
;; - (verilog-ext-compile-verible (concat "verible-verilog-lint " buffer-file-name))
;; - (verilog-ext-compile-slang (concat "slang --color-diagnostics=false " buffer-file-name))
;; - (verilog-ext-compile-svlint (concat "svlint -1 " buffer-file-name))
;; - (verilog-ext-compile-surelog (concat "surelog -parseonly " buffer-file-name))
;;
;; It also includes a function to preprocess current buffer: `verilog-ext-preprocess'

;;; Code:

(require 'verilog-mode)
(require 'verilog-ext-template)
(require 'verilog-ext-utils)
(require 'make-mode)


Expand All @@ -59,100 +63,6 @@
:group 'verilog-ext-compile)


;;;; Preprocess
(defun verilog-ext-preprocess ()
"Preprocess current file.
Choose among different available programs and update `verilog-preprocessor'
variable."
(interactive)
(let ((tools-available (seq-filter (lambda (bin)
(executable-find bin))
'("verilator" "iverilog" "vppreproc"))))
(pcase (completing-read "Select tool: " tools-available)
;; Verilator
("verilator" (setq verilog-preprocessor "verilator -E __FLAGS__ __FILE__"))
;; Verilog-Perl
("vppreproc" (setq verilog-preprocessor "vppreproc __FLAGS__ __FILE__"))
;; Icarus Verilog: `iverilog' command syntax requires writing to an output file (defaults to a.out).
("iverilog" (let* ((filename-sans-ext (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))
(iver-out-file (concat (temporary-file-directory) filename-sans-ext "_pp_iver.sv")))
(setq verilog-preprocessor (concat "iverilog -E -o" iver-out-file " __FILE__ && "
"echo \"\" && " ; Add blank line between run command and first preprocessed line
"cat " iver-out-file)))))
(verilog-preprocess)
(pop-to-buffer "*Verilog-Preprocessed*")))


;;;; Makefile
(defun verilog-ext-makefile-create ()
"Create Iverilog/Verilator Yasnippet based Makefile.
Create it only if in a project and the Makefile does not already exist."
(interactive)
(let ((project-root (verilog-ext-buffer-proj-root))
file)
(if project-root
(if (file-exists-p (setq file (file-name-concat project-root "Makefile")))
(error "File %s already exists!" file)
(find-file file)
(verilog-ext-template-insert-yasnippet "verilog"))
(error "Not in a project!"))))

(defun verilog-ext-makefile-compile ()
"Prompt to available Makefile targets and compile.
Compiles them with various verilog regexps."
(interactive)
(let ((makefile (file-name-concat (verilog-ext-buffer-proj-root) "Makefile"))
(makefile-need-target-pickup t) ; Force refresh of makefile targets
target cmd)
(unless (file-exists-p makefile)
(error "%s does not exist!" makefile))
(with-temp-buffer
(insert-file-contents makefile)
(makefile-pickup-targets)
(setq target (completing-read "Target: " makefile-target-table)))
(setq cmd (concat "cd " (verilog-ext-buffer-proj-root) " && make " target))
(compile cmd)))


(defmacro verilog-ext-compile-define-mode (name &rest args)
"Macro to define a compilation derived mode for a Verilog error regexp.
NAME is the name of the created compilation mode.
ARGS is a property list with :desc, :docstring, :compile-re and :buf-name."
(declare (indent 1) (debug 1))
(let ((desc (plist-get args :desc))
(docstring (plist-get args :docstring))
(compile-re (plist-get args :compile-re))
(buf-name (plist-get args :buf-name)))
`(define-compilation-mode ,name ,desc ,docstring
(setq-local compilation-error-regexp-alist (mapcar #'car ,compile-re))
(setq-local compilation-error-regexp-alist-alist ,compile-re)
(when ,buf-name
(rename-buffer ,buf-name))
(setq truncate-lines t)
(goto-char (point-max)))))

(defmacro verilog-ext-compile-define-fn (name &rest args)
"Macro to define a function to compile with error regexp highlighting.
Function will be callable by NAME.
ARGS is a property list."
(declare (indent 1) (debug 1))
(let ((docstring (plist-get args :docstring))
(buf (plist-get args :buf))
(comp-mode (plist-get args :comp-mode)))
`(defun ,name (command)
,docstring
(when (and ,buf (get-buffer ,buf))
(if (y-or-n-p (format "Buffer %s is in use, kill its process and start new compilation?" ,buf))
(kill-buffer ,buf)
(user-error "Aborted")))
(compile command)
(,comp-mode))))


;;;; Compilation-re
(defconst verilog-ext-compile-filename-re "[a-zA-Z0-9-_\\.\\/]+")

Expand Down Expand Up @@ -207,14 +117,52 @@ ARGS is a property list."
(surelog-info2 ,(concat "^\\[\\(?1:INF:\\(?2:[A-Z0-9]+\\)\\)\\]\\s-+") nil nil nil 0 nil (1 compilation-info-face) (2 verilog-ext-compile-msg-code-face)))
"Surelog regexps.")

(defvar verilog-ext-compile-verilator-buf "*verilator*")
(defvar verilog-ext-compile-iverilog-buf "*iverilog*")
(defvar verilog-ext-compile-verible-buf "*verible*")
(defvar verilog-ext-compile-slang-buf "*slang*")
(defvar verilog-ext-compile-svlint-buf "*svlint*")
(defvar verilog-ext-compile-surelog-buf "*surelog*")
(defconst verilog-ext-compile-verilator-buf "*verilator*")
(defconst verilog-ext-compile-iverilog-buf "*iverilog*")
(defconst verilog-ext-compile-verible-buf "*verible*")
(defconst verilog-ext-compile-slang-buf "*slang*")
(defconst verilog-ext-compile-svlint-buf "*svlint*")
(defconst verilog-ext-compile-surelog-buf "*surelog*")

;;;; Compilation-modes and macros
(defmacro verilog-ext-compile-define-mode (name &rest args)
"Macro to define a compilation derived mode for a Verilog error regexp.
NAME is the name of the created compilation mode.
ARGS is a property list with :desc, :docstring, :compile-re and :buf-name."
(declare (indent 1) (debug 1))
(let ((desc (plist-get args :desc))
(docstring (plist-get args :docstring))
(compile-re (plist-get args :compile-re))
(buf-name (plist-get args :buf-name)))
`(define-compilation-mode ,name ,desc ,docstring
(setq-local compilation-error-regexp-alist (mapcar #'car ,compile-re))
(setq-local compilation-error-regexp-alist-alist ,compile-re)
(when ,buf-name
(rename-buffer ,buf-name))
(setq truncate-lines t)
(goto-char (point-max)))))

(defmacro verilog-ext-compile-define-fn (name &rest args)
"Macro to define a function to compile with error regexp highlighting.
Function will be callable by NAME.
ARGS is a property list."
(declare (indent 1) (debug 1))
(let ((docstring (plist-get args :docstring))
(buf (plist-get args :buf))
(comp-mode (plist-get args :comp-mode)))
`(defun ,name (command)
,docstring
(when (and ,buf (get-buffer ,buf))
(if (y-or-n-p (format "Buffer %s is in use, kill its process and start new compilation?" ,buf))
(kill-buffer ,buf)
(user-error "Aborted")))
(compile command)
(,comp-mode))))

;;;; Compilation-modes and functions
(verilog-ext-compile-define-mode verilog-ext-compile-verilator-mode
:desc "Verilator"
:docstring "Verilator Compilation mode."
Expand Down Expand Up @@ -282,8 +230,24 @@ ARGS is a property list."
:comp-mode verilog-ext-compile-surelog-mode)


(defun verilog-ext-compile ()
"Compile using command of :compile-cmd of `verilog-ext-project-alist' project.
;;;; Compilation interactive functions
(defun verilog-ext-compile-makefile ()
"Prompt to available Makefile targets and compile."
(interactive)
(let ((makefile (file-name-concat (verilog-ext-buffer-proj-root) "Makefile"))
(makefile-need-target-pickup t) ; Force refresh of makefile targets
target cmd)
(unless (file-exists-p makefile)
(error "%s does not exist!" makefile))
(with-temp-buffer
(insert-file-contents makefile)
(makefile-pickup-targets)
(setq target (completing-read "Target: " makefile-target-table)))
(setq cmd (mapconcat #'identity `("cd" ,(verilog-ext-buffer-proj-root) "&&" "make" ,target) " "))
(compile cmd)))

(defun verilog-ext-compile-project ()
"Compile using :compile-cmd of `verilog-ext-project-alist' project.
Depending on the command, different syntax highlight will be applied.
Expand Down Expand Up @@ -318,10 +282,37 @@ set the appropriate mode."
;; For the rest use the provided command
(t
compile-cmd)))
(cmd (concat "cd " (verilog-ext-buffer-proj-root proj) " && " cmd-processed)))
(cmd (mapconcat `("cd" ,(verilog-ext-buffer-proj-root proj) "&&" ,cmd-processed) " ")))
(funcall fn cmd)))


;;;; Preprocess
(defun verilog-ext-preprocess ()
"Preprocess current file.
Choose among available programs and update `verilog-preprocessor' variable.
Supports verilator, vppreproc and iverilog."
(interactive)
(let ((tools-available (seq-filter (lambda (bin)
(executable-find bin))
'("verilator" "iverilog" "vppreproc"))))
(pcase (completing-read "Select tool: " tools-available)
;; Verilator
("verilator" (setq verilog-preprocessor "verilator -E __FLAGS__ __FILE__"))
;; Verilog-Perl
("vppreproc" (setq verilog-preprocessor "vppreproc __FLAGS__ __FILE__"))
;; Icarus Verilog: `iverilog' command syntax requires writing to an output file (defaults to a.out).
("iverilog" (let* ((filename-sans-ext (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))
(iver-out-file (concat (temporary-file-directory) filename-sans-ext "_pp_iver.sv")))
(setq verilog-preprocessor (concat "iverilog -E -o" iver-out-file " __FILE__ && "
"echo \"\" && " ; Add blank line between run command and first preprocessed line
"cat " iver-out-file)))))
(verilog-preprocess)
(pop-to-buffer "*Verilog-Preprocessed*")))



(provide 'verilog-ext-compile)

;;; verilog-ext-compile.el ends here
2 changes: 1 addition & 1 deletion verilog-ext-flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ https://chipsalliance.github.io/verible/lint.html"
(defcustom verilog-ext-flycheck-svlint-include-path nil
"List of include paths for svlint linter.
Variables is needed since svlint doesn't allow both source and -f command file
Variable is needed since svlint doesn't allow both source and -f command file
at the same time."
:type '(repeat directory)
:group 'verilog-ext-flycheck)
Expand Down
1 change: 1 addition & 0 deletions verilog-ext-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ Similar to `verilog-match-translate-off' but including
;;; Setup
(defun verilog-ext-font-lock-setup ()
"Setup syntax highlighting of SystemVerilog buffers.
Add `verilog-ext-mode' font lock keywords before running
`verilog-mode' in order to populate `font-lock-keywords-alist'
before `font-lock' is loaded."
Expand Down
22 changes: 14 additions & 8 deletions verilog-ext-hierarchy.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ Used to navigate definitions with `verilog-ext-hierarchy-twidget-nav-open'.")
(defvar verilog-ext-hierarchy-internal-alist nil
"Per project flat hierarchy alist.
Used by builtin and tree-sitter backends.")

(defvar verilog-ext-hierarchy-current-flat-hier nil
"Current flat hierarchy.
Used by `verilog-ext-hierarchy-extract--internal',
`verilog-ext-hierarchy-ghdl-extract' and their subroutines.
`verilog-ext-hierarchy-vhier-extract' and their subroutines.
Needed since `verilog-ext-hierarchy-extract--childrenfn' can only
have one argument (item).")

Expand Down Expand Up @@ -126,7 +127,7 @@ Arg ITEM are hierarchy nodes."
(defun verilog-ext-hierarchy-extract--internal (module)
"Construct hierarchy struct for MODULE.
Entities and instances will be analyzed from corresponding entry in
Modules and instances will be analyzed from corresponding entry in
`verilog-ext-hierarchy-current-flat-hier'. These entries will have an
associated project present `verilog-ext-project-alist' and will be of the form:
\(module instance1:NAME1 instance2:NAME2 ...\).
Expand Down Expand Up @@ -295,7 +296,10 @@ Used for hierarchy.el frontend to visit file of module at point."

(defun verilog-ext-hierarchy-vhier-extract (module)
"Extract hierarchy of MODULE using Verilog-Perl vhier as a backend.
Return hierarchy as an indented string."
With current prefix, force refreshing of hierarchy database for active project.
Return populated `hierarchy' struct."
(unless (executable-find "vhier")
(error "Executable vhier not found"))
(let* ((proj (verilog-ext-buffer-proj))
Expand Down Expand Up @@ -338,6 +342,7 @@ Return hierarchy as an indented string."
(setq hierarchy-string (buffer-substring-no-properties (point-min) (point-max))))
;; Convert indented string to alist for caching and default hierarchy.el displaying
(setq hierarchy-alist (verilog-ext-hierarchy--convert-string-to-alist hierarchy-string))
;; Update variables and cache
(verilog-ext-proj-setcdr proj verilog-ext-hierarchy-vhier-alist hierarchy-alist)
(verilog-ext-hierarchy-build-module-alist proj-files proj)
(setq verilog-ext-hierarchy-current-flat-hier hierarchy-alist)
Expand Down Expand Up @@ -545,7 +550,7 @@ If OTHER-WINDOW is non-nil, open definition in other window."
(verilog-ext-hierarchy-outshine-jump-to-file :other-window))

(define-minor-mode verilog-ext-hierarchy-outshine-nav-mode
"Instance navigation frontend for Verilog-Perl `vhier'.
"Instance navigation frontend with `outshine'.
Makes use of processed output under `outline-minor-mode' and `outshine'."
:lighter " vH"
:keymap
Expand Down Expand Up @@ -605,7 +610,8 @@ Expects HIERARCHY to be a indented string."
(goto-char (point-min))
(open-line 1)
(insert "// Hierarchy generated by `verilog-ext'\n")
(verilog-mode)
(verilog-ext-with-no-hooks
(verilog-mode))
(verilog-ext-hierarchy-outshine-nav-mode))
(pop-to-buffer buf)))

Expand Down Expand Up @@ -665,13 +671,13 @@ With prefix arg, clear cache for ALL projects."
"Construct hierarchy for MODULE depending on selected backend."
(cond (;; Verilog-Perl vhier
(eq verilog-ext-hierarchy-backend 'vhier)
(verilog-ext-hierarchy-vhier-extract module)) ; Returns indented string
(verilog-ext-hierarchy-vhier-extract module))
(;; Tree-sitter
(eq verilog-ext-hierarchy-backend 'tree-sitter)
(verilog-ext-hierarchy-tree-sitter-extract module)) ; Returns populated hierarchy struct
(verilog-ext-hierarchy-tree-sitter-extract module))
(;; Built-in
(eq verilog-ext-hierarchy-backend 'builtin)
(verilog-ext-hierarchy-builtin-extract module)) ; Returns populated hierarchy struct
(verilog-ext-hierarchy-builtin-extract module))
;; Fallback
(t (error "Must set a proper extraction backend in `verilog-ext-hierarchy-backend'"))))

Expand Down
Loading

0 comments on commit 885917c

Please sign in to comment.