mercredi 1 novembre 2017

Stringify template type for inline assembler

I am looking for a way to automate gcc inline assembly calls through template functions.
For example I have the following dummy function to store a value into a pointer. For now I specialize the template function for different types. Every time something changes in the code, I need to change it for every specialization.

template <typename T>
void store_ptr(T *location, T value);

template <>
void store_ptr<char>(char *location, char value) {
  __asm__ __volatile__(
      "strb %1, [%0]\n\t"
      : "+r" (location)
      : "r" (value)
      : "memory"
  );
}


template <>
void store_ptr<short>(short *location, short value) {
  __asm__ __volatile__(
      "strh %1, [%0]\n\t"
      : "+r" (location)
      : "r" (value)
      : "memory"
  );
}

It would be nice if the template could stringify the instruction appendix ("b", "h" ...) depending on the template type.

template <typename T>
void store_ptr<T>(T *location, T value) {
  __asm__ __volatile__(
     "str" stringify_template_type(T) " %1, [%0]\n\t"
     : "+r" (location)
     : "r" (value)
     : "memory"
     );
}

Is there a (simple) way of achieving this?

Aucun commentaire:

Enregistrer un commentaire