From aec5f6840003b7ff1b019339e5b3c2a60a67469e Mon Sep 17 00:00:00 2001 From: Benson Chu Date: Sat, 17 Dec 2022 16:51:41 -0600 Subject: [PATCH] Refactoring complete? --- .../{ => llvm-act-on-file}/act-on-c-file.el | 17 +++--- .../{ => llvm-act-on-file}/act-on-ll-file.el | 3 +- .../act-on-test-file.el | 0 .../llvm-act-on-file/llvm-act-on-file.el | 45 ++++++++++++++ lisp/llvm-lib/llvm-build-tool.el | 48 +++++++++++++++ lisp/llvm-lib/llvm-gdb-command.el | 60 +++++++++++++++++++ lisp/llvm-lib/llvm-lib.el | 38 ++++++++++++ lisp/llvm-lib/llvm-shared.el | 25 +++++++- lisp/ti-config/libraries | 2 +- lisp/ti-config/work-config.el | 1 - 10 files changed, 225 insertions(+), 14 deletions(-) rename lisp/llvm-lib/{ => llvm-act-on-file}/act-on-c-file.el (84%) rename lisp/llvm-lib/{ => llvm-act-on-file}/act-on-ll-file.el (97%) rename lisp/llvm-lib/{ => llvm-act-on-file}/act-on-test-file.el (100%) create mode 100644 lisp/llvm-lib/llvm-act-on-file/llvm-act-on-file.el create mode 100644 lisp/llvm-lib/llvm-build-tool.el create mode 100644 lisp/llvm-lib/llvm-gdb-command.el create mode 100644 lisp/llvm-lib/llvm-lib.el diff --git a/lisp/llvm-lib/act-on-c-file.el b/lisp/llvm-lib/llvm-act-on-file/act-on-c-file.el similarity index 84% rename from lisp/llvm-lib/act-on-c-file.el rename to lisp/llvm-lib/llvm-act-on-file/act-on-c-file.el index ca102df..a5a3301 100644 --- a/lisp/llvm-lib/act-on-c-file.el +++ b/lisp/llvm-lib/llvm-act-on-file/act-on-c-file.el @@ -24,6 +24,7 @@ ;;; Code: (require 'llvm-ir-mode) +(require 'llvm-shared) (require 'action-map-lib) (defvar ll/c-file-action-map @@ -39,13 +40,15 @@ (compiler (completing-read "Which clang? " (lls/get-tool "clang$")))) - (concat (funcall lls/get-clang-command-fun compiler file compiler-action) - (pcase action - ('debug (format "-mllvm -debug-only=%s" (read-string "Which pass? "))) - ('before-after (let ((pass (read-string "Which pass? "))) - (format "-mllvm -print-before=%s -mllvm -print-after=%s" pass pass))) - ('changed "-mllvm -print-before-all")) - " "))) + (string-join + (list (funcall lls/get-clang-command-fun compiler file compiler-action) + (pcase action + ('debug (format "-mllvm -debug-only=%s" (read-string "Which pass? "))) + ('before-after (let ((pass (read-string "Which pass? "))) + (format "-mllvm -print-before=%s -mllvm -print-after=%s" pass pass))) + ('changed "-mllvm -print-before-all")) + " ") + " "))) (defun ll/act-on-c-file (file) (let* ((action (aml/read-action-map ll/c-file-action-map))) diff --git a/lisp/llvm-lib/act-on-ll-file.el b/lisp/llvm-lib/llvm-act-on-file/act-on-ll-file.el similarity index 97% rename from lisp/llvm-lib/act-on-ll-file.el rename to lisp/llvm-lib/llvm-act-on-file/act-on-ll-file.el index 3b1fb6b..406615d 100644 --- a/lisp/llvm-lib/act-on-ll-file.el +++ b/lisp/llvm-lib/llvm-act-on-file/act-on-ll-file.el @@ -30,8 +30,7 @@ (stop-before :key ?S :major-mode llvm-mode :buffer-string "before-%s" :description "[S]top-before"))) (defun ll/build-llc-command (file _action) - (concat "llc -o - " - file " ")) + (funcall lls/get-clang-command-fun file action)) (defun ll/act-on-ll-file (file) (let* ((action (aml/read-action-map ll/ll-file-action-map)) diff --git a/lisp/llvm-lib/act-on-test-file.el b/lisp/llvm-lib/llvm-act-on-file/act-on-test-file.el similarity index 100% rename from lisp/llvm-lib/act-on-test-file.el rename to lisp/llvm-lib/llvm-act-on-file/act-on-test-file.el diff --git a/lisp/llvm-lib/llvm-act-on-file/llvm-act-on-file.el b/lisp/llvm-lib/llvm-act-on-file/llvm-act-on-file.el new file mode 100644 index 0000000..99a5651 --- /dev/null +++ b/lisp/llvm-lib/llvm-act-on-file/llvm-act-on-file.el @@ -0,0 +1,45 @@ +;;; llvm-act-on-file.el --- -*- lexical-binding: t -*- + +;; Copyright (C) 2022 Benson Chu + +;; Author: Benson Chu +;; Created: [2022-12-16 18:46] + +;; This file is not part of GNU Emacs + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;;; Code: +(require 'llvm-shared) +(require 'act-on-test-file) +(require 'act-on-c-file) +(require 'act-on-ll-file) + +(defun ll/act-on-file (file) + (interactive (list (or (buffer-file-name (current-buffer)) + (read-file-name "File? ")))) + (when (null file) + (setq file (make-temp-file nil nil ".ll")) + (write-file file)) + (pcase (file-name-extension file) + ((and _ (guard (ll/is-test-file file))) + (ll/act-on-test-file file)) + ("c" (ll/act-on-c-file file)) + ("ll" (ll/act-on-ll-file file)) + (_ (message "Not sure what you'd like me to do with this file")))) + +(provide 'llvm-act-on-file) +;;; llvm-act-on-file.el ends here diff --git a/lisp/llvm-lib/llvm-build-tool.el b/lisp/llvm-lib/llvm-build-tool.el new file mode 100644 index 0000000..2d7c480 --- /dev/null +++ b/lisp/llvm-lib/llvm-build-tool.el @@ -0,0 +1,48 @@ +;;; llvm-build-tool.el --- -*- lexical-binding: t -*- + +;; Copyright (C) 2022 Benson Chu + +;; Author: Benson Chu +;; Created: [2022-12-17 12:48] + +;; This file is not part of GNU Emacs + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;;; Code: +(require 'llvm-shared) + +(defvar lls/name-llvm-build-buffer + (lambda (directory tools) + (format "*%s-%s*" + (file-name-nondirectory directory) + (string-join tools ",")))) + +(defun ll/llvm-build-tool (directory tools) + (interactive + (list + (my/completing-read "build directory" (lls/get-llvm-build-dirs)) + (progn + (split-string (read-string "ninja -j X ") " ")))) + (let* ((default-directory directory) + (buffer-name (funcall lls/name-llvm-build-buffer directory tools))) + (compilation-start + (lls/ninja-build-tools directory tools) + nil + (lambda (_) buffer-name)))) + +(provide 'llvm-build-tool) +;;; llvm-build-tool.el ends here diff --git a/lisp/llvm-lib/llvm-gdb-command.el b/lisp/llvm-lib/llvm-gdb-command.el new file mode 100644 index 0000000..bad821d --- /dev/null +++ b/lisp/llvm-lib/llvm-gdb-command.el @@ -0,0 +1,60 @@ +;;; llvm-gdb-command.el --- -*- lexical-binding: t -*- + +;; Copyright (C) 2022 Benson Chu + +;; Author: Benson Chu +;; Created: [2022-12-17 16:21] + +;; This file is not part of GNU Emacs + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;;; Code: + +(require 'llvm-shared) + +(defun ll/get-cc1-command (file) + (let* ((fname (file-name-nondirectory file)) + (buffer (format "*cc1-%s*" fname)) + (clang (lls/prompt-tool "clang$"))) + (save-window-excursion + (let ((command (string-join + (list (funcall + lls/get-clang-command-fun + clang file 'compile + (make-temp-file nil nil ".o")) + "-v") + " "))) + (shell-command command buffer)) + (with-current-buffer (get-buffer buffer) + (beginning-of-buffer) + (re-search-forward (rx "\"" (literal clang) "\" " + (group "-cc1" (+ nonl)))) + (match-string 1))))) + +(defun ll/kill-gdb-command (file) + (interactive + (list + (--> (current-buffer) + (buffer-file-name) + (read-file-name "Which file? " nil)))) + (let ((fname (file-name-nondirectory file))) + (kill-new + (format "r %s" + (ll/get-cc1-command file))))) + +(provide 'llvm-gdb-command) +;;; llvm-gdb-command.el ends here diff --git a/lisp/llvm-lib/llvm-lib.el b/lisp/llvm-lib/llvm-lib.el new file mode 100644 index 0000000..a76a7a4 --- /dev/null +++ b/lisp/llvm-lib/llvm-lib.el @@ -0,0 +1,38 @@ +;;; llvm-lib.el --- -*- lexical-binding: t -*- + +;; Copyright (C) 2022 Benson Chu + +;; Author: Benson Chu +;; Created: [2022-12-17 12:54] + +;; This file is not part of GNU Emacs + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;;; Code: +(require 'llvm-act-on-file) +(require 'llvm-build-tool) +(require 'llvm-gdb-command) + +(define-prefix-command '*llvm-map*) +;; (define-key *root-map* (kbd "C-w") '*llvm-map*) + +(define-key *llvm-map* (kbd "a") #'ll/act-on-file) +(define-key *llvm-map* (kbd "c") #'ll/llvm-build-tool) +(define-key *llvm-map* (kbd "M-w") #'ll/kill-gdb-command) + +(provide 'llvm-lib) +;;; llvm-lib.el ends here diff --git a/lisp/llvm-lib/llvm-shared.el b/lisp/llvm-lib/llvm-shared.el index 79a0ba8..56b4e99 100644 --- a/lisp/llvm-lib/llvm-shared.el +++ b/lisp/llvm-lib/llvm-shared.el @@ -87,14 +87,27 @@ ;; =============================== Misc ============================== +(defun my/completing-read (prompt collection) + (let ((len (length collection))) + (cond ((< len 1) + (user-error "Uhhh, no %ss? " prompt)) + ((= len 1) (car collection)) + (t (completing-read (format "Which %s? " prompt) + collection))))) + +(defun lls/prompt-tool (tool-regexp &optional directories) + (my/completing-read tool-regexp + (lls/get-tool tool-regexp directories))) + (defun lls/get-tool (tool-regexp &optional directories) (cl-mapcan #'(lambda (dir) - (directory-files dir t tool-regexp)) + (when (file-exists-p dir) + (directory-files dir t tool-regexp))) (or directories (lls/get-llvm-bin-dirs)))) (defvar lls/get-clang-command-fun - (lambda (compiler file action &optional rest) + (lambda (compiler file action &optional output rest) (string-join (list compiler (string-join rest " ") file @@ -103,9 +116,15 @@ ('assemble "-S") ('preprocess "-E") ('llvm-ir "-S -emit-llvm")) - "-o -") + (format "-o %s" + (or output "-"))) " "))) +(defvar lls/get-llc-command-fun + (lambda (file _action) + (concat "llc -o - " + file " "))) + ;; ========================= LLVM Build Dirs ========================= (setq lls/target-init-fun diff --git a/lisp/ti-config/libraries b/lisp/ti-config/libraries index 44439fd..1b6d1cc 160000 --- a/lisp/ti-config/libraries +++ b/lisp/ti-config/libraries @@ -1 +1 @@ -Subproject commit 44439fdce54a0a8b6dcf0b43a178c3be3cb2b25d +Subproject commit 1b6d1cc62fd5fefa1bc5f784349a7e7e73be797f diff --git a/lisp/ti-config/work-config.el b/lisp/ti-config/work-config.el index 56134dc..d60fc78 100644 --- a/lisp/ti-config/work-config.el +++ b/lisp/ti-config/work-config.el @@ -29,7 +29,6 @@ (require 'work-asm-config) (require 'ti-keymap) (require 'ti-lib) -(require 'ti-build-tool) (require 'ti-tools-backup) (require 'argo-fastsim-dump-mode) (require 'frame-restore)