Home > Enterprise >  Tools to convert lisp code to assembly language
Tools to convert lisp code to assembly language

Time:10-24

I have a lisp function. Is it possible to convert it to assembly language?

(defun isprime (num &optional (d (- num 1))) 
  (if (/= num 1) (or (= d 1)
      (and (/= (rem num d) 0)
           (is-prime  num (- d 1)))) ()))

The above code I want to convert it to assembly language. I am not sure if linux or windows have any tools to do it?

CodePudding user response:

Why do you want to do that?

If you want to study how Lisp compilation works, just use disassemble:

(disassemble 'isprime)

If you want to compile the code for future use, then use compile-file.

CodePudding user response:

"Is it possible to convert it to assembly language?"

Not as it currently stands. It has both isprime and is-prime. Correcting the second one to isprime, I can compile and run the isprime function. I also added a 'main' function, which does this:

(defun main ()
   (format t "Prime test on 23 is ~A" (isprime 23)))

I saved the whole thing in TestCompile.lisp

I am using Portacle / Windows to run SBCL / SLIME. I can compile a function by left clicking to put the cursor in the function, and then using C-c C-c. I can compile the whole file using C-c C-k, creating a FASL file.

From the REPL, I can now run (main)

CL-USER> (main)
Prime test on 23 is T
NIL

Finally, I decide that I want to create an executable. From PowerShell in the folder that contains the LISP source code:

sbcl
(load "TestCompile.lisp")
(sb-ext:save-lisp-and-die "prime.exe" :toplevel #'main :executable t)

./prime.exe

The executable is not compressed on Windows, and includes the compiler. It can be compressed manually using zip or similar.

  • Related