[Swfdec-commits] 4 commits - vivified/code

Pekka Lampila medar at kemper.freedesktop.org
Thu Jun 26 01:49:53 PDT 2008


 vivified/code/rewrite.c                      |   76 ++++++++++++++++++++++++++-
 vivified/code/test/compiler/Makefile.am      |    2 
 vivified/code/test/compiler/asm_if.as        |    4 +
 vivified/code/test/compiler/asm_if.as.expect |    6 ++
 vivified/code/vivi_code_asm_code_default.h   |   14 ++++
 vivified/code/vivi_parser.c                  |   34 +++++++++++-
 6 files changed, 132 insertions(+), 4 deletions(-)

New commits:
commit 8a7063476d10cc2adc330c127ba8cb827fa065b6
Author: Pekka Lampila <pekka.lampila at iki.fi>
Date:   Mon Jun 16 17:11:10 2008 +0300

    Fix an issue with --sort-enumerate when file had more than one enumerate

diff --git a/vivified/code/rewrite.c b/vivified/code/rewrite.c
index 7714a88..1df0a7e 100644
--- a/vivified/code/rewrite.c
+++ b/vivified/code/rewrite.c
@@ -169,68 +169,68 @@ static void
 sort_enumerate (ViviCodeAssembler *assembler)
 {
   guint i, j;
-  ViviCodeStatement *statement;
-  ViviCodeCompiler *compiler;
-  ViviCodeAssembler *asm2;
-
-  statement = vivi_parse_string (
-      "asm {"
-      "  push 0;"
-      "  init_array;"
-      "  store 0;"
-      "  pop;"
-
-      "read_start:"
-      "  push_duplicate;"
-      "  push undefined;"
-      "  equals2;"
-      "  if read_end;"
-
-      "  push 1, reg 0, 'push';"
-      "  call_method;"
-      "  pop;"
-      "  jump read_start;"
-
-      "read_end:"
-      "  pop;"
-
-      "  push 0, reg 0, 'sort';"
-      "  call_method;"
-      "  pop;"
-
-      "  push undefined;"
-      "write_start:"
-      "  push 0, reg 0, 'length';"
-      "  get_member;"
-      "  equals2;"
-      "  if write_end;"
-
-      "  push 0, reg 0, 'pop';"
-      "  call_method;"
-      "  jump write_start;"
-
-      "write_end:"
-      "}"
-      );
-  g_assert (statement);
-  compiler = vivi_code_compiler_new (7); // FIXME: version
-  vivi_code_compiler_compile_statement (compiler, statement);
-  g_object_unref (statement);
-  asm2 = g_object_ref (vivi_code_compiler_get_assembler (compiler));
-  g_object_unref (compiler);
 
   for (i = 0; i < vivi_code_assembler_get_n_codes (assembler); i++) {
     ViviCodeAsm *code = vivi_code_assembler_get_code (assembler, i);
     if (VIVI_IS_CODE_ASM_ENUMERATE (code) ||
 	VIVI_IS_CODE_ASM_ENUMERATE2 (code)) {
+      ViviCodeStatement *statement;
+      ViviCodeCompiler *compiler;
+      ViviCodeAssembler *asm2;
+
+      // FIXME: compile once and use multiple times (problem is the labels)
+      statement = vivi_parse_string (
+	  "asm {"
+	  "  push 0;"
+	  "  init_array;"
+	  "  store 0;"
+	  "  pop;"
+
+	  "read_start:"
+	  "  push_duplicate;"
+	  "  push undefined;"
+	  "  equals2;"
+	  "  if read_end;"
+
+	  "  push 1, reg 0, 'push';"
+	  "  call_method;"
+	  "  pop;"
+	  "  jump read_start;"
+
+	  "read_end:"
+	  "  pop;"
+
+	  "  push 0, reg 0, 'sort';"
+	  "  call_method;"
+	  "  pop;"
+
+	  "  push undefined;"
+	  "write_start:"
+	  "  push 0, reg 0, 'length';"
+	  "  get_member;"
+	  "  equals2;"
+	  "  if write_end;"
+
+	  "  push 0, reg 0, 'pop';"
+	  "  call_method;"
+	  "  jump write_start;"
+
+	  "write_end:"
+	  "}"
+	  );
+      g_assert (statement);
+      compiler = vivi_code_compiler_new (7); // FIXME: version
+      vivi_code_compiler_compile_statement (compiler, statement);
+      g_object_unref (statement);
+      asm2 = g_object_ref (vivi_code_compiler_get_assembler (compiler));
+      g_object_unref (compiler);
       for (j = 0; j < vivi_code_assembler_get_n_codes (asm2); j++) {
 	vivi_code_assembler_insert_code (assembler, ++i,
 	    vivi_code_assembler_get_code (asm2, j));
       }
+      g_object_unref (asm2);
     }
   }
-
-  g_object_unref (asm2);
 }
 
 static void
