branch: externals/beardbolt commit 627dd4c1c9650818b587ddb891eefb3b8699819e Author: Jay Kamat <jaygka...@gmail.com> Commit: Jay Kamat <jaygka...@gmail.com>
Allow for custom asm processing functions Useful for bytecode --- rmsbolt.el | 185 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 107 insertions(+), 78 deletions(-) diff --git a/rmsbolt.el b/rmsbolt.el index 702d601af0..f1fb2b288c 100644 --- a/rmsbolt.el +++ b/rmsbolt.el @@ -229,7 +229,7 @@ Outputs assembly file if ASM." :type 'symbol :documentation "The object dumper to use if disassembling binary.") (demangler - "c++filt" + nil :type 'string :documentation "The command of the demangler to use for this source code.") (starter-file-name @@ -247,7 +247,13 @@ Outputs assembly file if ASM." (compile-cmd-function nil :type 'function - :documentation "A function which takes in a compile command (could be the default) and adds needed args to it.")) + :documentation "A function which takes in a compile command +(could be the default) and adds needed args to it.") + (process-asm-custom-fn + nil + :type 'function + :documentation "A custom function used for parsing asm lines + instead of the default assembly one." )) (cl-defun rmsbolt--c-compile-cmd (&key src-buffer) @@ -337,6 +343,13 @@ Outputs assembly file if ASM." "-Cllvm-args=--x86-asm-syntax=intel")) " "))) cmd)) +(cl-defun rmsbolt--py-compile-cmd (&key src-buffer) + "Process a compile command for rustc." + (let* ((cmd (buffer-local-value 'rmsbolt-command src-buffer))) + (mapconcat 'identity + (list cmd "-m" "dis" (buffer-file-name) + ">" (rmsbolt-output-filename src-buffer)) + " "))) (defvar rmsbolt--hidden-func-c (rx bol (or (and "__" (0+ any)) @@ -371,6 +384,7 @@ Outputs assembly file if ASM." :compile-cmd "gcc" :supports-asm t :supports-disass t + :demangler "c++filt" :starter-file-name "rmsbolt.c" :compile-cmd-function #'rmsbolt--c-compile-cmd :disass-hidden-funcs rmsbolt--hidden-func-c)) @@ -379,6 +393,7 @@ Outputs assembly file if ASM." :compile-cmd "g++" :supports-asm t :supports-disass t + :demangler "c++filt" :starter-file-name "rmsbolt.cpp" :compile-cmd-function #'rmsbolt--c-compile-cmd :disass-hidden-funcs rmsbolt--hidden-func-c)) @@ -388,7 +403,6 @@ Outputs assembly file if ASM." :compile-cmd "ocamlopt" :supports-asm t :supports-disass t - :demangler nil :starter-file-name "rmsbolt.ml" :compile-cmd-function #'rmsbolt--ocaml-compile-cmd :disass-hidden-funcs rmsbolt--hidden-func-ocaml)) @@ -398,7 +412,6 @@ Outputs assembly file if ASM." :supports-asm t :supports-disass nil :objdumper 'cat - :demangler nil :starter-file-name "rmsbolt.lisp" :compile-cmd-function #'rmsbolt--lisp-compile-cmd :disass-hidden-funcs nil)) @@ -412,6 +425,15 @@ Outputs assembly file if ASM." :starter-file-name "rmsbolt.rs" :compile-cmd-function #'rmsbolt--rust-compile-cmd :disass-hidden-funcs nil)) + ;; ONLY SUPPORTS PYTHON 3 + (python-mode + . ,(make-rmsbolt-lang :mode 'python-mode + :compile-cmd "python3" + :supports-asm t + :supports-disass nil + :starter-file-name "rmsbolt.py" + :compile-cmd-function #'rmsbolt--py-compile-cmd + :disass-hidden-funcs nil)) )) ;;;; Macros @@ -561,80 +583,87 @@ Outputs assembly file if ASM." (cl-defun rmsbolt--process-asm-lines (src-buffer asm-lines) "Process and filter a set of asm lines." - (if (buffer-local-value 'rmsbolt-disassemble src-buffer) - (rmsbolt--process-disassembled-lines src-buffer asm-lines) - (let ((used-labels (rmsbolt--find-used-labels src-buffer asm-lines)) - (result nil) - (prev-label nil) - (source-linum nil) - (source-file nil) - (skip-file-match - ;; Skip file match if we don't have a current filename - (not (buffer-file-name src-buffer)))) - (dolist (line asm-lines) - (let* ((raw-match (or (string-match rmsbolt-label-def line) - (string-match rmsbolt-assignment-def line))) - (match (when raw-match - (match-string 1 line))) - (used-label (cl-find match used-labels :test #'equal))) - (cl-tagbody - ;; Process file name hints - (when (string-match rmsbolt-source-file line) - (if (match-string 3 line) - ;; Clang style match - (setq source-file (expand-file-name - (match-string 3 line) - (match-string 2 line))) - (setq source-file (match-string 2 line)))) - ;; Process any line number hints - (when (string-match rmsbolt-source-tag line) - (if (or skip-file-match - (file-equal-p (buffer-file-name src-buffer) source-file)) - (setq source-linum (string-to-number - (match-string 2 line))) - (setq source-linum nil))) - (when (string-match rmsbolt-source-stab line) - (pcase (string-to-number (match-string 1 line)) - ;; http://www.math.utah.edu/docs/info/stabs_11.html - (68 - (setq source-linum (match-string 2 line))) - ((or 100 132) - (setq source-linum nil)))) - - ;; End block, reset prev-label and source - (when (string-match-p rmsbolt-endblock line) - (setq prev-label nil)) - - (when (and (buffer-local-value 'rmsbolt-filter-comment-only src-buffer) - (string-match-p rmsbolt-comment-only line)) - (go continue)) - - ;; continue means we don't add to the ouptut - (when match - (if (not used-label) - ;; Unused label - (when (buffer-local-value 'rmsbolt-filter-labels src-buffer) - (go continue)) - ;; Real label, set prev-label - (setq prev-label raw-match))) - (when (and (buffer-local-value 'rmsbolt-filter-directives src-buffer) - (not match)) - (if (and (string-match-p rmsbolt-data-defn line) - prev-label) - ;; data is being used - nil - (when (string-match-p rmsbolt-directive line) - (go continue)))) - ;; Add line numbers to mapping - (when (and source-linum - (rmsbolt--has-opcode-p line)) - (add-text-properties 0 (length line) - `(rmsbolt-src-line ,source-linum) line)) - ;; Add line - (push line result) - - continue))) - (nreverse result)))) + (let* ((lang (with-current-buffer src-buffer + (rmsbolt--get-lang))) + (process-asm-fn (rmsbolt-l-process-asm-custom-fn lang))) + (cond + (process-asm-fn + (funcall process-asm-fn src-buffer asm-lines)) + ((buffer-local-value 'rmsbolt-disassemble src-buffer) + (rmsbolt--process-disassembled-lines src-buffer asm-lines)) + (t + (let ((used-labels (rmsbolt--find-used-labels src-buffer asm-lines)) + (result nil) + (prev-label nil) + (source-linum nil) + (source-file nil) + (skip-file-match + ;; Skip file match if we don't have a current filename + (not (buffer-file-name src-buffer)))) + (dolist (line asm-lines) + (let* ((raw-match (or (string-match rmsbolt-label-def line) + (string-match rmsbolt-assignment-def line))) + (match (when raw-match + (match-string 1 line))) + (used-label (cl-find match used-labels :test #'equal))) + (cl-tagbody + ;; Process file name hints + (when (string-match rmsbolt-source-file line) + (if (match-string 3 line) + ;; Clang style match + (setq source-file (expand-file-name + (match-string 3 line) + (match-string 2 line))) + (setq source-file (match-string 2 line)))) + ;; Process any line number hints + (when (string-match rmsbolt-source-tag line) + (if (or skip-file-match + (file-equal-p (buffer-file-name src-buffer) source-file)) + (setq source-linum (string-to-number + (match-string 2 line))) + (setq source-linum nil))) + (when (string-match rmsbolt-source-stab line) + (pcase (string-to-number (match-string 1 line)) + ;; http://www.math.utah.edu/docs/info/stabs_11.html + (68 + (setq source-linum (match-string 2 line))) + ((or 100 132) + (setq source-linum nil)))) + + ;; End block, reset prev-label and source + (when (string-match-p rmsbolt-endblock line) + (setq prev-label nil)) + + (when (and (buffer-local-value 'rmsbolt-filter-comment-only src-buffer) + (string-match-p rmsbolt-comment-only line)) + (go continue)) + + ;; continue means we don't add to the ouptut + (when match + (if (not used-label) + ;; Unused label + (when (buffer-local-value 'rmsbolt-filter-labels src-buffer) + (go continue)) + ;; Real label, set prev-label + (setq prev-label raw-match))) + (when (and (buffer-local-value 'rmsbolt-filter-directives src-buffer) + (not match)) + (if (and (string-match-p rmsbolt-data-defn line) + prev-label) + ;; data is being used + nil + (when (string-match-p rmsbolt-directive line) + (go continue)))) + ;; Add line numbers to mapping + (when (and source-linum + (rmsbolt--has-opcode-p line)) + (add-text-properties 0 (length line) + `(rmsbolt-src-line ,source-linum) line)) + ;; Add line + (push line result) + + continue))) + (nreverse result)))))) ;;;;; Handlers (defun rmsbolt--handle-finish-compile (buffer _str)