-2

I was posed the question by a classmate asking since an OS is an extended or virtual machine, does the compiler need to know the number of registers, or instructions of the processor when it generates assembly code of a C program.

I've spent a while scouring the internet and here is what I think...

It doesn't need to know the number of registers because being a virtual machine it has unlimited resources in memory per say.

However, it does need to know the instructions of the processor to know when it is able to perform specific functions at specific times.

I was wondering if someone could clarify this for me because I'm not very confident in my answers.

Bob
  • 1,344
  • 3
  • 29
  • 63
  • 2
    "since an OS is an extended or virtual machine" - Huh? That sounds wrong. What do you mean? – too honest for this site Feb 03 '16 at 17:57
  • @Olaf: that could sound ok, in a very specific sense. See my answer – Basile Starynkevitch Feb 03 '16 at 17:59
  • @BasileStarynkevitch: Hmm... maybe. But I strongly suspect there are quite some missconceptions leading to the question. To really understand the implications, these missconceptions have to be resolved first. – too honest for this site Feb 03 '16 at 18:02
  • It was a prefessor given question from a class I wasn't in and it got be thinking, mainly because of my extreme lack of knowledge on the subject. – Bob Feb 03 '16 at 18:15
  • If the compiler doesn't know the number of registers, how can it decide what to store in registers and what not to store in registers? Isn't that one of its jobs? – David Schwartz Feb 03 '16 at 18:15
  • @DavidScwartz: some compilers are generating C code (not assember) – Basile Starynkevitch Feb 03 '16 at 18:26
  • I have not seen any CPU that can execute C. The ones I've seen only understand binary instruction codes and data. If the output is a C source text file, I would suggest that it's not a compiler. – Martin James Feb 03 '16 at 22:30
  • My `gcc` compiler is emitting assembler files, and my CPU cannot handle it neither; also several things like Chicken or Oz are named compilers and translate some language (e.g. Scheme) to C. The first C++ implementations emitted C, and Cfront was a compiler – Basile Starynkevitch Feb 04 '16 at 06:18

2 Answers2

5

In practice, the compiler is compiling (into object code, often via some assembler file) not only for a target processor (in particular instruction set architecture - ISA), but for a target application binary interface - ABI, which defines some conventions regarding register usage (and how to make system calls) & calling conventions.

An Operating system (provided by the kernel) is - or gives to application programs and processes - a virtual machine very close to the processor; the VM is the (user-mode, unpriviledged) machine instructions + an instruction (SYSENTER) to switch into kernel or supervisor mode for system calls.

See also this & that. Regarding compilers, read about register allocation, instruction scheduling, optimizing compilers.

If you have GCC on your computer, try compiling a hello-world program (perhaps in a fresh directory) with gcc -fverbose-asm -O -S hello.c then look into the generated assembler code hello.s; add -fdump-tree-gimple and look into additional compiler dump file[s] (and even more of them with -fdump-tree-all)

PS. Some compilers compile to machine code in memory (e.g. SBCL). Read also about JIT compilers. Other compilers compile to C code.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Compilation have several stages, from different abstractions to the target machine and this depend on the compiler architecture.

In some stages, registers are not very limited, but at some stages later a mapping is done. You can read about register allocation for more details. I can also suggest you to have a look at Appel's book about compilers architecture.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69