commit d4dce60556990bf0ceb20d4fdd3201eaf349fdba
Author: Pekka Lampila <pekka.lampila at iki.fi>
Date:   Mon Jun 16 16:56:23 2008 +0300

    Implement --sort-enumerate (aka. -e) for vivi-rewrite

diff --git a/vivified/code/rewrite.c b/vivified/code/rewrite.c
index a837120..7714a88 100644
--- a/vivified/code/rewrite.c
+++ b/vivified/code/rewrite.c
@@ -40,6 +40,7 @@ typedef enum {
   REWRITE_TRACE_FUNCTION_NAME = (1 << 0),
   REWRITE_RANDOM              = (1 << 1),
   REWRITE_GETTERS	      = (1 << 2),
+  REWRITE_ENUMERATE           = (1 << 3),
 
   REWRITE_INIT		      =	(1 << 17)
 } RewriteOptions;
@@ -165,6 +166,74 @@ insert_function_trace (ViviCodeAssembler *assembler, const char *name)
 }
 
 static void
+sort_enumerate (ViviCodeAssembler *assembler)
+{
+  guint i, j;
+  ViviCodeStatement *statement;
+  ViviCodeCompiler *compiler;
+  ViviCodeAssembler *asm2;
+
+  statement = vivi_parse_string (
+      "asm {"
+      "  push 0;"
+      "  init_array;"
+      "  store 0;"
+      "  pop;"
+
+      "read_start:"
+      "  push_duplicate;"
+      "  push undefined;"
+      "  equals2;"
+      "  if read_end;"
+
+      "  push 1, reg 0, 'push';"
+      "  call_method;"
+      "  pop;"
+      "  jump read_start;"
+
+      "read_end:"
+      "  pop;"
+
+      "  push 0, reg 0, 'sort';"
+      "  call_method;"
+      "  pop;"
+
+      "  push undefined;"
+      "write_start:"
+      "  push 0, reg 0, 'length';"
+      "  get_member;"
+      "  equals2;"
+      "  if write_end;"
+
+      "  push 0, reg 0, 'pop';"
+      "  call_method;"
+      "  jump write_start;"
+
+      "write_end:"
+      "}"
+      );
+  g_assert (statement);
+  compiler = vivi_code_compiler_new (7); // FIXME: version
+  vivi_code_compiler_compile_statement (compiler, statement);
+  g_object_unref (statement);
+  asm2 = g_object_ref (vivi_code_compiler_get_assembler (compiler));
+  g_object_unref (compiler);
+
+  for (i = 0; i < vivi_code_assembler_get_n_codes (assembler); i++) {
+    ViviCodeAsm *code = vivi_code_assembler_get_code (assembler, i);
+    if (VIVI_IS_CODE_ASM_ENUMERATE (code) ||
+	VIVI_IS_CODE_ASM_ENUMERATE2 (code)) {
+      for (j = 0; j < vivi_code_assembler_get_n_codes (asm2); j++) {
+	vivi_code_assembler_insert_code (assembler, ++i,
+	    vivi_code_assembler_get_code (asm2, j));
+      }
+    }
+  }
+
+  g_object_unref (asm2);
+}
+
+static void
 replace_random (ViviCodeAssembler *assembler, guint init)
 {
   guint i = 0;
@@ -245,6 +314,8 @@ do_script (SwfdecBuffer *buffer, guint flags, const char *name, guint version)
     replace_random (assembler, flags & REWRITE_INIT);
   if (flags & REWRITE_GETTERS)
     rewrite_getters (assembler);
+  if (flags & REWRITE_ENUMERATE)
+    sort_enumerate (assembler);
 
   script = vivi_code_assembler_assemble_script (assembler, version, &error);
   g_object_unref (assembler);
@@ -402,11 +473,13 @@ main (int argc, char *argv[])
   gboolean trace_function_names = FALSE;
   gboolean random = FALSE;
   gboolean getters = FALSE;
+  gboolean enumerate = FALSE;
 
   GOptionEntry options[] = {
     { "getters", 'g', 0, G_OPTION_ARG_NONE, &getters, "trace all variable get operations", NULL },
     { "trace-function-names", 'n', 0, G_OPTION_ARG_NONE, &trace_function_names, "trace names of called functions", NULL },
     { "no-random", 'r', 0, G_OPTION_ARG_NONE, &random, "replace all random values with 0", NULL },
+    { "sort-enumerate", 'e', 0, G_OPTION_ARG_NONE, &enumerate, "sort all enumerate actions to have predictable order", NULL },
     { NULL }
   };
   GOptionContext *ctx;
@@ -444,7 +517,8 @@ main (int argc, char *argv[])
   buffer = process_buffer (buffer,
       (trace_function_names ? REWRITE_TRACE_FUNCTION_NAME : 0) |
       (random ? REWRITE_RANDOM : 0) |
-      (getters ? REWRITE_GETTERS : 0));
+      (getters ? REWRITE_GETTERS : 0) |
+      (enumerate ? REWRITE_ENUMERATE : 0));
   if (buffer == NULL) {
     g_printerr ("\"%s\": Broken Flash file\n", argv[1]);
     return 1;
diff --git a/vivified/code/vivi_code_asm_code_default.h b/vivified/code/vivi_code_asm_code_default.h
index c3ca285..48d8acb 100644
--- a/vivified/code/vivi_code_asm_code_default.h
+++ b/vivified/code/vivi_code_asm_code_default.h
@@ -57,6 +57,20 @@ ViviCodeAsm *		vivi_code_asm_ ## underscore_name ## _new	(void);
 #define VIVI_CODE_ASM_RANDOM_CLASS(klass)            (G_TYPE_CHECK_CLASS_CAST ((klass), VIVI_TYPE_CODE_ASM_RANDOM, ViviCodeAsmRandomClass))
 #define VIVI_CODE_ASM_RANDOM_GET_CLASS(obj)          (G_TYPE_INSTANCE_GET_CLASS ((obj), VIVI_TYPE_CODE_ASM_RANDOM, ViviCodeAsmRandomClass))
 
+#define VIVI_TYPE_CODE_ASM_ENUMERATE		     (vivi_code_asm_enumerate_get_type())
+#define VIVI_IS_CODE_ASM_ENUMERATE(obj)                 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VIVI_TYPE_CODE_ASM_ENUMERATE))
+#define VIVI_IS_CODE_ASM_ENUMERATE_CLASS(klass)         (G_TYPE_CHECK_CLASS_TYPE ((klass), VIVI_TYPE_CODE_ASM_ENUMERATE))
+#define VIVI_CODE_ASM_ENUMERATE(obj)                    (G_TYPE_CHECK_INSTANCE_CAST ((obj), VIVI_TYPE_CODE_ASM_ENUMERATE, ViviCodeAsmEnumerate))
+#define VIVI_CODE_ASM_ENUMERATE_CLASS(klass)            (G_TYPE_CHECK_CLASS_CAST ((klass), VIVI_TYPE_CODE_ASM_ENUMERATE, ViviCodeAsmEnumerateClass))
+#define VIVI_CODE_ASM_ENUMERATE_GET_CLASS(obj)          (G_TYPE_INSTANCE_GET_CLASS ((obj), VIVI_TYPE_CODE_ASM_ENUMERATE, ViviCodeAsmEnumerateClass))
+
+#define VIVI_TYPE_CODE_ASM_ENUMERATE2		     (vivi_code_asm_enumerate2_get_type())
+#define VIVI_IS_CODE_ASM_ENUMERATE2(obj)                 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VIVI_TYPE_CODE_ASM_ENUMERATE2))
+#define VIVI_IS_CODE_ASM_ENUMERATE2_CLASS(klass)         (G_TYPE_CHECK_CLASS_TYPE ((klass), VIVI_TYPE_CODE_ASM_ENUMERATE2))
+#define VIVI_CODE_ASM_ENUMERATE2(obj)                    (G_TYPE_CHECK_INSTANCE_CAST ((obj), VIVI_TYPE_CODE_ASM_ENUMERATE2, ViviCodeAsmEnumerate2))
+#define VIVI_CODE_ASM_ENUMERATE2_CLASS(klass)            (G_TYPE_CHECK_CLASS_CAST ((klass), VIVI_TYPE_CODE_ASM_ENUMERATE2, ViviCodeAsmEnumerate2Class))
+#define VIVI_CODE_ASM_ENUMERATE2_GET_CLASS(obj)          (G_TYPE_INSTANCE_GET_CLASS ((obj), VIVI_TYPE_CODE_ASM_ENUMERATE2, ViviCodeAsmEnumerate2Class))
+
 
 G_END_DECLS
 #endif
commit 9d9e1bb54af3f1c4b0b0039e67a9b5318a097ff7
Author: Pekka Lampila <pekka.lampila at iki.fi>
Date:   Mon Jun 16 15:21:40 2008 +0300

    vivi-compile: Add support for parsing if asm statements. Add a test

diff --git a/vivified/code/test/compiler/Makefile.am b/vivified/code/test/compiler/Makefile.am
index 6b0c476..16c3f79 100644
--- a/vivified/code/test/compiler/Makefile.am
+++ b/vivified/code/test/compiler/Makefile.am
@@ -28,6 +28,8 @@ EXTRA_DIST = \
 	asm_empty.as.expect \
 	asm_get_url2.as \
 	asm_get_url2.as.expect \
+	asm_if.as \
+	asm_if.as.expect \
 	asm_jump.as \
 	asm_jump.as.expect \
 	asm_label.as \
diff --git a/vivified/code/test/compiler/asm_if.as b/vivified/code/test/compiler/asm_if.as
new file mode 100644
index 0000000..6a2ffd7
--- /dev/null
+++ b/vivified/code/test/compiler/asm_if.as
@@ -0,0 +1,4 @@
+asm {
+  if hello
+hello:
+}
diff --git a/vivified/code/test/compiler/asm_if.as.expect b/vivified/code/test/compiler/asm_if.as.expect
new file mode 100644
index 0000000..d2c4086
--- /dev/null
+++ b/vivified/code/test/compiler/asm_if.as.expect
@@ -0,0 +1,6 @@
+asm {
+  if hello
+
+hello:
+  end
+}
diff --git a/vivified/code/vivi_parser.c b/vivified/code/vivi_parser.c
index 69c9aaf..1ef1144 100644
--- a/vivified/code/vivi_parser.c
+++ b/vivified/code/vivi_parser.c
@@ -31,6 +31,7 @@
 #include "vivi_code_and.h"
 #include "vivi_code_asm_code_default.h"
 #include "vivi_code_asm_get_url2.h"
+#include "vivi_code_asm_if.h"
 #include "vivi_code_asm_jump.h"
 #include "vivi_code_asm_pool.h"
 #include "vivi_code_asm_push.h"
@@ -1013,6 +1014,28 @@ parse_asm_store (ParseData *data)
 }
 
 static ViviCodeAsm *
