Browse Source

Replace explicit map iteration with foreach.

Giovanni Mascellani 5 years ago
parent
commit
29710261f2
5 changed files with 112 additions and 99 deletions
  1. 44 34
      asmg/c_compile.g
  2. 24 15
      asmg/c_preproc.g
  3. 20 32
      asmg/map.g
  4. 12 8
      asmg/ramfs.g
  5. 12 10
      asmg/vfs.g

+ 44 - 34
asmg/c_compile.g

@@ -380,6 +380,17 @@ fun cctx_destroy_types 1 {
   ctx CCTX_ENUM_CONSTS take map_destroy ;
 }
 
+fun global_destroy_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  value global_destroy ;
+}
+
 fun cctx_destroy 1 {
   $ctx
   @ctx 0 param = ;
@@ -388,14 +399,7 @@ fun cctx_destroy 1 {
 
   $globals
   @globals ctx CCTX_GLOBALS take = ;
-  $i
-  @i 0 = ;
-  while i globals map_size < {
-    if globals i map_has_idx {
-      globals i map_at_idx global_destroy ;
-    }
-    @i i 1 + = ;
-  }
+  globals @global_destroy_closure 0 map_foreach ;
   globals map_destroy ;
 
   ctx CCTX_LABEL_POS take vector_destroy ;
@@ -440,46 +444,52 @@ fun cctx_dump_types 1 {
   }
 }
 
+fun dump_typename_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  "Typename " 1 platform_log ;
+  key 1 platform_log ;
+  ": #" 1 platform_log ;
+  value itoa 1 platform_log ;
+  "\n" 1 platform_log ;
+}
+
 fun cctx_dump_typenames 1 {
   $ctx
   @ctx 0 param = ;
 
-  $i
-  @i 0 = ;
   $typenames
   @typenames ctx CCTX_TYPENAMES take = ;
-  while i typenames map_size < {
-    if typenames i map_has_idx {
-      "Typename " 1 platform_log ;
-      typenames i map_key_at_idx 1 platform_log ;
-      ": #" 1 platform_log ;
-      typenames i map_at_idx itoa 1 platform_log ;
-      "\n" 1 platform_log ;
-    }
-    @i i 1 + = ;
-  }
+  typenames @dump_typename_closure 0 map_foreach ;
+}
+
+fun dump_global_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  "Global " 1 platform_log ;
+  key 1 platform_log ;
+  ": " 1 platform_log ;
+  value global_dump ;
+  "\n" 1 platform_log ;
 }
 
 fun cctx_dump_globals 1 {
   $ctx
   @ctx 0 param = ;
 
-  $i
-  @i 0 = ;
   $globals
   @globals ctx CCTX_GLOBALS take = ;
-  while i globals map_size < {
-    if globals i map_has_idx {
-      "Global " 1 platform_log ;
-      globals i map_key_at_idx 1 platform_log ;
-      $global
-      @global globals i map_at_idx = ;
-      ": " 1 platform_log ;
-      global global_dump ;
-      "\n" 1 platform_log ;
-    }
-    @i i 1 + = ;
-  }
+  globals @dump_global_closure 0 map_foreach ;
 }
 
 ifun cctx_type_compare 3

+ 24 - 15
asmg/c_preproc.g

@@ -79,19 +79,23 @@ fun ppctx_init 0 {
   ptr ret ;
 }
 
+fun subst_destroy_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  value subst_destroy ;
+}
+
 fun ppctx_destroy 1 {
   $ptr
   @ptr 0 param = ;
   $defs
   @defs ptr PPCTX_DEFINES take = ;
-  $i
-  @i 0 = ;
-  while i defs map_size < {
-    if defs i map_has_idx {
-      defs i map_at_idx subst_destroy ;
-    }
-    @i i 1 + = ;
-  }
+  defs @subst_destroy_closure 0 map_foreach ;
   defs map_destroy ;
   ptr PPCTX_BASE_PATH take free ;
   ptr free ;
@@ -471,6 +475,17 @@ fun push_token 2 {
   }
 }
 
