Skip to content

Commit

Permalink
Added functionality to push and pop frames and values on framesc
Browse files Browse the repository at this point in the history
  • Loading branch information
jbf committed Feb 23, 2011
1 parent b3fce10 commit c41de30
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 16 deletions.
46 changes: 45 additions & 1 deletion src/vm/cpp_vm.cpp
@@ -1,14 +1,30 @@
#include "cpp_vm.hpp"
#include <iostream>

/* Frame */
Context::Frame::Frame(int s, Frame *p) :
prev(p),
next(0),
size(s),
top(0),
u(0)
{
;
u = new stack_entry[size];
for (int i = 0; i < size; i++) {
*(u+i)=0;
}
}

std::ostream& operator<<(std::ostream& os, const Context::Frame& f) {
os << "Frame at: " << &f << ", size: " << f.size;
return os;
}

Context::Frame::~Frame() {
delete [] u; // what happens to each element now?
}

/* Context */
Context::Context(int initial_frame_size) :
top(0),
bottom(0)
Expand Down Expand Up @@ -63,6 +79,34 @@ void Context::pop_frame() {
delete f;
}

void Context::push_on(stack_entry s) {
if (bottom == 0) {
//CMH: error
return;
}
if(bottom->top == bottom->size) {
//CMH: error
return;
}
*(bottom->u + bottom->top) = s;
bottom->top++;
}

Context::stack_entry Context::pop_off(bool zero) {
if (bottom->top < 1) {
//CMH: error
return 0;
}

bottom->top--;
stack_entry t = *(bottom->u + bottom->top);
if(zero) {
*(bottom->u + bottom->top) = 0;
}

return t;
}

Context::Context() :
top(0),
bottom(0)
Expand Down
26 changes: 21 additions & 5 deletions src/vm/cpp_vm.hpp
@@ -1,34 +1,45 @@
#ifndef CPP_VM_HPP
#define CPP_VM_HPP 1

#include <iosfwd>

class Context {
typedef uint64_t stack_entry;

public:
/* Frame */
class Frame {
private:
Frame *prev;
Frame *next;
int size;
union {
void *obp;
unsigned long im;
} *u;
int top;
stack_entry *u;

Frame(int s, Frame *p);

Frame(const Frame&);
Frame& operator=(const Frame&);

public:

~Frame();

friend class Context;
friend class TestContext;
friend std::ostream& operator<<(std::ostream&, const Context::Frame&);
};

/* Registers */
class Registers {
friend class Context;
};

/* Handler */
class Handler {
friend class Context;
};

/* Meta */
class Meta {
friend class Context;
};
Expand All @@ -45,9 +56,14 @@ class Context {
explicit Context(int initial_frame_size); // no conversion constructor
~Context();

/* Frame management */
Frame * push_new_frame(int size);
void pop_frame();

/* Values on frame */
void push_on(stack_entry);
stack_entry pop_off(bool zero=false);

friend class TestContext;
};

Expand Down
81 changes: 71 additions & 10 deletions src/vm/test/test_context.cpp
Expand Up @@ -4,61 +4,122 @@

class TestContext {
public:
static void test() {
static void test_frames() {
Context c;
Context::Frame *t;

std::cout << "===test_frames===" << std::endl;

c.push_new_frame(0);
std::cout << "Frame 0" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.push_new_frame(1);
std::cout << "Frame 1" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.push_new_frame(2);
std::cout << "Frame 2" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.push_new_frame(3);
std::cout << "Frame 3" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.pop_frame();
std::cout << "Frame 2" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.pop_frame();
std::cout << "Frame 1" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.pop_frame();
std::cout << "Frame 0" << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}

c.pop_frame();
std::cout << "Frame NULL" << std::endl;
std::cout << "top: " << c.top << ", bottom: " << c.bottom << std::endl;
for(t = c.top; t != 0; t = t->next) {
std::cout << t << std::endl;
std::cout << *t << std::endl;
}
}

static void test_values() {
Context c;
Context::Frame *t;

std::cout << std::endl << "===test_values==" << std::endl;

c.push_new_frame(5);
c.push_on(1);
c.push_on(2);
c.push_on(3);

std::cout << "Frame 0" << std::endl;
t = c.bottom;
std::cout << *t << std::endl;
for (int i = 0; i < t->top; i++) {
std::cout << "i: " << i << ", content: " << (t->u + i) <<
", value: " << *(t->u + i) << std::endl;
}
std::cout << "unused stack:" << std::endl;
for (int i = t->top; i < t->size; i++) {
std::cout << "i: " << i << ", content: " << (t->u + i) <<
", value: " << *(t->u + i) << std::endl;
}

c.push_new_frame(10);
c.push_on(11);
c.push_on(12);

std::cout << "Frame 1" << std::endl;
t = c.bottom;
std::cout << *t << std::endl;
for (int i = 0; i < t->top; i++) {
std::cout << "i: " << i << ", content: " << (t->u + i) <<
", value: " << *(t->u + i) << std::endl;
}
std::cout << "unused stack:" << std::endl;
for (int i = t->top; i < t->size; i++) {
std::cout << "i: " << i << ", content: " << (t->u + i) <<
", value: " << *(t->u + i) << std::endl;
}

c.pop_frame();
c.pop_off(true);

std::cout << "Frame 0" << std::endl;
t = c.bottom;
std::cout << *t << std::endl;
for (int i = 0; i < t->top; i++) {
std::cout << "i: " << i << ", content: " << (t->u + i) <<
", value: " << *(t->u + i) << std::endl;
}
std::cout << "unused stack:" << std::endl;
for (int i = t->top; i < t->size; i++) {
std::cout << "i: " << i << ", content: " << (t->u + i) <<
", value: " << *(t->u + i) << std::endl;
}
}
};

int main(void) {
TestContext::test();
TestContext::test_frames();
TestContext::test_values();
}

0 comments on commit c41de30

Please sign in to comment.