tablegen.txt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Writing a table generator
  2. This documentation is preliminary.
  3. Parts of the API are not good and should be changed.
  4. Basic concepts
  5. A table generator consists of two files, *_tablegen.c and *_tablegen.h.
  6. The .h file will provide the variable declarations and initialization
  7. code for the tables, the .c calls the initialization code and then prints
  8. the tables as a header file using the tableprint.h helpers.
  9. Both of these files will be compiled for the host system, so to avoid
  10. breakage with cross-compilation neither of them may include, directly
  11. or indirectly, config.h or avconfig.h.
  12. This means that e.g. libavutil/mathematics.h is ok but libavutil/libm.h is not.
  13. Due to this, the .c file or Makefile may have to provide additional defines
  14. or stubs, though if possible this should be avoided.
  15. In particular, CONFIG_HARDCODED_TABLES should always be defined to 0.
  16. The .c file
  17. This file should include the *_tablegen.h and tableprint.h files and
  18. anything else it needs as long as it does not depend on config.h or
  19. avconfig.h.
  20. In addition to that it must contain a main() function which initializes
  21. all tables by calling the init functions from the .h file and then prints
  22. them.
  23. The printing code typically looks like this:
  24. write_fileheader();
  25. printf("static const uint8_t my_array[100] = {\n");
  26. write_uint8_t_array(my_array, 100);
  27. printf("};\n");
  28. This is the more generic form, in case you need to do something special.
  29. Usually you should instead use the short form:
  30. write_fileheader();
  31. WRITE_ARRAY("static const", uint8_t, my_array);
  32. write_fileheader() adds some minor things like a "this is a generated file"
  33. comment and some standard includes.
  34. tablegen.h defines some write functions for one- and two-dimensional arrays
  35. for standard types - they print only the "core" parts so they are easier
  36. to reuse for multi-dimensional arrays so the outermost {} must be printed
  37. separately.
  38. If there's no standard function for printing the type you need, the
  39. WRITE_1D_FUNC_ARGV macro is a very quick way to create one.
  40. See libavcodec/dv_tablegen.c for an example.
  41. The .h file
  42. This file should contain:
  43. - one or more initialization functions
  44. - the table variable declarations
  45. If CONFIG_HARDCODED_TABLES is set, the initialization functions should
  46. not do anything, and instead of the variable declarations the
  47. generated *_tables.h file should be included.
  48. Since that will be generated in the build directory, the path must be
  49. included, i.e.
  50. #include "libavcodec/example_tables.h"
  51. not
  52. #include "example_tables.h"
  53. Makefile changes
  54. To make the automatic table creation work, you must manually declare the
  55. new dependency.
  56. For this add a line similar to this:
  57. $(SUBDIR)example.o: $(SUBDIR)example_tables.h
  58. under the "ifdef CONFIG_HARDCODED_TABLES" section in the Makefile.