TUM-LRR / era-gp-sim

ERA-Großpraktikum Simulatorentwicklung
GNU General Public License v3.0
16 stars 4 forks source link

Implement class for assembling instructions into bit strings #37

Closed yuriyarabskyy closed 7 years ago

goldsborough commented 8 years ago

source/arch/riscv/instruction-node.cpp:

  1. Please use more descriptive variable names. Variables are like children and you would not call your child i. This refers to:
    • s/immToI/immediateToIFormat
    • s/vec/vector
  2. Use std::size_t instead of int for for-loop variables here.
  3. About your str2int: I'm not sure you know what you're doing here, but your str2int is the djb2 string hash function. There are multiple things wrong about this:
    • I see zero comments about why/what you are doing. I just happen to know this hash function and understand that you want to hash the strings to switch-case, but I think this is not common knowledge, so commenting is quite necessary here.
    • If you want to hash strings, you should use std::hash<std::string>{}(string).
    • Of course, you could also just do if-else and ==.
    • Since performance is not relevant here, the best solution would be:
      1. Use if-else and refactor the whole thing into a new function (which you should have done anyway).
      2. Store a hashmap from names to functions and let it do the hashing yourself.
    • Please realize that anytime you are using the C api of things in C++ (c_str()) you are probably doing something wrong! You should have stopped at that point.
  4. Why are there no comments anywhere? I'm not supposed to have to sit 5 minutes in front of your code and try to understand what it's doing.
  5. The use of auto is questionable. For example, you write InstructionKey instructionKey = _instructionInformation.getKey(). Here, I know the result will be an instructionKey, so it's ok to use auto. But then you write auto boolResult = assembler(instructionKey, args);. Here, I have absolutely no idea what assembler returns and boolResult only hints at it, so I have to waste time looking up the interface, whereas std::vector<bool> result = ... would make more sense, since I see that it is a std::vector<bool>, and the result :blush:
  6. There is no need to use at if you don't explicitly want bounds checking. You should use [], and assertions if you want to check bounds.
  7. Do not name your children k.
  8. Also, a note on refactoring. A function is supposed to do one thing and one thing only

era-gp-sim/source/arch/riscv/formats.cpp:

era-gp-sim/include/arch/common/register-node.hpp:

era-gp-sim/source/common/utility.hpp: There are quite a few things wrong with convertToBin:

The code has very many issues and is not ready. Main things to look into:

  1. Document your functions properly, like everyone else.
  2. Use comments to make it clear to other human beings what you are trying to achieve.
  3. Try to keep functions under 20-30 lines. If you are writing more than that many lines in your function, you're probably doing more than one thing and you could refactor it into a new function.
  4. Your variable names should be or consist of words that can be found in the Oxford English Dictionary. Otherwise I will write a script that downloads the Oxford English Dictionary and tests your code against it.
  5. Do not, ever, in the world, while you are alive, while I'm alive, use C functions in C++.

Thanks and good luck. Take this potato on your journey: 🍠