diff options
author | Tom Stellard <tstellar@gmail.com> | 2010-12-16 02:56:36 -0800 |
---|---|---|
committer | Tom Stellard <tstellar@gmail.com> | 2010-12-16 02:56:36 -0800 |
commit | 87da9b9035d27fb433a402d0e99d966b9bff1d78 (patch) | |
tree | 951f006d4f94e159879dd711d0925059ff29e522 | |
parent | b1d5428ecbbcde94071d642a3c1672f2f74a6ee1 (diff) |
Rename inst_loader -> program_loader plus some interface changes to
the instruction class.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | emulator.cpp | 14 | ||||
-rw-r--r-- | emulator.h | 8 | ||||
-rw-r--r-- | inst_loader.cpp | 4 | ||||
-rw-r--r-- | inst_loader.h | 12 | ||||
-rw-r--r-- | instruction.cpp | 30 | ||||
-rw-r--r-- | instruction.h | 17 | ||||
-rw-r--r-- | program.cpp | 7 | ||||
-rw-r--r-- | program.h | 20 | ||||
-rw-r--r-- | program_loader.cpp | 4 | ||||
-rw-r--r-- | program_loader.h | 14 | ||||
-rw-r--r-- | register_address.h | 1 | ||||
-rw-r--r-- | test_loader.cpp | 16 | ||||
-rw-r--r-- | test_loader.h | 10 | ||||
-rw-r--r-- | value.cpp | 7 | ||||
-rw-r--r-- | value.h | 1 |
16 files changed, 123 insertions, 44 deletions
@@ -3,7 +3,7 @@ DEPS := $(OBJS:.o=.d) -include $(DEPS) -debugger: main.o evaluator.o emulator.o value.o instruction.o register_address.o test_loader.o +debugger: main.o evaluator.o emulator.o value.o instruction.o register_address.o test_loader.o program.o g++ -o $@ $^ %.o: %.cpp diff --git a/emulator.cpp b/emulator.cpp index 18cec8a..140b49e 100644 --- a/emulator.cpp +++ b/emulator.cpp @@ -4,12 +4,13 @@ #include "emulator.h" -#include "inst_loader.h" +#include "program_loader.h" #include "instruction.h" +#include "program.h" #include "register_address.h" emulator::emulator( - inst_loader * loader, + program_loader * loader, int num_temp_regs, int num_const_regs, int num_out_regs) @@ -23,9 +24,14 @@ emulator::emulator( void emulator::run() { - std::vector<instruction *> * insts = m_loader->load(); + program * p = m_loader->load(); + if (!p) { + std::cerr << "Could not load program\n"; + return; + } std::vector<instruction *>::iterator it; - for(it = insts->begin(); it < insts->end(); ++it) { + for(it = p->m_instructions.begin(); + it < p->m_instructions.end(); ++it) { (*it)->execute(*this); } } @@ -6,12 +6,12 @@ #include "value.h" -class inst_loader; +class program_loader; class emulator { public: emulator( - inst_loader * loader, + program_loader * loader, int num_temp_regs, int num_const_regs, int num_out_regs); @@ -30,10 +30,12 @@ private: unsigned int index, enum swizzle swz); - inst_loader * m_loader; + program_loader * m_loader; std::vector<value *> m_temp_regs; std::vector<float_value *> m_const_regs; std::vector<value *> m_out_regs; + std::vector<value *> m_private_regs; + std::vector<float_value*> m_imediate_regs; }; #endif //EMULATOR_H diff --git a/inst_loader.cpp b/inst_loader.cpp deleted file mode 100644 index 49c95d9..0000000 --- a/inst_loader.cpp +++ /dev/null @@ -1,4 +0,0 @@ - - -#include "inst_loader.h" - diff --git a/inst_loader.h b/inst_loader.h deleted file mode 100644 index c668a7e..0000000 --- a/inst_loader.h +++ /dev/null @@ -1,12 +0,0 @@ - -#ifndef INST_LOADER_H_ -#define INST_LOADER_H_ - -#include "instruction.h" - -class inst_loader { -public: - virtual std::vector<instruction *> * load() = 0; -}; - -#endif //INST_LOADER_H_ diff --git a/instruction.cpp b/instruction.cpp index b4bf727..2b67c8e 100644 --- a/instruction.cpp +++ b/instruction.cpp @@ -1,3 +1,5 @@ +#include <assert.h> + #include "instruction.h" #include "emulator.h" @@ -5,20 +7,34 @@ instruction::instruction( const char * name, evaluator * evaluator, - std::vector<register_address> dst) : + std::vector<register_address> dst, + unsigned int src_reg_count) : m_name(name), m_evaluator(evaluator), - m_dst(dst) + m_dst(dst), + m_src_reg_count(src_reg_count) { } +void instruction::add_src_reg(std::vector<register_address> src_reg) +{ + assert(m_src_regs.size() < m_src_reg_count); + m_src_regs.push_back(src_reg); +} + add_instruction::add_instruction( std::vector<register_address> dst, std::vector<register_address> src0, std::vector<register_address> src1) : - instruction("ADD", new add_evaluator(), dst), - m_src0(src0), - m_src1(src1) + /*XXX: Can we call our own constructor here? */ + instruction("ADD", new add_evaluator(), dst, 2) +{ + m_src_regs.push_back(src0); + m_src_regs.push_back(src1); +} + +add_instruction::add_instruction(std::vector<register_address> dst) : + instruction("ADD", new add_evaluator(), dst, 2) { } void @@ -27,8 +43,8 @@ add_instruction::execute(emulator & emulator) unsigned int i; for (i = 0; i < m_dst.size(); i++) { tree_value * val = new tree_value(this->m_evaluator, - emulator.get_value(m_src0[i]), - emulator.get_value(m_src1[i])); + emulator.get_value(m_src_regs[0][i]), + emulator.get_value(m_src_regs[1][i])); emulator.set_value(m_dst[i], val); } } diff --git a/instruction.h b/instruction.h index c1222be..f17530a 100644 --- a/instruction.h +++ b/instruction.h @@ -12,19 +12,30 @@ class emulator; class instruction { public: - instruction(const char * name, evaluator * evaluator, std::vector<register_address> dst); + instruction( + const char * name, + evaluator * evaluator, + std::vector<register_address> dst, + unsigned int src_reg_count); virtual void execute(emulator & emulator) = 0; + virtual void add_src_reg(std::vector<register_address> src_reg); protected: const char * m_name; evaluator * m_evaluator; std::vector<register_address> m_dst; + std::vector<std::vector<register_address> > m_src_regs; + unsigned int m_src_reg_count; }; class add_instruction : public instruction { public: - add_instruction(std::vector<register_address> dst, std::vector<register_address>, - std::vector<register_address> src1); + add_instruction( + std::vector<register_address> dst, + std::vector<register_address> src0, + std::vector<register_address> src1); + + add_instruction(std::vector<register_address> dst); void execute(emulator & emulator); private: std::vector<register_address> m_src0; diff --git a/program.cpp b/program.cpp new file mode 100644 index 0000000..e6d7ca7 --- /dev/null +++ b/program.cpp @@ -0,0 +1,7 @@ +#include "program.h" + +void +program::add_instruction(instruction * inst) +{ + m_instructions.push_back(inst); +} diff --git a/program.h b/program.h new file mode 100644 index 0000000..fbee082 --- /dev/null +++ b/program.h @@ -0,0 +1,20 @@ +#ifndef PROGRAM_H_ +#define PROGRAM_H_ + +#include <vector> + +#include "register_address.h" + +class float_value; +class instruction; + +class program { +public: + register_address add_immediate(float value); + void add_instruction(instruction * inst); + + std::vector<instruction *> m_instructions; + std::vector<float_value *> m_immediate_regs; +}; + +#endif //PROGRAM_H_ diff --git a/program_loader.cpp b/program_loader.cpp new file mode 100644 index 0000000..844886c --- /dev/null +++ b/program_loader.cpp @@ -0,0 +1,4 @@ + + +#include "program_loader.h" + diff --git a/program_loader.h b/program_loader.h new file mode 100644 index 0000000..f3e2ceb --- /dev/null +++ b/program_loader.h @@ -0,0 +1,14 @@ + +#ifndef PROGRAM_LOADER_H_ +#define PROGRAM_LOADER_H_ + +class program; + +class program_loader { +public: + virtual program * load() = 0; +protected: + program * m_program; +}; + +#endif //PROGRAM_LOADER_H_ diff --git a/register_address.h b/register_address.h index c6f4cd0..293cfda 100644 --- a/register_address.h +++ b/register_address.h @@ -1,3 +1,4 @@ +#include <string> #ifndef REGISTER_ADDRESS_H_ #define REGISTER_ADDRESS_H_ diff --git a/test_loader.cpp b/test_loader.cpp index c0fce86..962a1c5 100644 --- a/test_loader.cpp +++ b/test_loader.cpp @@ -3,6 +3,8 @@ #include "test_loader.h" +#include "instruction.h" +#include "program.h" #include "register_address.h" std::vector<enum swizzle> @@ -29,27 +31,27 @@ test_loader::make_register( return new_register; } -std::vector<instruction *> * +program * test_loader::load() { - std::vector<instruction *> * instructions = new std::vector<instruction *>(); - - instructions->push_back(new add_instruction( + //XXX: This should go in parent constructor. + m_program = new program(); + m_program->add_instruction(new add_instruction( make_register(REGISTER_TYPE_TEMP, 0, XYZ()), make_register(REGISTER_TYPE_CONST, 0, XYZ()), make_register(REGISTER_TYPE_CONST, 0, XYZ()))); - instructions->push_back(new add_instruction( + m_program->add_instruction(new add_instruction( make_register(REGISTER_TYPE_TEMP, 0, XYZ()), make_register(REGISTER_TYPE_TEMP, 0, XYZ()), make_register(REGISTER_TYPE_CONST, 0, XYZ()))); - instructions->push_back(new add_instruction( + m_program->add_instruction(new add_instruction( make_register(REGISTER_TYPE_OUT, 0, XYZ()), make_register(REGISTER_TYPE_TEMP, 0, XYZ()), make_register(REGISTER_TYPE_TEMP, 0, XYZ()))); - return instructions; + return m_program; } diff --git a/test_loader.h b/test_loader.h index 95e7b76..d8d135c 100644 --- a/test_loader.h +++ b/test_loader.h @@ -4,11 +4,15 @@ #include <vector> -#include "inst_loader.h" +#include "register_address.h" +#include "program_loader.h" -class test_loader : public inst_loader { +class program; + + +class test_loader : public program_loader { public: - std::vector<instruction *> * load(); + program * load(); private: std::vector<enum swizzle> XYZ(); std::vector<register_address> make_register( @@ -55,6 +55,13 @@ float_value::float_value() : m_has_value(false) { } +float_value::float_value(float init_value) : + /* XXX: Can we call the other constructor here? */ + value(VALUE_TYPE_FLOAT), + m_has_value(true), + m_value(init_value) + { } + value * float_value::simplify() { @@ -43,6 +43,7 @@ private: class float_value : public value { public: float_value(); + float_value(float init_value); value * simplify(); value * clone(); std::string to_string(); |