Commit 44def0fb authored by Benoit Perrot's avatar Benoit Perrot
Browse files

2003-07-11 Benoît Perrot <benoit@lrde.epita.fr>

        * dev/mipsy-scanner-gen.py, 
        * src/inst/register.hh, src/vm/cpu.hh:
        Use a register vector for general purpose registers.
        * src/vm/virtual_machine.cc:
        Adjust.
parent 44c9211f
2003-07-11 Benot Perrot <benoit@lrde.epita.fr>
* dev/mipsy-scanner-gen.py,
* src/inst/register.hh, src/vm/cpu.hh:
Use a register vector for general purpose registers.
* src/vm/virtual_machine.cc:
Adjust.
2003-07-10 Benot Perrot <benoit@lrde.epita.fr>
* dev/mipsy.xml, src/inst/program_solver.hh, src/vm/virtual_machine.hh:
......
......@@ -53,6 +53,8 @@ print """
static std::string current_file;
using inst::Register;
%}
%option noyywrap
......@@ -99,35 +101,36 @@ for inst in instructions:
## Epilogue ----------------------------
print """
\"$zero\" {
yylval->reg = new inst::Register(inst::Register::zero);
return REGISTER;
}
\"$v\"[0-1] {
yylval->reg = new inst::Register(inst::Register::result, atoi(yytext + 2));
yylval->reg = new Register(Register::zero);
return REGISTER;
}
\"$a\"[0-3] {
yylval->reg = new inst::Register(inst::Register::argument, atoi(yytext + 2));
return REGISTER;
}
\"$t\"[0-9] {
yylval->reg = new inst::Register(inst::Register::temporary, atoi(yytext +2));
return REGISTER;
}
\"$s\"[0-8] {
yylval->reg = new inst::Register(inst::Register::saved, atoi(yytext + 2));
return REGISTER;
}
}"""
for i in range(2):
print "\"$v" + str(i) + "\" {"
print " yylval->reg = new Register(Register::v" + str(i) + ");"
print " return REGISTER;\n}"
for i in range(4):
print "\"$a" + str(i) + "\" {"
print " yylval->reg = new Register(Register::a" + str(i) + ");"
print " return REGISTER;\n}"
for i in range(10):
print "\"$t" + str(i) + "\" {"
print " yylval->reg = new Register(Register::t" + str(i) + ");"
print " return REGISTER;\n}"
for i in range(8):
print "\"$s" + str(i) + "\" {"
print " yylval->reg = new Register(Register::s" + str(i) + ");"
print " return REGISTER;\n}"
print """
\"$sp\" {
yylval->reg = new inst::Register(inst::Register::sp);
yylval->reg = new Register(Register::sp);
return REGISTER;
}
\"$fp\" {
yylval->reg = new inst::Register(inst::Register::fp);
yylval->reg = new Register(Register::fp);
return REGISTER;
}
\"$ra\" {
yylval->reg = new inst::Register(inst::Register::ra);
yylval->reg = new Register(Register::ra);
return REGISTER;
}
......@@ -139,7 +142,7 @@ print """
// << \"(use --FIXME to enable it)\" << std::endl;
// exit_set (exit_scan);
// }
yylval->reg = new inst::Register(inst::Register::unlimited, atoi(yytext +2));
yylval->reg = new Register(atoi(yytext +2));
return REGISTER;
}
......
......@@ -29,37 +29,29 @@ namespace inst
class Register
{
public:
typedef enum
enum kind_t
{
zero,
zero = 0,
at,
result,
argument,
temporary,
saved,
sp,
v0, v1,
a0, a1, a2, a3,
t0, t1, t2, t3, t4, t5, t6, t7,
s0, s1, s2, s3, s4, s5, s6, s7,
t8, t9,
sp = 29,
fp,
ra,
unlimited
} kind_t;
};
public:
Register (kind_t kind, int index = -1):
kind(kind),
index(index)
Register (kind_t kind):
kind(kind), index(-1)
{
}
Register (int index):
kind(unlimited), index(index)
{
switch (kind)
{
case zero:
case at:
case fp:
case sp:
case ra:
assertion(index == -1);
break;
default:
assertion(index >= 0);
}
}
Register& operator=(const Register& reg)
{
......@@ -67,7 +59,7 @@ namespace inst
this->index = reg.index;
return (*this);
}
public:
kind_t get_kind () const
{
......@@ -81,19 +73,10 @@ namespace inst
public:
void print (std::ostream& ostr) const
{
switch (kind)
{
case zero: ostr << "$zero"; break;
case at: ostr << "$at"; break;
case result: ostr << "$v" << index; break;
case argument: ostr << "$a" << index; break;
case temporary: ostr << "$t" << index; break;
case saved: ostr << "$s" << index; break;
case sp: ostr << "$sp"; break;
case fp: ostr << "$fp"; break;
case ra: ostr << "$ra"; break;
case unlimited: ostr << "$x" << index; break;
}
if (kind != unlimited)
ostr << "$" << kind;
else
ostr << "$x" << index;
}
protected:
......
......@@ -43,54 +43,46 @@ namespace vm
public:
void reset()
{
zero = 0;
at = 0;
for (unsigned i = 0; i < 2; ++i)
v[i] = 0;
for (unsigned i = 0; i < 4; ++i)
a[i] = 0;
for (unsigned i = 0; i < 10; ++i)
t[i] = 0;
for (unsigned i = 0; i < 9; ++i)
s[i] = 0;
sp = Memory::stack_bottom;
fp = Memory::stack_bottom;
ra = 0;
pc = 0;
for (unsigned i = 0; i < 32; ++i)
GPR[i] = 0;
GPR[inst::Register::sp] = Memory::stack_bottom;
GPR[inst::Register::fp] = Memory::stack_bottom;
lo = 0;
hi = 0;
}
public:
register_t get_at() const { return at; }
void set_at(register_t r) { at = r; }
register_t get_at() const { return GPR[inst::Register::at]; }
void set_at(register_t r) { GPR[inst::Register::at] = r; }
register_t get_v(unsigned i) const
{
precondition(i < 2);
return v[i];
}
void set_v(unsigned i, register_t r)
{
precondition(i < 2);
v[i] = r;
}
register_t get_v0() const { return GPR[inst::Register::v0]; }
void set_v0(register_t r) { GPR[inst::Register::v0] = r; }
register_t get_a(unsigned i) const
{
precondition(i < 4);
return a[i];
}
register_t get_a0() const { return GPR[inst::Register::a0]; }
register_t get_a1() const { return GPR[inst::Register::a1]; }
register_t get_sp() const { return sp; }
void set_sp(register_t r) { sp = r; }
register_t get_sp() const { return GPR[inst::Register::sp]; }
void set_sp(register_t r) { GPR[inst::Register::sp] = r; }
register_t get_fp() const { return fp; }
void set_fp(register_t r) { fp = r; }
register_t get_fp() const { return GPR[inst::Register::fp]; }
void set_fp(register_t r) { GPR[inst::Register::fp] = r; }
register_t get_ra() const { return ra; }
void set_ra(register_t r) { ra = r; }
register_t get_ra() const { return GPR[inst::Register::ra]; }
void set_ra(register_t r) { GPR[inst::Register::ra] = r; }
register_t get_register(const inst::Register::kind_t k) const
{
assertion(k != inst::Register::unlimited);
return GPR[k];
}
void set_register(const inst::Register::kind_t k,
register_t r)
{
assertion((k != inst::Register::unlimited) &&
(k != inst::Register::zero));
GPR[k] = r;
}
register_t get_unlimited(int i) const
{
......@@ -99,45 +91,27 @@ namespace vm
}
void set_unlimited(int i, register_t r)
{
unlimited.put(i, r);
unlimited.put(i, r);
}
register_t get_register(const inst::Register& reg) const
{
switch (reg.get_kind())
{
case inst::Register::zero: return zero;
case inst::Register::at: return at;
case inst::Register::result: return v[reg.get_index()];
case inst::Register::argument: return a[reg.get_index()];
case inst::Register::temporary: return t[reg.get_index()];
case inst::Register::saved: return s[reg.get_index()];
case inst::Register::sp: return sp;
case inst::Register::fp: return fp;
case inst::Register::ra: return ra;
case inst::Register::unlimited:
// FIXME: check existence!
return unlimited.get(reg.get_index ());
}
return zero;
if (reg.get_kind() != inst::Register::unlimited)
return GPR[reg.get_kind()];
// FIXME: check existence!
return unlimited.get(reg.get_index ());
}
void set_register(const inst::Register& reg, register_t r)
{
switch (reg.get_kind())
{
case inst::Register::zero: break;
case inst::Register::at: at = r; break;
case inst::Register::result: v[reg.get_index()] = r; break;
case inst::Register::argument: a[reg.get_index()] = r; break;
case inst::Register::temporary: t[reg.get_index()] = r; break;
case inst::Register::saved: s[reg.get_index()] = r; break;
case inst::Register::sp: sp = r; break;
case inst::Register::fp: fp = r; break;
case inst::Register::ra: ra = r; break;
case inst::Register::unlimited:
unlimited.put(reg.get_index (), r);
break;
default:
GPR[reg.get_kind()] = r;
break;
}
}
......@@ -163,14 +137,16 @@ namespace vm
}
protected:
register_t zero;
register_t at;
register_t v[2];
register_t a[4];
register_t t[10];
register_t s[9];
register_t sp, fp;
register_t ra;
register_t GPR[32];
// register_t zero;
// register_t at;
// register_t v[2];
// register_t a[4];
// register_t t[10];
// register_t s[9];
// register_t sp, fp;
// register_t ra;
Table<int, register_t> unlimited;
......
......@@ -492,16 +492,16 @@ namespace vm
void
VirtualMachine::visit(const inst::Syscall&)
{
switch (cpu.get_v(0))
switch (cpu.get_v0())
{
// print_int (integer: $a0)
case 1:
std::cout << cpu.get_a(0);
std::cout << cpu.get_a0();
break;
// print_string (buffer: $a0)
case 4:
for (int i = cpu.get_a(0); true; ++i)
for (int i = cpu.get_a0(); true; ++i)
{
char b = _memory.load_byte(i);
if (b == 0)
......@@ -515,20 +515,20 @@ namespace vm
{
int i = 0;
int c = 0;
for (; (i < cpu.get_a(1) - 1) && (c != '\n') && (c != '\r') ; ++i)
for (; (i < cpu.get_a1() - 1) && (c != '\n') && (c != '\r') ; ++i)
{
c = std::cin.get();
if (std::cin.eof())
break;
_memory.store_byte(cpu.get_a(0) + i, c);
_memory.store_byte(cpu.get_a0() + i, c);
}
_memory.store_byte(cpu.get_a(0) + i, 0);
_memory.store_byte(cpu.get_a0() + i, 0);
}
break;
// sbrk (size: $a0)
case 9:
cpu.set_v(0, _memory.sbrk(cpu.get_a(0)));
cpu.set_v0(_memory.sbrk(cpu.get_a0()));
break;
// exit
......
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