]> granicus.if.org Git - yasm/commitdiff
Allow registering of new modules at runtime.
authorPeter Johnson <peter@tortall.net>
Wed, 30 Apr 2008 04:40:29 +0000 (04:40 -0000)
committerPeter Johnson <peter@tortall.net>
Wed, 30 Apr 2008 04:40:29 +0000 (04:40 -0000)
svn path=/trunk/yasm/; revision=2080

libyasm/module.h
libyasm/module.in

index f930800f8d9769a13cb564f37dd9251a99664953..b6b4f60b791ab160eb4117e1feb7c76bd9939335 100644 (file)
@@ -69,4 +69,7 @@ void yasm_list_modules
 #define yasm_list_preproc(func)         \
     yasm_list_modules(YASM_MODULE_PREPROC, func)
 
+void yasm_register_module(yasm_module_type type, const char *keyword,
+                          void *data);
+
 #endif
index 2a3fd571924152a0d680598a4364386fba2d58d2..ff1e399b146f1d44f32c90220da467d4e287592f 100644 (file)
@@ -73,14 +73,33 @@ static struct {
     {preproc_modules, sizeof(preproc_modules)/sizeof(module)},
 };
 
+typedef struct loaded_module {
+    yasm_module_type type;          /* module type */
+    const char *keyword;            /* module keyword */
+    void *data;                     /* associated data */
+} loaded_module;
+
+static loaded_module *loaded_modules = NULL;
+static size_t num_loaded_modules = 0;
+
 void *
 yasm_load_module(yasm_module_type type, const char *keyword)
 {
     size_t i;
-    module *modules = module_types[type].m;
-    size_t n = module_types[type].n;
+    module *modules;
+    size_t n;
 
-    /* Look for the module/symbol. */
+    /* Look for the module/symbol, first in loaded modules */
+    if (loaded_modules) {
+        for (i=0; i<num_loaded_modules; i++) {
+            if (loaded_modules[i].type == type &&
+                yasm__strcasecmp(loaded_modules[i].keyword, keyword) == 0)
+                return loaded_modules[i].data;
+        }
+    }
+
+    modules = module_types[type].m;
+    n = module_types[type].n;
     for (i=0; i<n; i++) {
         if (yasm__strcasecmp(modules[i].keyword, keyword) == 0)
             return modules[i].data;
@@ -90,12 +109,21 @@ yasm_load_module(yasm_module_type type, const char *keyword)
 }
 
 void
-yasm_list_modules(yasm_module_type type,
-                  void (*printfunc) (const char *name, const char *keyword))
+yasm_register_module(yasm_module_type type, const char *keyword, void *data)
+{
+    loaded_modules =
+        yasm_xrealloc(loaded_modules,
+                      (num_loaded_modules+1)*sizeof(loaded_module));
+    loaded_modules[num_loaded_modules].type = type;
+    loaded_modules[num_loaded_modules].keyword = keyword;
+    loaded_modules[num_loaded_modules].data = data;
+    num_loaded_modules++;
+}
+
+static void
+yasm_list_one_module(yasm_module_type type, void *data,
+                     void (*printfunc) (const char *name, const char *keyword))
 {
-    size_t i;
-    module *modules = module_types[type].m;
-    size_t n = module_types[type].n;
     yasm_arch_module *arch;
     yasm_dbgfmt_module *dbgfmt;
     yasm_objfmt_module *objfmt;
@@ -103,33 +131,50 @@ yasm_list_modules(yasm_module_type type,
     yasm_parser_module *parser;
     yasm_preproc_module *preproc;
 
+    switch (type) {
+        case YASM_MODULE_ARCH:
+            arch = data;
+            printfunc(arch->name, arch->keyword);
+            break;
+        case YASM_MODULE_DBGFMT:
+            dbgfmt = data;
+            printfunc(dbgfmt->name, dbgfmt->keyword);
+            break;
+        case YASM_MODULE_OBJFMT:
+            objfmt = data;
+            printfunc(objfmt->name, objfmt->keyword);
+            break;
+        case YASM_MODULE_LISTFMT:
+            listfmt = data;
+            printfunc(listfmt->name, listfmt->keyword);
+            break;
+        case YASM_MODULE_PARSER:
+            parser = data;
+            printfunc(parser->name, parser->keyword);
+            break;
+        case YASM_MODULE_PREPROC:
+            preproc = data;
+            printfunc(preproc->name, preproc->keyword);
+            break;
+    }
+}
+
+void
+yasm_list_modules(yasm_module_type type,
+                  void (*printfunc) (const char *name, const char *keyword))
+{
+    size_t i;
+    module *modules;
+    size_t n;;
+
     /* Go through available list, and try to load each one */
-    for (i=0; i<n; i++) {
-        switch (type) {
-            case YASM_MODULE_ARCH:
-                arch = modules[i].data;
-                printfunc(arch->name, arch->keyword);
-                break;
-            case YASM_MODULE_DBGFMT:
-                dbgfmt = modules[i].data;
-                printfunc(dbgfmt->name, dbgfmt->keyword);
-                break;
-            case YASM_MODULE_OBJFMT:
-                objfmt = modules[i].data;
-                printfunc(objfmt->name, objfmt->keyword);
-                break;
-            case YASM_MODULE_LISTFMT:
-                listfmt = modules[i].data;
-                printfunc(listfmt->name, listfmt->keyword);
-                break;
-            case YASM_MODULE_PARSER:
-                parser = modules[i].data;
-                printfunc(parser->name, parser->keyword);
-                break;
-            case YASM_MODULE_PREPROC:
-                preproc = modules[i].data;
-                printfunc(preproc->name, preproc->keyword);
-                break;
-        }
+    if (loaded_modules) {
+        for (i=0; i<num_loaded_modules; i++)
+            yasm_list_one_module(type, loaded_modules[i].data, printfunc);
     }
+
+    modules = module_types[type].m;
+    n = module_types[type].n;
+    for (i=0; i<n; i++)
+        yasm_list_one_module(type, modules[i].data, printfunc);
 }