+parse_asm_if (ParseData *data)
+{
+  ViviCodeAsm *code;
+  ViviCodeLabel *label;
+  const char *name;
+
+  name = parse_identifier_value (data);
+  label = vivi_parser_get_label (data, name);
+  if (label == NULL) {
+    label = VIVI_CODE_LABEL (vivi_code_label_new (name));
+    vivi_parser_add_waiting_label (data, label);
+  }
+
+  parse_automatic_semicolon (data);
+
+  code = vivi_code_asm_if_new (label);
+  g_object_unref (label);
+
+  return code;
+}
+
+static ViviCodeAsm *
 parse_asm_jump (ParseData *data)
 {
   ViviCodeAsm *code;
@@ -1299,6 +1322,7 @@ static const AsmStatement asm_statements[] = {
 #include "vivi_code_defaults.h"
 #undef DEFAULT_ASM
   { "get_url2", NULL, parse_asm_get_url2 },
+  { "if", NULL, parse_asm_if },
   { "jump", NULL, parse_asm_jump },
   { "pool", NULL, parse_asm_pool },
   { "push", NULL, parse_asm_push },
@@ -1337,6 +1361,8 @@ parse_asm_code (ParseData *data)
     identifier = g_strdup ("throw");
   } else if (try_parse_token (data, TOKEN_TRY)) {
     identifier = g_strdup ("try");
+  } else if (try_parse_token (data, TOKEN_IF)) {
+    identifier = g_strdup ("if");
   } else if (try_parse_token (data, TOKEN_IMPLEMENTS)) {
     identifier = g_strdup ("implements");
   } else if (try_parse_token (data, TOKEN_DELETE)) {
@@ -1419,6 +1445,7 @@ parse_asm_statement (ParseData *data)
     } while (peek_token (data, TOKEN_IDENTIFIER) ||
 	peek_token (data, TOKEN_THROW) ||
 	peek_token (data, TOKEN_TRY) ||
+	peek_token (data, TOKEN_IF) ||
 	peek_token (data, TOKEN_IMPLEMENTS) ||
 	peek_token (data, TOKEN_DELETE) ||
 	peek_token (data, TOKEN_RETURN) ||
commit eef641769b0e815da9d85e944961d71e5f402d78
Author: Pekka Lampila <pekka.lampila at iki.fi>
Date:   Mon Jun 16 15:19:51 2008 +0300

    vivi-compile: Fix label reference counting when parsing

diff --git a/vivified/code/vivi_parser.c b/vivified/code/vivi_parser.c
index e632b96..69c9aaf 100644
--- a/vivified/code/vivi_parser.c
+++ b/vivified/code/vivi_parser.c
@@ -389,14 +389,14 @@ vivi_parser_get_waiting_label (ParseData *data, const char *name)
   for (iter = data->level->waiting_labels; iter != NULL; iter = iter->next) {
     ViviCodeLabel *label = VIVI_CODE_LABEL (iter->data);
     if (g_str_equal (vivi_code_label_get_name (label), name))
-      return label;
+      return g_object_ref (label);
   }
 
   return NULL;
 }
 
 static void
-vivi_parser_remove_waiting_label (ParseData *data, const ViviCodeLabel *label)
+vivi_parser_remove_waiting_label (ParseData *data, ViviCodeLabel *label)
 {
   g_return_if_fail (data != NULL);
   g_return_if_fail (data->level != NULL);
@@ -404,6 +404,7 @@ vivi_parser_remove_waiting_label (ParseData *data, const ViviCodeLabel *label)
 
   data->level->waiting_labels =
     g_slist_remove (data->level->waiting_labels, label);
+  g_object_unref (label);
 }
 
 static ViviCodeLabel *
@@ -418,7 +419,7 @@ vivi_parser_get_label (ParseData *data, const char *name)
   for (iter = data->level->labels; iter != NULL; iter = iter->next) {
     ViviCodeLabel *label = VIVI_CODE_LABEL (iter->data);
     if (g_str_equal (vivi_code_label_get_name (label), name))
-      return label;
+      return g_object_ref (label);
   }
 
   return vivi_parser_get_waiting_label (data, name);


More information about the Swfdec-commits mailing list