Questions on creating a virtual machine: Operations

  • Thread starter Morten Aune Lyrstad
  • Start date
M

Morten Aune Lyrstad

Hi there! I'm in the process of writing a virtual machine for my script
language, and I'm trying to determine the best way of handling operations on
different types of data. My language supports floats, ints, and unsigned
ints for numerical data. I have implemented the basic "assembly-ish" version
of the language, but I can't seem to make up my mind on how to do numerical
operations, like comparisons and mathematical functions. Should I have
separate opcodes for each datatype (like iadd, uadd, fadd for ints, uints
and floats) or should I have a single operation for all types and let C++
handle the type conversions? This vm will be used in my games, so it needs
to be fast.

Any thoughts? And perhaps any other general recommendations when it comes to
writing a vm?

Help will be very appreciated.
Yours,
Morten Aune Lyrstad
 
D

Dave Townsend

Morten Aune Lyrstad said:
Hi there! I'm in the process of writing a virtual machine for my script
language, and I'm trying to determine the best way of handling operations on
different types of data. My language supports floats, ints, and unsigned
ints for numerical data. I have implemented the basic "assembly-ish" version
of the language, but I can't seem to make up my mind on how to do numerical
operations, like comparisons and mathematical functions. Should I have
separate opcodes for each datatype (like iadd, uadd, fadd for ints, uints
and floats) or should I have a single operation for all types and let C++
handle the type conversions? This vm will be used in my games, so it needs
to be fast.

Any thoughts? And perhaps any other general recommendations when it comes to
writing a vm?

Help will be very appreciated.
Yours,
Morten Aune Lyrstad
If performance is your goal, it would seem that separate op codes for each
type/operation
would be best, since you would avoid the extra run-time operation of
decoding the actual
values.
 
M

Morten Aune Lyrstad

Thank you for your quick response.
Your answer was wat I thought it would be. However: You talk about decoding
overhead: Currently the actual type of the variable is stored as well (I
need to delete string memory on exit). Would this have any effect on your
evaluation?

Yours,
Morten Aune Lyrstad
 
D

Dave Townsend

Morten Aune Lyrstad said:
Thank you for your quick response.
Your answer was wat I thought it would be. However: You talk about decoding
overhead: Currently the actual type of the variable is stored as well (I
need to delete string memory on exit). Would this have any effect on your
evaluation?

Yours,
Morten Aune Lyrstad

Morten,

Its a bit hard to say without examining the code. However, I'm sure you can
always
right specialized/customized functions for dealing with specific types which
would outperform
the general purpose approach. Its all a trade-off, runtime performance
against code
complexity ( well not quite complexity, but you'd have more cases to code
and to test).
(one idea that has just sprung into my head is that you might use templates
so your effort
in coding would be reduced somewhat, but you would still get the runtime
performance advantage. ).

You really need to benchmark alternatives, its ok for me to say one approach
is bound to
be faster than the other, but I'm not saying how much, if the difference is
marginal, you
might want to look at the general approach since this is likely to be less
complicated to
get right.

good luck!

dave
 
M

Morten Aune Lyrstad

Thank you very much! Especially for the template function suggestion.
Genious! I hadn't thought of that :) I'll continue on my current road then,
with separate operations for each datatype.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top