New output mode to generate size (in bytes) of some compound types
authorMatthias Braun <matthias.braun@kit.edu>
Thu, 26 Jul 2012 13:51:30 +0000 (15:51 +0200)
committerMatthias Braun <matthias.braun@kit.edu>
Thu, 26 Jul 2012 14:34:12 +0000 (16:34 +0200)
This will be used by the X10 native code integration.

Makefile
main.c
wrappergen/write_compoundsizes.c [new file with mode: 0644]
wrappergen/write_compoundsizes.h [new file with mode: 0644]

index 3e8ee4a..6387123 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -61,7 +61,8 @@ SOURCES := \
        warning.c \
        walk.c \
        wrappergen/write_fluffy.c \
-       wrappergen/write_jna.c
+       wrappergen/write_jna.c \
+       wrappergen/write_compoundsizes.c
 
 OBJECTS = $(SOURCES:%.c=$(BUILDDIR)/%.o)
 DEPENDS = $(OBJECTS:%.o=%.d)
diff --git a/main.c b/main.c
index 675247e..ebf4500 100644 (file)
--- a/main.c
+++ b/main.c
@@ -77,6 +77,7 @@
 #include "adt/array.h"
 #include "wrappergen/write_fluffy.h"
 #include "wrappergen/write_jna.h"
+#include "wrappergen/write_compoundsizes.h"
 #include "revision.h"
 #include "warning.h"
 #include "help.h"
@@ -583,7 +584,8 @@ typedef enum compile_mode_t {
        CompileAssembleLink,
        PrintAst,
        PrintFluffy,
-       PrintJna
+       PrintJna,
+       PrintCompoundSizes,
 } compile_mode_t;
 
 static void usage(const char *argv0)
@@ -1290,6 +1292,9 @@ again:
                        } else if (mode == PrintJna) {
                                write_jna_decls(out, unit->ast);
                                break;
+                       } else if (mode == PrintCompoundSizes) {
+                               write_compoundsizes(out, unit->ast);
+                               break;
                        }
 
                        /* build the firm graph */
@@ -1916,6 +1921,8 @@ int main(int argc, char **argv)
                                        print_parenthesis = true;
                                } else if (streq(option, "print-fluffy")) {
                                        mode = PrintFluffy;
+                               } else if (streq(option, "print-compound-sizes")) {
+                                       mode = PrintCompoundSizes;
                                } else if (streq(option, "print-jna")) {
                                        mode = PrintJna;
                                } else if (streq(option, "jna-limit")) {
@@ -2106,6 +2113,7 @@ int main(int argc, char **argv)
                case PrintAst:
                case PrintFluffy:
                case PrintJna:
+               case PrintCompoundSizes:
                case PreprocessOnly:
                case ParseOnly:
                        outname = "-";
diff --git a/wrappergen/write_compoundsizes.c b/wrappergen/write_compoundsizes.c
new file mode 100644 (file)
index 0000000..4b30bdd
--- /dev/null
@@ -0,0 +1,39 @@
+#include "type_t.h"
+#include "ast_t.h"
+#include "entity_t.h"
+#include "attribute_t.h"
+#include "write_compoundsizes.h"
+
+void write_compoundsizes(FILE *output, const translation_unit_t *unit)
+{
+       for (const entity_t *entity = unit->scope.entities;
+                entity != NULL; entity = entity->base.next) {
+               if (entity->kind != ENTITY_TYPEDEF)
+                       continue;
+
+               type_t *type = skip_typeref(entity->typedefe.type);
+               if (!is_type_compound(type))
+                       continue;
+
+               /* see if we have the required attributes */
+               const compound_t *compound = type->compound.compound;
+               bool        had_dllexport = false;
+               const char *string        = NULL;
+               for (const attribute_t *attrib = compound->attributes;
+                    attrib != NULL; attrib = attrib->next) {
+                   if (attrib->kind == ATTRIBUTE_GNU_DLLEXPORT)
+                               had_dllexport = true;
+                       if (attrib->kind == ATTRIBUTE_GNU_DEPRECATED) {
+                               const attribute_argument_t *argument = attrib->a.arguments;
+                               assert(argument != NULL && argument->kind == ATTRIBUTE_ARGUMENT_EXPRESSION);
+                               const expression_t *expr = argument->v.expression;
+                               assert(expr->kind == EXPR_STRING_LITERAL);
+                               string = expr->string_literal.value.begin;
+                       }
+               }
+
+               if (had_dllexport && string != NULL) {
+                       fprintf(output, "%s %u\n", string, get_type_size(type));
+               }
+       }
+}
diff --git a/wrappergen/write_compoundsizes.h b/wrappergen/write_compoundsizes.h
new file mode 100644 (file)
index 0000000..b190012
--- /dev/null
@@ -0,0 +1,3 @@
+#include "ast.h"
+
+void write_compoundsizes(FILE *out, const translation_unit_t *unit);