0

Recently for fun I have decided to build a toy programming, compiler and a low-level register based interpreted vm. While starting to implement the virtual machine I got stuck. The stack which holds the variables and structs I implemented as separate arrays for each type. The problem is when I have a reference to a struct the elements are not aligned, int struct.x might be at address 1, and float struct.y might be at address 0, so accessing the struct by a reference would be impossible, because the indexes are not linear. How could I solve this?

stack int:     
//other 0: 5,        
//x 1: 67

stack long:  
//y 0: 56

stack reference:  
//pointer to x, struct access like c array: #00x01

For each type I mean for each primitive. I know I could implement it with unions but I want to learn how it is really implemented in java, c++ or c#, that's kind of the point of making a toy language, to better understand what you are programming.

9
  • In a real implementation, there is one stack (actually, one per thread) with data from all types intermixed on it. Commented Nov 30, 2015 at 20:18
  • but how can you have all types intermixed?
    – Coder3000
    Commented Nov 30, 2015 at 20:19
  • quite easily when you're constructing a program in assembly language (in which types barely exist). not so much when you're working in C++ Commented Nov 30, 2015 at 20:19
  • 2
    I'm not sure I can follow your example. Could you perhaps add a memory diagram of the stacks, or a bit of pseudo-code to illustrate your problem? Even sketching out the solution with unions might be helpful to communicate the problem. Also: Are you writing an high-level interpreter or a low-level virtual machine?
    – amon
    Commented Nov 30, 2015 at 20:22
  • 2
    Please forgive me if this sounds patronizing, but you are writing this in C++? With classes and such? Rather than writing C-esque code in C++? Polymorphism is often considered one of the foundations of Object Oriented programming, and if you aren't using while trying to write complex C++ code, it is quite possible that your design is fundamentally flawed and you may wish to go back and relearn some of the basics of C++ and how to use Object Oriented programming, encapsulation, classes, and such.
    – user40980
    Commented Nov 30, 2015 at 21:00

1 Answer 1

2

Definitely go with only one stack for everything.

You have two options:

  1. Declare a "StackEntry" union which may hold either a primitive data type, (char, bool, int, etc.) or a pointer to a struct, and then make your stack consist of such "StackEntry" unions. Each one of these unions will roughly be as large as a machine word.

  2. Do it the object oriented way: declare an "Object" class to represent any kind of object, declare derived classes, one to hold each kind of primitive and one to hold a struct, and make your stack a stack of Object. Each stack entry will be exactly one machine word, (a pointer to an Object,) but then for each stack entry you will have an entire dynamically allocated Object. This will perform worse, but it will be a lot neater and cleaner, saving you from a lot of headache on the long run. (Besides, if this is a toy VM, you really should not care about performance.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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