My own language | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

My own language

Hey guys! I'm on a project of my own programming language. I already got syntax, lexer, parser, ast etc. (in C++) The problem is, I'm trying to code a virtual machine for my language but I don't know how many registers to use. So far, I discovered that I need only two, which are both vector<string>. One for returning from functions (keep where I am), and second for.. well.. the stack. But I'm not sure and just wanted to ask. How many registers do I really need?

1st Aug 2018, 10:16 PM
Mustafa Yıldız
Mustafa Yıldız - avatar
2 ответов
Some VM's (such as Lua's) are register-based rather than stack, and can execute instructions faster as there's less overhead with no stack, but of course are limited to values stored in the registers and memory + instructions are larger because of the need to address more registers than usual. For a stack-based VM there's often the instruction pointer as you already have, but you could use an accumulator to store a calculated value, and depending how you're managing memory (if you're doing something like Simpletron) may need to know the address of where the free portion of memory starts, and if you're wanting to use a large amount of memory may need to registers be to address it in pages. There's other possible registers too, but I haven't messed with this stuff in a long time.
2nd Aug 2018, 12:50 AM
Tom Shaver
Tom Shaver - avatar
Tom Shaver I find out that I need 2 more. One for function arguments, and the other is for pointers. thank you :) Now all left is coding. ( The hard part )
2nd Aug 2018, 5:19 PM
Mustafa Yıldız
Mustafa Yıldız - avatar