简体   繁体   中英

How to include an assembler in a compiler

I am currently enrolled in a compilers course and have successfully built a small compiler in JAVA that accepts a file as an input and writes NASM output.

I was wondering however if i ever decided to distribute my compiler how would i package an assembler with it?

I am sure i cannot ask the end user to first compile the code to NASM, ensure that he/she has NASM installed and then use that to build the final executable!

Also since i want to target several different systems the idea of writing an efficient assembler for each intended target is obviously an arduous and time consuming (not to mention seemingly impossible) task!!

Any help is appreciated.

You can select an open-source assembler and package that with your compiler. Take care of the licence however, the licence of the assembler might require you to publish your compiler under a certain license also.

Of course this does not solve your second problem: how to make this platform (or at least CPU) independent. Then going through assembly language (or directly to machine code) is not the way to go.

A solution might be to use someone else's backend. LLVM is somewhat "hot" at the moment. In a way this works by sending "platform independent assembly language" to LLVM, and LLVM then outputs the object code for the target platform for you.

From www.nasm.us/pub/nasm/releasebuilds/2.11.08 it seems that at least for Windows and Mac OS X platforms the NASM assembler can be deployed as simple portable application in a zip file.

You'd just distribute the zip files with your application and when needed extract the appropriate platform-specific zip into a temporary directory and run the tool in the background.


Some further topics that need to be clarified before taking an action:

  • how to create temporary directory with Java ?
  • how to unzip an archive with Java ?
  • how to mark an executable as executable (eg how to set chmod+x ) without threatening the user?
  • how to spawn an wait-for background process in Java ?
  • what is the NASM 's redistribution licensing policy?
  • does NASM have uncommon 3rd party dependencies which must be shipped as well?
  • how to write cross-platform Java code and how to do multi-platform testing?
  • ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM