Commit 41930bab authored by Benoit Perrot's avatar Benoit Perrot
Browse files

Allow constant for destination of branch instruction.

parent 70b965c1
2003-06-29 Benot Perrot <benoit@lrde.epita.fr>
* dev/mipsy-parser-gen.py:
Allow expression for destination of branch instructions.
* dev/mipsy-inst-gen.py:
Generate an Exp& variable for a label.
* src/inst/program_builder.cc, src/inst/program_solver.hh,
* src/vm/virtual_machine.cc,
Adjust.
2003-06-29 Benot Perrot <benoit@lrde.epita.fr>
* doc/mipsy.texi: Add some comments on internal.
......
......@@ -95,7 +95,7 @@ namespace inst
type_map = {
"register": "Register&",
"immediate": "Exp&",
"label": "Label&",
"label": "Exp&",
"address": "Register&"
}
......
......@@ -30,7 +30,7 @@ def header(inst):
type_map = {
"register": "Register&",
"immediate": "Exp&",
"label": "Label",
"label": "Exp&",
"address": "Register&"
}
......@@ -88,10 +88,7 @@ namespace inst
print " return " + attribute.name + ";"
print " }"
if type != "int":
if type == "Label":
print " " + type + "&\t\tget_" + attribute.name + " ()"
else:
print " " + type + "\t\tget_" + attribute.name + " ()"
print " " + type + "\t\tget_" + attribute.name + " ()"
print " {"
print " return " + attribute.name + ";"
print " }"
......
......@@ -142,7 +142,7 @@ instruction
token_map = {
"register": "REGISTER",
"immediate": "exp",
"label": "LABEL",
"label": "exp",
"address": "REGISTER",
"hidden": ""
}
......
......@@ -80,14 +80,14 @@ namespace inst
// Greater
void
ProgramBuilder::add_bge(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_bge(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Sub(at, src1, src2));
_program->text_section ().add_inst(* new Bgez(at, dest));
}
void
ProgramBuilder::add_bge(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_bge(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -96,14 +96,14 @@ namespace inst
_program->text_section ().add_inst(* new Bgez(at, dest));
}
void
ProgramBuilder::add_bgeu(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_bgeu(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Subu(at, src1, src2));
_program->text_section ().add_inst(* new Bgez(at, dest));
}
void
ProgramBuilder::add_bgeu(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_bgeu(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -113,14 +113,14 @@ namespace inst
}
void
ProgramBuilder::add_bgt(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_bgt(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Sub(at, src1, src2));
_program->text_section ().add_inst(* new Bgtz(at, dest));
}
void
ProgramBuilder::add_bgt(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_bgt(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -129,14 +129,14 @@ namespace inst
_program->text_section ().add_inst(* new Bgtz(at, dest));
}
void
ProgramBuilder::add_bgtu(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_bgtu(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Subu(at, src1, src2));
_program->text_section ().add_inst(* new Bgtz(at, dest));
}
void
ProgramBuilder::add_bgtu(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_bgtu(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -147,14 +147,14 @@ namespace inst
// Lower
void
ProgramBuilder::add_ble(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_ble(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Sub(at, src1, src2));
_program->text_section ().add_inst(* new Blez(at, dest));
}
void
ProgramBuilder::add_ble(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_ble(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -163,14 +163,14 @@ namespace inst
_program->text_section ().add_inst(* new Blez(at, dest));
}
void
ProgramBuilder::add_bleu(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_bleu(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Subu(at, src1, src2));
_program->text_section ().add_inst(* new Blez(at, dest));
}
void
ProgramBuilder::add_bleu(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_bleu(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -180,14 +180,14 @@ namespace inst
}
void
ProgramBuilder::add_blt(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_blt(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Sub(at, src1, src2));
_program->text_section ().add_inst(* new Bltz(at, dest));
}
void
ProgramBuilder::add_blt(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_blt(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -196,14 +196,14 @@ namespace inst
_program->text_section ().add_inst(* new Bltz(at, dest));
}
void
ProgramBuilder::add_bltu(Register& src1, Register& src2, Label& dest)
ProgramBuilder::add_bltu(Register& src1, Register& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().add_inst(* new Subu(at, src1, src2));
_program->text_section ().add_inst(* new Bltz(at, dest));
}
void
ProgramBuilder::add_bltu(Register& src1, Exp& src2, Label& dest)
ProgramBuilder::add_bltu(Register& src1, Exp& src2, Exp& dest)
{
Register& at = * new Register(Register::at);
_program->text_section ().
......@@ -219,9 +219,10 @@ namespace inst
_program->text_section ().add_inst(* new Addi(dest, base, exp));
}
void
ProgramBuilder::add_la (Register& reg, Label& label)
ProgramBuilder::add_la (Register& reg, Exp& exp)
{
_program->text_section ().add_inst(* new Li(reg, * new LabelExp(label)));
// FIXME: alias
_program->text_section ().add_inst(* new Li(reg, exp));
}
} // namespace inst
......@@ -55,40 +55,32 @@ namespace inst
int _pc;
protected:
void resolve_label(Label& label)
{
if (label.get_offset() == 0)
{
if (! _program->text_section().has_label(label))
{
std::cerr << "Undefined text label: "<< label << std::endl;
exit_set(exit_solve);
}
else
label.set_offset(_program->text_section().get_offset(label) - _pc);
}
}
void resolve_exp(Exp& exp)
{
exp.accept(*this);
}
protected:
virtual void visit(IntExp& i)
{
i.set_immediate(i.get_integer());
}
virtual void visit(LabelExp& l)
{
if (! _program->data_section().has_label(l.get_label()))
{
std::cerr << "Undefined data label: "<< l.get_label()
<< std::endl;
exit_set(exit_solve);
}
if (! _program->text_section().has_label(l.get_label()))
{
std::cerr << "Undefined label: "<< l.get_label()
<< std::endl;
exit_set(exit_solve);
}
else
l.set_immediate(_program->text_section().
get_offset(l.get_label()) - _pc);
else
l.set_immediate(_program->data_section().get_offset(l.get_label()));
}
virtual void visit(OpExp& o)
{
o.get_left().accept(*this);
......@@ -113,6 +105,7 @@ namespace inst
break;
}
}
protected:
virtual void visit(Add&) {}
......@@ -172,47 +165,47 @@ namespace inst
protected:
virtual void visit(Beq& beq)
{
resolve_label(beq.get_label());
resolve_exp(beq.get_label());
}
virtual void visit(Bne& bne)
{
resolve_label(bne.get_label());
resolve_exp(bne.get_label());
}
virtual void visit(Bgez& bgez)
{
resolve_label(bgez.get_label());
resolve_exp(bgez.get_label());
}
virtual void visit(Bgezal& bgezal)
{
resolve_label(bgezal.get_label());
resolve_exp(bgezal.get_label());
}
virtual void visit(Bgtz& bgtz)
{
resolve_label(bgtz.get_label());
resolve_exp(bgtz.get_label());
}
virtual void visit(Blez& blez)
{
resolve_label(blez.get_label());
resolve_exp(blez.get_label());
}
virtual void visit(Bltz& bltz)
{
resolve_label(bltz.get_label());
resolve_exp(bltz.get_label());
}
virtual void visit(Bltzal& bltzal)
{
resolve_label(bltzal.get_label());
resolve_exp(bltzal.get_label());
}
virtual void visit(Jmp& jmp)
{
resolve_label(jmp.get_label());
resolve_exp(jmp.get_label());
}
virtual void visit(Jr&) {}
virtual void visit(Jal& jal)
{
resolve_label(jal.get_label());
resolve_exp(jal.get_label());
}
virtual void visit(Jalr&) {}
......
......@@ -329,7 +329,7 @@ namespace vm
void
VirtualMachine::visit(const inst::Jmp& jmp)
{
cpu.set_pc(cpu.get_pc() + jmp.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + jmp.get_label());
}
void
VirtualMachine::visit(const inst::Jr& jr)
......@@ -344,7 +344,7 @@ namespace vm
VirtualMachine::visit(const inst::Jal& jal)
{
cpu.set_ra(cpu.get_pc());
cpu.set_pc(cpu.get_ra() + jal.get_label().get_offset());
cpu.set_pc(cpu.get_ra() + jal.get_label());
// Open scope
begin_scope();
......@@ -366,13 +366,13 @@ namespace vm
VirtualMachine::visit(const inst::Beq& beq)
{
if (cpu.get_register(beq.get_src1 ()) == cpu.get_register(beq.get_src2 ()))
cpu.set_pc(cpu.get_pc() + beq.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + beq.get_label());
}
void
VirtualMachine::visit(const inst::Bne& bne)
{
if (cpu.get_register(bne.get_src1 ()) != cpu.get_register(bne.get_src2 ()))
cpu.set_pc(cpu.get_pc() + bne.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + bne.get_label());
}
// Greater
......@@ -380,7 +380,7 @@ namespace vm
VirtualMachine::visit(const inst::Bgez& bgez)
{
if (cpu.get_register(bgez.get_src ()) >= 0)
cpu.set_pc(cpu.get_pc() + bgez.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + bgez.get_label());
}
void
VirtualMachine::visit(const inst::Bgezal& bgezal)
......@@ -391,14 +391,14 @@ namespace vm
begin_scope();
cpu.set_ra(cpu.get_pc());
cpu.set_pc(cpu.get_pc() + bgezal.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + bgezal.get_label());
}
}
void
VirtualMachine::visit(const inst::Bgtz& bgtz)
{
if (cpu.get_register(bgtz.get_src ()) > 0)
cpu.set_pc(cpu.get_pc() + bgtz.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + bgtz.get_label());
}
// Lower
......@@ -406,13 +406,13 @@ namespace vm
VirtualMachine::visit(const inst::Blez& blez)
{
if (cpu.get_register(blez.get_src ()) <= 0)
cpu.set_pc(cpu.get_pc() + blez.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + blez.get_label());
}
void
VirtualMachine::visit(const inst::Bltz& bltz)
{
if (cpu.get_register(bltz.get_src ()) < 0)
cpu.set_pc(cpu.get_pc() + bltz.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + bltz.get_label());
}
void
VirtualMachine::visit(const inst::Bltzal& bltzal)
......@@ -423,7 +423,7 @@ namespace vm
begin_scope();
cpu.set_ra(cpu.get_pc());
cpu.set_pc(cpu.get_pc() + bltzal.get_label().get_offset());
cpu.set_pc(cpu.get_pc() + bltzal.get_label());
}
}
......
Supports Markdown
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