Refactoring complete?

This commit is contained in:
Benson Chu 2022-12-17 16:51:41 -06:00
parent 9254d03956
commit aec5f68400
10 changed files with 225 additions and 14 deletions

View file

@ -24,6 +24,7 @@
;;; Code:
(require 'llvm-ir-mode)
(require 'llvm-shared)
(require 'action-map-lib)
(defvar ll/c-file-action-map
@ -39,12 +40,14 @@
(compiler (completing-read
"Which clang? "
(lls/get-tool "clang$"))))
(concat (funcall lls/get-clang-command-fun compiler file compiler-action)
(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)

View file

@ -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))

View file

@ -0,0 +1,45 @@
;;; llvm-act-on-file.el --- -*- lexical-binding: t -*-
;; Copyright (C) 2022 Benson Chu
;; Author: Benson Chu <bensonchu457@gmail.com>
;; 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 <https://www.gnu.org/licenses/>.
;;; 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

View file

@ -0,0 +1,48 @@
;;; llvm-build-tool.el --- -*- lexical-binding: t -*-
;; Copyright (C) 2022 Benson Chu
;; Author: Benson Chu <bensonchu457@gmail.com>
;; 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 <https://www.gnu.org/licenses/>.
;;; 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

View file

@ -0,0 +1,60 @@
;;; llvm-gdb-command.el --- -*- lexical-binding: t -*-
;; Copyright (C) 2022 Benson Chu
;; Author: Benson Chu <bensonchu457@gmail.com>
;; 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 <https://www.gnu.org/licenses/>.
;;; 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

38
lisp/llvm-lib/llvm-lib.el Normal file
View file

@ -0,0 +1,38 @@
;;; llvm-lib.el --- -*- lexical-binding: t -*-
;; Copyright (C) 2022 Benson Chu
;; Author: Benson Chu <bensonchu457@gmail.com>
;; 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 <https://www.gnu.org/licenses/>.
;;; 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

View file

@ -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

@ -1 +1 @@
Subproject commit 44439fdce54a0a8b6dcf0b43a178c3be3cb2b25d
Subproject commit 1b6d1cc62fd5fefa1bc5f784349a7e7e73be797f

View file

@ -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)