1

I wanted to know how to generate assembly code from the abstract syntax tree?

Thank you!

Pink
  • 223
  • 1
  • 2
  • 5

1 Answers1

0

I am not a master in compiler construction. But if you are a beginner, I guess this answer will help.

First of all, assembly is a platform dependant. You have to decide for which platform you are going to generate assembly code. You can refer to the instruction set architecture for this.

After finalizing that, there are two ways you can generate assembly code.

  1. Generate assembly manually
  2. Using a library

If you are going to generate assembly manually, you should have a good knowledge of instruction set architecture. You have to traverse through the AST and convert nodes into assembly code. You can use a design pattern like Visitor or Listener aka Observer pattern to traverse through the AST.

Also, you can use a library like Asmjit for machine code generation as well.

Godbolt will help you to get some knowledge about assembly code.

Here are few other Stackoverflow answers that may help you.

Buddhika Chathuranga
  • 1,334
  • 2
  • 13
  • 22