+fun vector_destroy_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  value vector_destroy ;
+}
+
 fun process_token_function 5 {
   $ctx
   $tokens
@@ -560,13 +575,7 @@ fun process_token_function 5 {
   }
 
   # Free temporaries
-  @i 0 = ;
-  while i args map_size < {
-    if args i map_has_idx {
-      args i map_at_idx vector_destroy ;
-    }
-    @i i 1 + = ;
-  }
+  args @vector_destroy_closure 0 map_foreach ;
   args map_destroy ;
 }
 

+ 20 - 32
asmg/map.g

@@ -66,28 +66,6 @@ fun map_at 2 {
   addr MAP_ELEM_VALUE take ret ;
 }
 
-fun map_at_idx 2 {
-  $map
-  $idx
-  @map 1 param = ;
-  @idx 0 param = ;
-  $addr
-  @addr map idx vector_at_addr = ;
-  addr MAP_ELEM_PRESENT take "map_at_idx: element is not present" assert_msg ;
-  addr MAP_ELEM_VALUE take ret ;
-}
-
-fun map_key_at_idx 2 {
-  $map
-  $idx
-  @map 1 param = ;
-  @idx 0 param = ;
-  $addr
-  @addr map idx vector_at_addr = ;
-  addr MAP_ELEM_PRESENT take "map_key_at_idx: element is not present" assert_msg ;
-  addr MAP_ELEM_KEY take ret ;
-}
-
 fun map_has 2 {
   $map
   $key
@@ -103,16 +81,6 @@ fun map_has 2 {
   addr MAP_ELEM_PRESENT take ret ;
 }
 
-fun map_has_idx 2 {
-  $map
-  $idx
-  @map 1 param = ;
-  @idx 0 param = ;
-  $addr
-  @addr map idx vector_at_addr = ;
-  addr MAP_ELEM_PRESENT take ret ;
-}
-
 fun map_set 3 {
   $map
   $key
@@ -153,3 +121,23 @@ fun map_size 1 {
   @map 0 param = ;
   map vector_size ret ;
 }
+
+fun map_foreach 3 {
+  $map
+  $func
+  $ctx
+  @map 2 param = ;
+  @func 1 param = ;
+  @ctx 0 param = ;
+
+  $i
+  @i 0 = ;
+  while i map vector_size < {
+    $addr
+    @addr map i vector_at_addr = ;
+    if addr MAP_ELEM_PRESENT take {
+      ctx addr MAP_ELEM_KEY take addr MAP_ELEM_VALUE take func \3 ;
+    }
+    @i i 1 + = ;
+  }
+}

+ 12 - 8
asmg/ramfs.g

@@ -178,20 +178,24 @@ const RAMMOUNT_OPEN 4
 const RAMMOUNT_FILES 8
 const SIZEOF_RAMMOUNT 12
 
+fun ramfile_destroy_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  value ramfile_destroy ;
+}
+
 fun rammount_destroy 1 {
   $ram
   @ram 0 param = ;
 
   $files
   @files ram RAMMOUNT_FILES take = ;
-  $i
-  @i 0 = ;
-  while i files map_size < {
-    if files i map_has_idx {
-      files i map_at_idx ramfile_destroy ;
-    }
-    @i i 1 + = ;
-  }
+  files @ramfile_destroy_closure 0 map_foreach ;
   files map_destroy ;
 
   ram free ;

+ 12 - 10
asmg/vfs.g

@@ -124,22 +124,24 @@ fun vfsinst_init 0 {
   vfsinst ret ;
 }
 
+fun mount_destroy_closure 3 {
+  $ctx
+  $key
+  $value
+  @ctx 2 param = ;
+  @key 1 param = ;
+  @value 0 param = ;
+
+  value value MOUNT_DESTROY take \1 ;
+}
+
 fun vfsinst_destroy 1 {
   $vfsinst
   @vfsinst 0 param = ;
 
   $mounts
   @mounts vfsinst VFSINST_MOUNTS take = ;
-  $i
-  @i 0 = ;
-  while i mounts map_size < {
-    if mounts i map_has_idx {
-      $mount
-      @mount mounts i map_at_idx = ;
-      mount mount MOUNT_DESTROY take \1 ;
-    }
-    @i i 1 + = ;
-  }
+  mounts @mount_destroy_closure 0 map_foreach ;
   mounts map_destroy ;
 
   vfsinst free ;