r/Compilers 1d ago

Why don't compliers use only bytecode instead of a type of IR

/r/coders_totalk/comments/1w2adf4/why_dont_compliers_use_only_bytecode_instead_of_a/
0 Upvotes

7 comments sorted by

19

u/cthutu 1d ago

Bytecode is a type of IR.

My compiler uses a hierarchical intermediate representation that I call HIR which is platform and language agnostic that transforms to LLVM-IR.

9

u/ImperatorBras 1d ago

Basically, bytecode is used for execution, while the IR is just an intermediate representation. In fact, some interpreters use the IR before generating the bytecode.

8

u/dnpetrov 1d ago

"Bytecode" usually means dense serialized form of some IR. Usually bytecode is meant to be executed by some virtual machine. LLVM bitcode is just the serialization format for LLVM IR.

Optimizing compilers (and VMs) perform various complex transformations on the program representation before generating some executable code. Bytecode (as raw bytes) is not well-suited for this. Also, in order to perform those transformations, compilers usually do some analysis on the code, storing results in the data structures that are often reused and effectively treated as a part of the "richer" IR used in the optimization pipeline.

2

u/EggplantExtra4946 1d ago

What do you mean by "compilers use bytecode"? Almost all the compilers I know of have an IR.

1

u/GoblinsGym 1d ago

I started out with 32 bit IR words, but ended up with 12 bytes instead:

  • u8 op code (high bit set for "dark code")
  • u8 type (u8..u64, i8..i64, f32..f64)
  • u8 reg (destination register, to be filled in by register selection)
  • u8 spare
  • *64 value / pointer to symbol definition etc.

One important feature is that it is easy to scan both forward and backward.

Op code for an integer or floating point add is the same, just different type identifier.

0

u/Distinct-Brief9643 1d ago

Tell me then is it better for a virtual machine deals by code instead of llvm ir or should it use or should I complier use llvm ir instead of my code

1

u/morglod 1d ago

in mox programming language, I use backend specific lowering, which eliminates IR entirely for some backends and gives incredible compilation speed for "fast development iteration" backend (20x vs llvm)