Hi Cadet, thanks for the suggestion;
It's one way to solve this issue...I'll take it into account.
As for the word size issue, the virtual machine is word agnostic, and that's what's great about the nandtotetris layered approach. I use 32 bits for this, that etc on the x86 win32.
For now, I changed the Memory.jack look like this:
class Memory
{
static int s_HeapHandle;
static int s_Variable;
/** Initializes memory parameters. */
function void init()
{
asm
{
INVOKE GetProcessHeap ; get handle heap
mov Memory@0, eax ; s_HeapHandle = 0
}
return;
}
/** finds and allocates from the heap a memory block of the
* specified size and returns a reference to its base address. */
function int alloc(int size)
{
let s_Variable = size;
asm
{
push Memory@1 ; s_Variable
push 0
push Memory@0 ; s_HeapHandle
call HeapAlloc
mov Memory@1, eax ;variable has pointer to allocated memory or null
}
return s_Variable;
}
/** De-allocates the given object and frees its space. */
function void deAlloc(int objectAddress)
{
let s_Variable = objectAddress;
asm
{
push Memory@1 ; s_Variable
push 0
push Memory@0 ; s_HeapHandle
call HeapFree
}
return;
}
}
One advantage of using the c code is that it will work as is whether it's on windows or linux etc...
whereas the asm code calls are for windows in the example above...
cadet1620 wrote
One thing that you might consider is writing your Jack OS classes in C (not C++).
C has a simple calling convention (cdecl [1]) that should be compatible with Jack. Have your VM use something like "_ _ _" [2] instead of "." when generating identifier names in the ASM, and use the C calling convention for all calls generated by the VM.
Then your VM ASM can be directly linked to C. Example Memory.alloc and deAllc would look something like
#include <stdint.h>
#include <stdlib.h>
#include "JackClass.h"
int16_t Memory___alloc(int16_t size)
{
char * p;
p = malloc(size);
if ( ! p)
Sys___error(6); // does not return
return allocJackPointer(p);
}
int16_t Memory___deAlloc(int16_t this)
{
char * p;
p = derefJackPointer(this); // returns NULL if 'this' not in map
free(p);
removeJackPointer(this);
return 0;
}
etcJackPointer functions would need to translate between 32-bit system memory pointers and 16-bit memory "handles" that the OS code would use to access system.
[Stream of conscience writing...]
How do you handle vm references to 'this' and 'that'? The pointer registers are only 16-bit so they can't hold a 32-bit system pointers, but accessing handle+offset is invalid.
Calling Memory___Peek and ___Poke for all this and that references could really slow things down.
Maybe Windows Jack should have all types be 32-bit. Then you wouldn't need to translate pointers <-> handles.
You might want to download the n2t source disto and look at the java in BuiltinVMClasses. They work similarly to the above.
--Mark
----------
[1]
http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl[2] I don't know if any libraries the use 3 underscores in a row in identifier names.