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