-
Notifications
You must be signed in to change notification settings - Fork 137
Cpp guidelines
We try to return all objects from functions using structures, or, at least pairs and tuples:
int value;
bool flag = foo( argument, &value); // worse
FooResult result = foo( argument); // better
auto result = foo( argument); // even better
std::pair<bool, int> result = foo( argument); // acceptable, but not the best
auto[flag, value] = foo( argument); // might be nice
If an object should be mutated inside a function, we use C raw pointer instead of C++ reference
void process_instruction(Instr* instr); // preferrable
process_instruction(&fetched_instr); // '&' emphasizes object is mutated
void process_instruction(Instr& instr); // avoid
process_instruction(fetched_instr); // is fetched_instr mutated?We do not use predefined C++ types as they have badly defined sizes. Instead, we use <infra/types.h> types:
int8, int16, int32, int64uint8, uint16, uint32, uint64-
Addr— to represent guest machine address and derived types like address masks, BPU targets etc.
For the most of cases you have to use uint32. Smaller types must be used only in two cases:
- when they are explicitly required — like keeping an array of bytes or performing sign extension.
- when you have to keep the data really dense — so far there are no such needs.
To avoid namespace pollution and possible interference between libraries (e.g. Boost and STL) constructions like using namespace std are strictly prohibited.
We use C++11 type deduction wherever possible:
auto inst = rp_decode_2_memory->read( cycle); // good
FuncInstr inst = rp_decode_2_memory->read( cycle); // worseWe do not use 'new'/'delete'. Instead, RAII memory management classes should be used, see our manual for smart pointers.
void avoid()
{
auto pointer = new MyClass(arg1, arg2);
// ...
delete pointer;
}
void prefer()
{
auto pointer = std::make_unique<MyClass>(arg1, arg2);
// ...
}We distinguish between internal errors and external errors. Internal errors are errors inside simulator like passing -1 way to cache read function. These errors can be handled by C-style assert() and warning() macros
External errors are errors caused by incorrect user input, like unsupported instruction, invalid cache size setting or ill-formed trace. They must be reported via C++ exceptions. Exception classes must be inherited from std::runtime_error
MIPT-V / MIPT-MIPS — Cycle-accurate pre-silicon simulation.