-4

I'm writing a compiler, and I want it to compile to a native executable (just Linux, for now). I don't want it to be Assembly, it needs to be PURE machine code. Can anyone point me in the right direction?

EDIT: I want to produce x86 Linux machine code.

12
  • " I don't want it to be Assembly" It's completely unclear what you're asking about. Linux is an operating system, machine code goes to your bare metal CPU. Could it be that you are confusing things? Commented Apr 10, 2020 at 13:51
  • 1
    You'll face a much harder job learning the bit-encoding of machine instructions than having an assembler do that part for you. Commented Apr 10, 2020 at 14:04
  • 1
    You can either use an appropriate assembler for that OS you're using, or simply put some byte sequences to execute. Your question doesn't have anything to do with Linux. Commented Apr 10, 2020 at 14:05
  • 2
    Nothing prevents your compiler from emitting machine code. Just write it so that it emits machine code. There are compilers that produce assembly (see godbolt.org for examples), but that is certainly not a requirement for writing a compiler. Commented Apr 10, 2020 at 14:36
  • 2
    If you are wanting to generate pure machine code, then that code will depend the CPU architecture (or at least class of CPU) that you intend to run the code on. EG x86 vs ARM. But you haven't even specified that, so it is hard to know where to send you. Commented Apr 10, 2020 at 14:47

3 Answers 3

0

The assembler is not used at runtime. It is used at compile time to generate native code, which is what is typically distributed in software packages. Once you have that executable, the assembler is no longer necessary. Also, the assembling stage is typically called automatically by the compiler, as one of its stages, so the programmer isn't explicitly aware of it.

Therefore, if you want to generate native code, and don't want to tie into one of the excellent existing toolchains like llvm, the first thing you would want to do is write an assembler, whether that's a separate executable or simply a module in your compiler. It's too difficult to go straight from high-level code to machine code. In fact, compilers typically have several stages.

0

You want your compiler to generate machine code directly.  But program files are not just machine code — there is an executable file format, within which there is relocatable machine code & initialized data.  For linux, there is a file format that covers both program files and object files: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format

Typically, the compiler (and/or assembler), linker, and operating system loader form a pipeline that cooperates to reconstitute a program into memory for execution by the processor in the context of an operating system process.  By the time the program runs, the location of all symbols is known and any references to symbols are converted from nominal references to numeric address references.

Each stage may be missing numeric address information, captured instead symbolically.  Use of external symbols and otherwise unknown addresses requires relocation at various stages in that pipeline.

Beyond raw machine code & data, these file formats are capable of containing metadata about the program such as symbol imports & exports, relocations required, debugger-required information, etc..

See also https://en.wikipedia.org/wiki/Comparison_of_executable_file_formats

Otherwise you might be interested in JIT technology, where machine code is generated directly into memory for immediate execution, and thus there is no output file.

0

Nothing stops you from writing machine code. Every good assembly manual should show the instruction encoding. Your compiler just has to write those instructions instead of writing assembly code.

Don't forget that your executable file is not just machine code, it also has ELF headers saying which things go where in memory.

Most compilers find it more convenient to write assembly code, but yours doesn't have to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.