2

I was considering attempting to research and build a very small contrived compiler for a subset of a language. I think in reflection it will be too difficult a problem for me at my current skill level.

However, something has me quite intrigued, how does a compiler translate an abstract idea such as an object into assembly/binary?

for example:

class Dog            
{
  public:            
    Dog(string name);
    ~Dog();
    string GetName();
};

dog *Dog = new Dog('rover');

How does that get translated into machine code - it is seriously baffiling.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • That's just the interface definition. Probably does not make it into the binary at all, anymore than a header file. It's just for the compiler to see. The implementation will be compiled. – Thilo Aug 01 '13 at 23:40
  • 3
    I think this question is too broad. I'd suggest searching for a tutorial on compiler construction or getting a book about compilers. – Troy Aug 01 '13 at 23:43
  • What are you asking? What type of code is generated (That is, to what type of code is translated the OO style code), or how the compiler works? – Manu343726 Aug 01 '13 at 23:44
  • Well i know that ultimately machine code is generated, but specifically, how does a compiler know in what specific machine code and memory state translates into that high level instance of a Dog class – Marty Wallace Aug 01 '13 at 23:46
  • 3
    At the machine code level there's usually just data and functions. A class-based type system is a high-level abstraction that the compiler has to track in order to know which data belongs to which user-defined language construct. – Kerrek SB Aug 01 '13 at 23:58
  • Do `Dog dog("Rover")` – David G Aug 02 '13 at 00:03
  • I recently read a book on compilers(The Dragon book). It had a chapter on compilers for object oriented languages, You might be interested in that. – nj-ath Aug 02 '13 at 04:46
  • Assembly language is a semester class. Compilers is a semester class. Programming language design is a semester class. They are all interesting topics. – brian beuning Aug 02 '13 at 22:57
  • Read about V-tables, it's a pretty trivial thing. For duck-typed languages it would be a bit different (e.g., hash maps instead of flat indexed tables). – SK-logic Aug 06 '13 at 10:11

1 Answers1

1

Behind the scenes? No big surprises here. Let's take a reasonable, byt hypothetical compiler:

First off, there's a global Dog* dog. Add a name to the global object table, size 4, type (Dog) pointer. Next, there's an initialization with a non-constant expression. Write a small bit of code __dog__initializer which calls ::operator new(sizeof(Dog)), stores the result in dog and then calls __Ctor_Dog. There's a string constant "rover" needed, so add that to the global string table.

When the whole file is read, all tables are translated into parts of the object file, so the linker can put them together. That's not really generating binary code, just putting existing parts together.

Remember, this is just a hypothetical implementation, and details definitely differ in practice.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • See a detailed answer here: http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work?rq=1 – gaoithe Sep 25 '16 at 23:14