Thread: bytecode/opcode interpreter

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    43

    bytecode/opcode interpreter

    I'm looking for a couple of examples and opinions on your views of how scripting languages create and implement bytecodes or opcodes.

    example:

    <script>

    for($i=0; $i < 10; $i++)
    print $i;

    </script>

    and then a C parser translates that to opcodes and executes.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by mingerso View Post
    I'm looking for a couple of examples and opinions on your views of how scripting languages create and implement bytecodes or opcodes.

    example:

    <script>

    for($i=0; $i < 10; $i++)
    print $i;

    </script>

    and then a C parser translates that to opcodes and executes.
    A "C parser" would cry if you tried to compile that.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mingerso View Post
    I'm looking for a couple of examples and opinions on your views of how scripting languages create and implement bytecodes or opcodes.

    example:

    <script>

    for($i=0; $i < 10; $i++)
    print $i;

    </script>

    and then a C parser translates that to opcodes and executes.
    bytecode programming requires a pretty decent knowledge of assembler programming - you just make a generic (and simple) assembler instruction set, and then implement a script compiler that generates instructions for that. I'm sure the Java bytecode is documented somewhere, but that may not be much help.

    It is of course possible to implement all the primitives (builtin functions and keywords) as "bytecode ops", but you still end up with something similar to machine code, but less flexible.

    However, it's probably easier to implement your language as an interpreted language first, then start thinking about how it can be compiled.

    A common problem with most scripting languages is the "dynamic typing" - for example in the code above, you do not give $i a type, it becomes (presumably) an integer when it's first assigned an integer value. This makes compiling it (whether to bytecode or "proper" machine-code) much harder, as you'd have to find all the uses of $i in that particular scope to find the proper type or, perhaps, you will have to allow a variable to change type throughout the code - I know PHP can do things like this:
    Code:
       $x = 0;
       ...  use $x as an integer ... 
       $x = "Hello, World" . $x;   // Assign $x as a string made from constant and it's integral value
       ... use $x as a string.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by matsp View Post
    bytecode programming requires a pretty decent knowledge of assembler programming - you just make a generic (and simple) assembler instruction set, and then implement a script compiler that generates instructions for that. I'm sure the Java bytecode is documented somewhere, but that may not be much help.

    It is of course possible to implement all the primitives (builtin functions and keywords) as "bytecode ops", but you still end up with something similar to machine code, but less flexible.

    However, it's probably easier to implement your language as an interpreted language first, then start thinking about how it can be compiled.

    A common problem with most scripting languages is the "dynamic typing" - for example in the code above, you do not give $i a type, it becomes (presumably) an integer when it's first assigned an integer value. This makes compiling it (whether to bytecode or "proper" machine-code) much harder, as you'd have to find all the uses of $i in that particular scope to find the proper type or, perhaps, you will have to allow a variable to change type throughout the code - I know PHP can do things like this:
    Code:
       $x = 0;
       ...  use $x as an integer ... 
       $x = "Hello, World" . $x;   // Assign $x as a string made from constant and it's integral value
       ... use $x as a string.
    --
    Mats
    I'm up to speed on this and the last comment and I'll look into the java docs - parsing and changing types is a fairly simple process.
    Last edited by mingerso; 03-31-2008 at 07:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter Features Question
    By audinue in forum C Programming
    Replies: 0
    Last Post: 10-19-2008, 07:29 AM
  2. interpreter
    By abuashraf in forum C Programming
    Replies: 4
    Last Post: 04-01-2008, 09:10 AM
  3. Replies: 0
    Last Post: 04-06-2007, 04:55 PM
  4. So I wrote brain.......... interpreter
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-07-2006, 09:24 AM
  5. Most pointless idea ever: Interpreter
    By Trauts in forum C++ Programming
    Replies: 5
    Last Post: 02-09-2003, 06:06 PM