0

While direct Language-Assembly compilers generate the code required to perform specific task for the given platform, how do interpreters do it?

Abstract example

The following pseudo-code...

printf("Hello World");

...would compile to this assembly language code.

org 100h
mov dx,msg
mov ah,9
int 21h
mov ah,4Ch
int 21h
msg db 'Hello, World!',0Dh,0Ah,'$'

I would like to take Python as an example. In CPython we have a print statement represented by PRINT_ITEM VM instruction. Then we have a print() function which (sic!) I believe calls directly or not the print statement, I may be wrong. But then does it mean that we cannot do something without implementing it before in the VM's source code? How does then Python/other interpreted languages implement more complex functions like writing data to file? Does it mean that really all of the standard library/basic functionality has to be implemented in the VM's code.

What is called when we want to write the data to the file? There is no direct VM instruction for that in CPython. Does it then call some method which is defined in the CPython's C code? Or maybe the C code exposes just some streams which enable that to the interpreted code?

I hope you can understand what I mean.

  • You may want to consider updating your references to ones that aren't using 16-bit assembly and DOS programming interfaces... (In fact, doing so might lead to you discovering the answer to your question yourself.) – Derek Elkins Aug 19 '17 at 2:12
  • 1
    Interpreters and compilers work exactly the same way. The only difference is that a compiler would write the code to output whereas the interpreter would run it. – Jörg W Mittag Aug 19 '17 at 4:57
1

I would like to take Python as an example. In CPython we have a print statement represented by PRINT_ITEM VM instruction. Then we have a print() function which (sic!) I believe calls directly or not the print statement, I may be wrong.

Print statement is in Python 2. Print function is in Python 3. That's change between the major versions. And, print function does not call print statement, it is directly sending items to an output stream.

Does it mean that really all of the standard library/basic functionality has to be implemented in the VM's code.

Mainly yes. Of course, some of it can be implemented already in the code emulated by an interpreter; it's very often. But, an interpreter will always have a portion of code that is implemented on lower level compiled language (finally, executed by a processor). Whether some functionality is implemented in this interpreter base, depends on two factors:

  1. Something cannot be implemented at all in the interpreted language. This includes interpreter startup and shutdown, instruction execution, OS interaction, direct memory access and garbage collection, and so on.
  2. Something is very inefficient to be implemented in the interpreted language. This factor is very specific to a particular implementation, but most interpreters are really slow with intensive calculations and long object chain accesses.
0

For each of the commands supported by the interpreter there would be a bit of pre-compiled plumbing code that directs the arguments of the command to an interpreter specific library function. The library function would do the language specific error handling and could ultimately delegate the low-level operation to an OS API.

So yes, there would be a significant amount of pre-implemented code supporting the behavior of the interpreter.

  • 2
    It goes on. The code in the interpreter or the standard library is essentially also just a bit of plumbing that eventually calls file-writing code in the operating system. This code may in turn delegate to the file system implementation library, to the kernel or to the device driver. Practically all the I/O you'd want to do in python, people already wanted to do in older languages, so there are a lot of preexisting facilities for it. – Kilian Foth Aug 19 '17 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.