|
|
This thread is locked; no one can reply to it.
|
1
2
|
| What is a TOKEN (C++) |
|
Thomas Fjellstrom
Member #476
June 2000
|
I must tell you, the first VM i did was one of my first real projects, back almost 6 years ago. The code has been worked on at various times over the years, so its kinda messy and not well thought out. casminfo.txt:
The actual code for the assembler is attached (comp.c), I assume it won't fi tin a code box, even with much of the unnesesary bits pulled out. Also attached (parse.c) is the assembler for my newer script vm. edit: Heres an example asm file for the parse.c assembler:
This one utilized the allegro binding I made for it. screen is allegro's screen, and "thing" is an object that the program used to draw a box or pixle on screen. And heres an example for he comp.c assembler:
edit2, and here's the lib.inc you see from that last example:
Yup, a mini libc -- |
|
kentl
Member #2,905
November 2002
|
Yeah OMG is a good expression. |
|
X-G
Member #856
December 2000
|
Needs a CRZ opcode. -- |
|
Thomas Fjellstrom
Member #476
June 2000
|
Thanks The second vm I made is pretty cool. It has psuedo OO support. you can set a "foo.bar" to a function: set "foo.bar" "somefunction" call it using the "extended" function calling support, as: foo.bar; and the assembler will do call "somefunction" "foo". And the "second pass" will attempt to turn all string references (which are looked up in a symtab hash at runtime normally), into direct symbol references (pointers to the stuff stored in the symtab, instead of a string which may need to be parsed, ie: "foo.bar.baz" requires the engine to split it at the dots, and then do a look up for baz in bar, and bar in foo, and foo in the global symtab...), which speeds up the engine by N times. (gotta be more than 10 but I can't recall). Oh yeah, the "foo.bar" syntax means look up the "bar" property in the "foo" object, and "foo.bar.baz" means, look up the baz property in the (possibly anon) object referenced in bar, and lookup the bar property in the foo object. I don't think i could have made it more complex >:) edit: X-G, excuse my ignorance, but what is a CRZ opcode? I've not heard of that one. A couple google searches didn't seem to help either. -- |
|
X-G
Member #856
December 2000
|
So it has first-class functions? -- |
|
Thomas Fjellstrom
Member #476
June 2000
|
The newer of the two does yes. function doit set doit system.stack.pop print doit.value ret
-- |
|
nonnus29
Member #2,606
August 2002
|
Nice work Moose. CRZ op rules. Quote: Yeah he needs to know assembler in that case. Can't really get why he head to implement his compiler in assembler though, but I guess it could be a good exercise if you study assembler. I don't think the OP was posting about a compilation oriented assignment. From what I've seen here in the states, infix to postix is a standard exercise students do when studying stacks etc... You certainly wouldn't use that method in a real compiler, you'd generate assembler from the AST. And you don't have to generate code at all. For my little Basic thingy I'm executing from the tree. |
|
Ciro Duran
Member #3,011
December 2002
|
How much compilers would assembler compile, if assembler would compile compilers? :-P (Ducks) --- In the beginning, God said "light_source { <0, 0, 0> color White }" and light was created. |
|
kentl
Member #2,905
November 2002
|
Quote: I don't think the OP was posting about a compilation oriented assignment. From what I've seen here in the states, infix to postix is a standard exercise students do when studying stacks etc... You certainly wouldn't use that method in a real compiler, you'd generate assembler from the AST. You're probably right about the goal of the assignment. I didn't check out the code he posted. Infix to postfix is a common excercise here as well when studying stacks. Quote: And you don't have to generate code at all. For my little Basic thingy I'm executing from the tree. Yeah. How is it going with that? |
|
Thomas Fjellstrom
Member #476
June 2000
|
IMO, compiling to some slim bytecode will always be more efficient runtime wise than traversing a tree. Not that its necessarily needed in all cases, but I like to brag about irrelevant things like MIPS, FLOPS and MHZ. -- |
|
kentl
Member #2,905
November 2002
|
It sounds sensible to me at least. Perhaps there is also a bit more flexibility in there. As you can layer things so that you can write a new backend for some other type of format (like Java Byte Code or something). On the other hand executing an interpreter directly from the tree is probably easier to implement. |
|
Thomas Fjellstrom
Member #476
June 2000
|
I always had this mental block with doing a direct execute interpreter. Wasn't ever able to fully grasp how to make one when I was still learning about VMs and such. -- |
|
nonnus29
Member #2,606
August 2002
|
It's coming along slowly. The method I'm using is pretty simple when your just passing ints around, but when you starting adding ints OR floats, it doesn't work:
As you can see the exec function just returns an int. Now I'm looking into using a runtime store of some sort to pass values around. But it's two different issues; compiling and then what you do with it. This is a question of the execution environment. And I'm beginning to see why emitting bytecode for a virtual machines is such a good idea. |
|
|
1
2
|