Commit 9fd8730e authored by Benoit Perrot's avatar Benoit Perrot
Browse files

* src/vm/virtual_machine.hh, src/vm/virtual_machine.cc:

        Define and use a step of execution.
        * src/common.hh, src/vm/vm-tasks.cc:
        Adjust.
parent 44def0fb
2003-07-11 Benot Perrot <benoit@lrde.epita.fr>
* src/vm/virtual_machine.hh, src/vm/virtual_machine.cc:
Define and use a step of execution.
* src/common.hh, src/vm/vm-tasks.cc:
Adjust.
2003-07-11 Benot Perrot <benoit@lrde.epita.fr>
* dev/mipsy-scanner-gen.py,
......
......@@ -29,8 +29,8 @@ enum exit_t
exit_failure = 1,
exit_scan = 2,
exit_parse = 3,
exit_solve = 4
// FIXME: exit_runtime
exit_solve = 4,
exit_runtime = 5
};
extern exit_t exit_status;
......
......@@ -279,14 +279,14 @@ namespace vm
VirtualMachine::visit(const inst::Sb& sb)
{
register_t addr = cpu.get_register(sb.get_base ()) + sb.get_offset ();
_memory.store_byte(addr, cpu.get_register(sb.get_src ()));
memory.store_byte(addr, cpu.get_register(sb.get_src ()));
}
void
VirtualMachine::visit(const inst::Sw& sw)
{
register_t addr = cpu.get_register(sw.get_base ()) + sw.get_offset ();
_memory.store_word(addr, cpu.get_register(sw.get_src ()));
memory.store_word(addr, cpu.get_register(sw.get_src ()));
}
// Load
......@@ -294,14 +294,14 @@ namespace vm
VirtualMachine::visit(const inst::Lb& lb)
{
register_t addr = cpu.get_register(lb.get_base ()) + lb.get_offset ();
cpu.set_register(lb.get_dest (), _memory.load_byte(addr));
cpu.set_register(lb.get_dest (), memory.load_byte(addr));
}
void
VirtualMachine::visit(const inst::Lbu& lbu)
{
register_t addr = cpu.get_register(lbu.get_base ()) + lbu.get_offset ();
unsigned b = _memory.load_byte(addr);
unsigned b = memory.load_byte(addr);
cpu.set_register(lbu.get_dest (), b % 256);
}
......@@ -309,7 +309,7 @@ namespace vm
VirtualMachine::visit(const inst::Lw& lw)
{
register_t addr = cpu.get_register(lw.get_base ()) + lw.get_offset ();
cpu.set_register(lw.get_dest (), _memory.load_word(addr));
cpu.set_register(lw.get_dest (), memory.load_word(addr));
}
// --------------------------------------------------------------------------
......@@ -503,7 +503,7 @@ namespace vm
case 4:
for (int i = cpu.get_a0(); true; ++i)
{
char b = _memory.load_byte(i);
char b = memory.load_byte(i);
if (b == 0)
break;
std::cout << b;
......@@ -520,20 +520,20 @@ namespace vm
c = std::cin.get();
if (std::cin.eof())
break;
_memory.store_byte(cpu.get_a0() + i, c);
memory.store_byte(cpu.get_a0() + i, c);
}
_memory.store_byte(cpu.get_a0() + i, 0);
memory.store_byte(cpu.get_a0() + i, 0);
}
break;
// sbrk (size: $a0)
case 9:
cpu.set_v0(_memory.sbrk(cpu.get_a0()));
cpu.set_v0(memory.sbrk(cpu.get_a0()));
break;
// exit
case 10:
_halt = true;
halt = true;
break;
default:
......
......@@ -20,6 +20,7 @@
# define VM_VIRTUAL_MACHINE_HH
# include "misc/contract.hh"
# include "common.hh"
# include "inst/program.hh"
# include "inst/visitor.hh"
......@@ -34,42 +35,44 @@ namespace vm
protected inst::ConstVisitor
{
public:
VirtualMachine()
VirtualMachine():
program(0), text_section(0)
{
_program = 0;
}
public:
void load_program(inst::Program& program)
void load_program(const inst::Program& program)
{
_program = &program;
_memory.store(program.data_section());
}
public:
void execute(bool trace = false)
{
precondition(_program);
const inst::TextSection& text_section = (*_program).text_section ();
if (! text_section.has_label(inst::Label("main")))
if (! program.text_section ().has_label(inst::Label("main")))
{
std::cerr << "No `main' label in assembly file." << std::endl;
exit_set(exit_runtime);
return;
}
cpu.set_pc(text_section.get_offset(inst::Label("main")));
_halt = false;
while (!_halt)
{
const inst::Inst& ri = text_section[cpu.get_pc() / 4];
cpu.set_pc(cpu.get_pc() + 4);
this->program = &program;
text_section = &(program.text_section ());
memory.store(program.data_section());
cpu.set_pc(text_section->get_offset(inst::Label("main")));
}
if (trace)
std::cout << ri << std::endl;
ri.accept(*this);
}
public:
void step(bool trace = false)
{
const inst::Inst& ri = (*text_section)[cpu.get_pc() / 4];
cpu.set_pc(cpu.get_pc() + 4);
if (trace)
std::cout << ri << std::endl;
ri.accept(*this);
}
void execute(bool trace = false)
{
precondition(program);
halt = false;
while (!halt)
step(trace);
}
protected:
......@@ -145,12 +148,13 @@ namespace vm
}
protected:
bool _halt;
bool halt;
protected:
Cpu cpu;
inst::Program* _program;
Memory _memory;
const inst::Program* program;
const inst::TextSection* text_section;
Memory memory;
};
} // namespace vm
......
......@@ -41,6 +41,8 @@ namespace vm
{
vm::VirtualMachine vm;
vm.load_program(* parse::tasks::program);
if (exit_status != exit_success)
exit(exit_status);
vm.execute(trace_exec_p);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment