cmake-module.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * YASM module loader
  3. *
  4. * Copyright (C) 2004-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <libyasm.h>
  29. typedef struct loaded_module {
  30. const char *keyword; /* module keyword */
  31. void *data; /* associated data */
  32. } loaded_module;
  33. static HAMT *loaded_modules[] = {NULL, NULL, NULL, NULL, NULL, NULL};
  34. static void
  35. load_module_destroy(/*@only@*/ void *data)
  36. {
  37. /* do nothing */
  38. }
  39. void *
  40. yasm_load_module(yasm_module_type type, const char *keyword)
  41. {
  42. if (!loaded_modules[type])
  43. return NULL;
  44. return HAMT_search(loaded_modules[type], keyword);
  45. }
  46. void
  47. yasm_register_module(yasm_module_type type, const char *keyword, void *data)
  48. {
  49. int replace = 1;
  50. assert(type < sizeof(loaded_modules));
  51. if (!loaded_modules[type])
  52. loaded_modules[type] = HAMT_create(0, yasm_internal_error_);
  53. HAMT_insert(loaded_modules[type], keyword, data, &replace,
  54. load_module_destroy);
  55. }
  56. typedef struct {
  57. yasm_module_type type;
  58. void (*printfunc) (const char *name, const char *keyword);
  59. } list_one_data;
  60. static int
  61. yasm_list_one_module(void *node, void *d)
  62. {
  63. list_one_data *data = (list_one_data *)d;
  64. yasm_arch_module *arch;
  65. yasm_dbgfmt_module *dbgfmt;
  66. yasm_objfmt_module *objfmt;
  67. yasm_listfmt_module *listfmt;
  68. yasm_parser_module *parser;
  69. yasm_preproc_module *preproc;
  70. switch (data->type) {
  71. case YASM_MODULE_ARCH:
  72. arch = node;
  73. data->printfunc(arch->name, arch->keyword);
  74. break;
  75. case YASM_MODULE_DBGFMT:
  76. dbgfmt = node;
  77. data->printfunc(dbgfmt->name, dbgfmt->keyword);
  78. break;
  79. case YASM_MODULE_OBJFMT:
  80. objfmt = node;
  81. data->printfunc(objfmt->name, objfmt->keyword);
  82. break;
  83. case YASM_MODULE_LISTFMT:
  84. listfmt = node;
  85. data->printfunc(listfmt->name, listfmt->keyword);
  86. break;
  87. case YASM_MODULE_PARSER:
  88. parser = node;
  89. data->printfunc(parser->name, parser->keyword);
  90. break;
  91. case YASM_MODULE_PREPROC:
  92. preproc = node;
  93. data->printfunc(preproc->name, preproc->keyword);
  94. break;
  95. }
  96. return 0;
  97. }
  98. void
  99. yasm_list_modules(yasm_module_type type,
  100. void (*printfunc) (const char *name, const char *keyword))
  101. {
  102. list_one_data data;
  103. /* Go through available list, and try to load each one */
  104. if (!loaded_modules[type])
  105. return;
  106. data.type = type;
  107. data.printfunc = printfunc;
  108. HAMT_traverse(loaded_modules[type], &data, yasm_list_one_module);
  109. }