changeset 354:fde5f96c6ff1

use common perl script
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 12 Jun 2020 20:44:01 +0900
parents c958c355f805
children 045299ad7e97
files src/CMakeLists.txt src/gearsTools/generate_context.pl src/gearsTools/generate_stub.pl
diffstat 3 files changed, 194 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Tue Jun 09 14:24:09 2020 +0900
+++ b/src/CMakeLists.txt	Fri Jun 12 20:44:01 2020 +0900
@@ -71,7 +71,7 @@
             add_custom_command (
                 OUTPUT    ${CMAKE_KERNEL_DIR_C}/${j}
                 DEPENDS   ${i}
-                COMMAND  "cd" "${CMAKE_KERNEL_DIR_C}" ";" "perl" "${CMAKE_SOURCE_DIR}/gearsTools/generate_stub.pl" "-o" ${j} ${CMAKE_SOURCE_DIR}/${i}
+                COMMAND  "cd" "${CMAKE_KERNEL_DIR_C}" ";" "perl" "${CMAKE_SOURCE_DIR}/gearsTools/generate_stub.pl" "--project" "xv6" "-o" ${j} ${CMAKE_SOURCE_DIR}/${i}
             )
             list(APPEND _Gears_CBC_SOURCES "${CMAKE_KERNEL_DIR_C}/${j}")
             #list(APPEND _Gears_CBC_SOURCES ${j})
@@ -100,7 +100,7 @@
     add_custom_command (
           OUTPUT    ${CMAKE_KERNEL_DIR_C}/c/${_Gears_TARGET}-context.c
           DEPENDS   ${_Gears_CBC_SOURCES} fs.img initcode
-          COMMAND  "cd" "CMakeFiles/kernel.dir" ";" "perl" "${CMAKE_SOURCE_DIR}/gearsTools/generate_context.pl" "-o" ${_Gears_TARGET} "-w" ${_Gears_CBC_SOURCES}
+          COMMAND  "cd" "CMakeFiles/kernel.dir" ";" "perl" "${CMAKE_SOURCE_DIR}/gearsTools/generate_context.pl" "--project" "xv6" "-o" ${_Gears_TARGET} "-w" ${_Gears_CBC_SOURCES}
     )
     # add_executable(${_Gears_TARGET} ${_Gears_CBC_SOURCES} ${_Gears_CSOURCES} ${CMAKE_KERNEL_DIR}/c/${_Gears_TARGET}-context.c )
     file(COPY "${CMAKE_SOURCE_DIR}/device" DESTINATION "${CMAKE_KERNEL_DIR_C}")
--- a/src/gearsTools/generate_context.pl	Tue Jun 09 14:24:09 2020 +0900
+++ b/src/gearsTools/generate_context.pl	Fri Jun 12 20:44:01 2020 +0900
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 
-use Getopt::Std;
+use Getopt::Long;
 use strict;
 
 # 
@@ -9,7 +9,7 @@
 # CodeGear
 # 
 # get stub information from # *.c
-#     __code taskManager_stub(struct Context* cbc_context) {
+#     __code taskManager_stub(struct Context* context) {
 # 
 # generate CodeGear indexn in context.h
 #     C_taskManager,
@@ -18,7 +18,7 @@
 #     extern __code taskManager_stub(struct Context*);
 # 
 # generate CodeGear stub reference in $name-context.h for each module
-#     cbc_context->code[C_taskManager]   = taskManager_stub;
+#     context->code[C_taskManager]   = taskManager_stub;
 # 
 # DataGear
 # 
@@ -43,8 +43,15 @@
 
 my $ddir = "c";
 
-our($opt_o,$opt_d,$opt_h,$opt_w);
-getopts('o:d:hw');
+our($opt_o,$opt_d,$opt_h,$opt_w, $opt_project);
+GetOptions(
+    "o=s"     => \$opt_o,
+    "d=s"     => \$opt_d,
+    "h"       => \$opt_h,
+    "w"       => \$opt_w,
+    "project" => \$opt_project,
+);
+
 
 my $name = $opt_o?$opt_o:"gears";
 
@@ -61,6 +68,22 @@
     exit;
 }
 
+my %projects = (
+    gears => { name => "gears", cotnext => "context" ,     template => "Gears::Context::Template"},
+    xv6   => { name => "xv6"  , context => "cbc_context" , template => "Gears::Context::Template::XV6"},
+);
+
+
+my $context_name = "context";
+
+my $project = $projects{gears};
+
+if ($opt_project && exists $projects{$opt_project}) {
+  $context_name = $projects{$opt_project}->{context};
+}
+
+
+
 my %codeGear;
 my %dataGear;
 my %constructor;
@@ -80,7 +103,7 @@
   use Data::Dumper;
   my @cbc_files;
   map { push(@cbc_files,File::Spec->rel2abs($_)); }  @ARGV;
-  my $gears      = Gears::Context->new(compile_sources => \@cbc_files, find_root => "$FindBin::Bin/../", output => $output);
+  my $gears      = Gears::Context->new(compile_sources => \@cbc_files, find_root => "$FindBin::Bin/../", output => $output, template => $project->{template});
   my $data_gears = $gears->extraction_dg_compile_sources($gears->{compile_sources});
   my $g          = $gears->set_data_gear_header_path(keys %{$data_gears->{impl}},keys %{$data_gears->{interfaces}});
 
@@ -109,7 +132,7 @@
     my ($filename) = @_;
     open my $fd,"<",$filename or die("can't open $filename $!");
     while (<$fd>) {
-        if (/^__code (\w+)_stub\(struct  *Context *\* *cbc_context\)/) {
+        if (/^__code (\w+)_stub\(struct  *Context *\* *${context_name}\)/) {
             $codeGear{$1} = $filename;
         } elsif (/^(\w+)(\*)+  *create(\w+)\(([^]]*)\)/) {
             my $interface = $1;
@@ -167,30 +190,40 @@
    
     my $code_init = ''; 
     for my $code ( sort keys %mCodeGear ) {
-        $code_init .=  "    cbc_context->code[C_${code}]    = ${code}_stub;\n";
+        $code_init .=  "    context->code[C_${code}]    = ${code}_stub;\n";
     }
 
     my $data_num = keys(%dataGear);
     $data_num++;
-my $context_c = << "EOFEOF";
+    my $context_c;
+    if ($project->{name} eq "xv6") {
+      $context_c .= << "EOFEOF";
 #ifndef CBCXV6
 #include <stdlib.h>
 #endif
+EOFEOF
+    } else {
+      $context_c .= << "EOFEOF";
+#include <stdlib.h>
+EOFEOF
+}
+
+$context_c .= << "EOFEOF";
 
 #include "../context.h"
 
-void initContext(struct Context* cbc_context) {
-    cbc_context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
-    cbc_context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*);
-    cbc_context->data = NEWN(ALLOCATE_SIZE, union Data*);
-    cbc_context->heapStart = NEWN(cbc_context->heapLimit, char);
-    cbc_context->heap = cbc_context->heapStart;
-    // cbc_context->codeNum = Exit;
+void initContext(struct Context* $context_name) {
+    ${context_name}\->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
+    ${context_name}\->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*);
+    ${context_name}\->data = NEWN(ALLOCATE_SIZE, union Data*);
+    ${context_name}\->heapStart = NEWN(${context_name}\->heapLimit, char);
+    ${context_name}\->heap = ${context_name}\->heapStart;
+    // ${context_name}\->codeNum = Exit;
 
 $code_init
 
 #include "dataGearInit.c"
-    cbc_context->dataNum = $data_num;
+    ${context_name}\->dataNum = $data_num;
 }
 EOFEOF
 
@@ -198,34 +231,51 @@
     print $fd $context_c;
 
 my $meta_call = <<"EOFEOF";
-__code meta(struct Context* cbc_context, enum Code next) {
+__code meta(struct Context* ${context_name}, enum Code next) {
     // printf("meta %d\\n",next);
-    goto (cbc_context->code[next])(cbc_context);
+    goto (${context_name}\->code[next])(${context_name});
 }
 
-__code parGotoMeta(struct Context* cbc_context, enum Code next) {
-    cbc_context->task     = NULL;
-    cbc_context->taskList = NULL;
-    goto (cbc_context->code[Gearef(cbc_context, TaskManager)->taskManager->TaskManager.spawnTasks])(cbc_context);
+__code parGotoMeta(struct Context* ${context_name}, enum Code next) {
+    ${context_name}->task     = NULL;
+    ${context_name}->taskList = NULL;
+    goto (${context_name}\->code[Gearef(${context_name}, TaskManager)->taskManager->TaskManager.spawnTasks])(${context_name});
+}
+
+__code start_code(struct Context* ${context_name}) {
+    goto meta(${context_name}, ${context_name}\->next);
 }
 
-__code start_code(struct Context* cbc_context) {
-    goto meta(cbc_context, cbc_context->next);
+__code start_code_stub(struct Context* ${context_name}) {
+    goto start_code(${context_name});
+}
+EOFEOF
+
+if ($project->{name} eq "gears") {
+    $mata_call .= <<"EOFEOF";
+__code exit_code(struct Context* ${context_name}) {
+    free(${context_name}->code);
+    free(${context_name}->data);
+    free(${context_name}->heapStart);
+    goto exit(0);
 }
+EOFEOF
 
-__code start_code_stub(struct Context* cbc_context) {
-    goto start_code(cbc_context);
+} else {
+
+$mata_call .= <<"EOFEOF";
+__code exit_code(struct Context* ${context_name}) {
+    // free(${context_name}->code);
+    // free(${context_name}->data);
+    // free(${context_name}->heapStart);
+    goto exit_code(cbc_context);
+}
+EOFEOF
 }
 
-__code exit_code(struct Context* cbc_context) {
-    // free(cbc_context->code);
-    // free(cbc_context->data);
-    // free(cbc_context->heapStart);
-    goto exit_code(cbc_context);
-}
-
-__code exit_code_stub(struct Context* cbc_context) {
-    goto exit_code(cbc_context);
+$mata_call .= <<"EOFEOF";
+__code exit_code_stub(struct Context* ${context_name}) {
+    goto exit_code(${context_name});
 }    
 
 // end context_c
@@ -250,7 +300,7 @@
 
 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!");
 for my $data ( sort keys %dataGear ) {
-    print $fd "    ALLOC_DATA(cbc_context, ${data});\n";
+    print $fd "    ALLOC_DATA(${context_name}, ${data});\n";
 }
 }
 
--- a/src/gearsTools/generate_stub.pl	Tue Jun 09 14:24:09 2020 +0900
+++ b/src/gearsTools/generate_stub.pl	Fri Jun 12 20:44:01 2020 +0900
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 use strict;
-use Getopt::Std;
+use Getopt::Long;
 use File::Path qw(make_path);
 
 # interface.h
@@ -15,8 +15,14 @@
 #         struct Queue* tasks;
 #     } Worker;
 
-our($opt_o,$opt_d,$opt_h);
-getopts('o:d:h');
+our($opt_o,$opt_d,$opt_h, $opt_project);
+
+GetOptions(
+    "o=s" => \$opt_o,
+    "d=s" => \$opt_d,
+    "h"   => \$opt_h,
+    "project" => \$opt_project,
+);
 
 my $dir = ".";
 if ($opt_d) {
@@ -26,6 +32,19 @@
     }
 }
 
+
+
+my %projects = (
+    gears => { cotnext => "context" },
+    xv6   => { context => "cbc_context" },
+);
+
+
+my $context_name = "context";
+if ($opt_project && exists $projects{$opt_project}) {
+  $context_name = $projects{$opt_project}->{context};
+}
+
 for my $fn (@ARGV) {
     next if ($fn !~ /\.cbc$/);
     &getDataGear($fn);
@@ -68,10 +87,10 @@
 #
 # generated meta level code
 #
-# Gearef(cbc_context, Stack)->stack = (union Data*)nodeStack;
-# Gearef(cbc_context, Stack)->data = (union Data*)node;
-# Gearef(cbc_context, Stack)->next = C_stackTest3;
-# goto meta(cbc_context, nodeStack->push);
+# Gearef(context, Stack)->stack = (union Data*)nodeStack;
+# Gearef(context, Stack)->data = (union Data*)node;
+# Gearef(context, Stack)->next = C_stackTest3;
+# goto meta(context, nodeStack->push);
 
 sub getDataGear {
     my ($filename) = @_;
@@ -117,6 +136,14 @@
                 if (-f $interfaceHeader) {
                     &getDataGear("$interfaceHeader");
                     &getCodeGear("$interfaceHeader");
+                } else {
+                  if ($filename =~ /([\w\/]+)\/(.+)$/) {
+                    $interfaceHeader = "$1/$interfaceHeader";
+                    if (-f $interfaceHeader) {
+                        &getDataGear("$interfaceHeader");
+                        &getCodeGear("$interfaceHeader");
+                    }
+                  }
                 }
             } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
                 my $codeGearName = $1;
@@ -134,11 +161,15 @@
         if (/^\s*(.*)\s+(\w+);$/ ) {
             my $ttype = $1;
             my $tname = $2;
-            if ($ttype =~ /^(union|struct|const)?\s*(\w+)/) {
+            if ($ttype =~ /^(union|struct|const|enu,)?\s*(\w+)/) {
                 if ($1 ne 'const') {
                     $ttype = $2;
                 } else {
-                    $ttype = "const $2";
+                    my $vname = $2;
+                    my $ttype = $1;
+                    if ($ttype =~ /(const|enum)/) {
+                      $ttype = "$1 $vname";
+                    }
                 }
             }
             $described_data_gear = 1;
@@ -147,15 +178,15 @@
         if (/__code (\w+)/) {
             next if $described_data_gear;
             my $args = $';
-            while ($args =~ /\s*(struct|union|const)?\s*([\w\[\]_]+)\*?\s*(\w+),?/g) {
+            while ($args =~ /\s*(struct|union|const|enum)?\s*([\w\[\]_]+)\*?\s*(\w+),?/g) {
               #$args eq  (Impl* vm, pde_t* pgdir, char* init, uint sz, __code next(...));
               my $const_type = $1;
               my $ttype = $2;
               my $tname = $3;
 
               $ttype =~ s/(Impl|Isa|Type)/Data/;
-              if ($const_type eq 'const') {
-                $ttype = "const $ttype";
+              if ($const_type =~ /(const|enum)/) {
+                $ttype = "$1 $ttype";
               }
               $var{$name}->{$tname} = $ttype;
             }
@@ -254,7 +285,7 @@
 
 sub generateStub {
     my($fd,$prevCodeGearName,$dataGearName) = @_;
-    print $fd "__code ", $prevCodeGearName ,"_stub(struct Context* cbc_context) {\n";
+    print $fd "__code ", $prevCodeGearName ,"_stub(struct Context* $context_name) {\n";
     print $fd $dataGearName;
     print $fd "\n} \n\n";
     return 1;
@@ -271,7 +302,7 @@
     push @{$dataGearVarType{$codeGearName}}, $typeName;
     if ($typeName eq $implementation) {
         # get implementation
-        $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(cbc_context, $interface, $varName);\n";
+        $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl($context_name, $interface, $varName);\n";
     } else {
         # interface var
         for my $ivar (keys %{$var{$interface}}) {
@@ -279,11 +310,11 @@
             if ($varName eq $ivar) {
                 if ($typeName eq $var{$interface}->{$ivar}) {
                     if ($output) {
-                        $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = &Gearef(cbc_context, $interface)->$varName;\n";
+                        $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = &Gearef($context_name, $interface)->$varName;\n";
                         $outputVar{$codeGearName} .= "\t$typeName$ptrType $varName  __attribute__((unused))  = *O_$varName;\n";
                         return 1;
                     }
-                    $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = Gearef(cbc_context, $interface)->$varName;\n";
+                    $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = Gearef($context_name, $interface)->$varName;\n";
                     return 1;
                 }
             }
@@ -293,7 +324,7 @@
         for my $cName (keys %{$code{$interface}}) {
             if ($varName eq $cName) {
                 # continuation field
-                $dataGearName{$codeGearName} .= "\tenum Code $varName = Gearef(cbc_context, $interface)->$varName;\n";
+                $dataGearName{$codeGearName} .= "\tenum Code $varName = Gearef($context_name, $interface)->$varName;\n";
                 return 1;
             }
         }
@@ -304,11 +335,11 @@
                 my ($type, $count) = split(/\s/, $codeGear{$codeGearName}->{"var"}->{$var});
                 if ($typeName eq $type) {
                     if ($output) {
-                        $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = ($typeName $ptrType*)&cbc_context->data[cbc_context->odg + $count];\n";
+                        $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = ($typeName $ptrType*)&${context_name}->data[${context_name}\->odg + $count];\n";
                         $outputVar{$codeGearName} .= "\t$typeName$ptrType $varName = *O_$varName;\n";
                         return 1;
                     }
-                    $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = &cbc_context->data[cbc_context->idg + $count]->$typeName;\n";
+                    $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = &${context_name}->data[${context_name}\->idg + $count]->$typeName;\n";
                     return 1;
                 }
             }
@@ -318,7 +349,7 @@
         for my $cName (keys %{$codeGear{$codeGearName}->{"code"}}) {
             if ($varName eq $cName) {
                 # continuation field
-                $dataGearName{$codeGearName} .= "\tenum Code $varName = cbc_context->next;\n";
+                $dataGearName{$codeGearName} .= "\tenum Code $varName = ${context_name}\->next;\n";
                 return 1;
             }
         }
@@ -326,10 +357,10 @@
         # par goto continuation
         # global or local variable case
         if ($typeName eq "Code") {
-            $dataGearName{$codeGearName} .= "\tenum $typeName$ptrType $varName = Gearef(cbc_context, $interface)->$varName;\n";
+            $dataGearName{$codeGearName} .= "\tenum $typeName$ptrType $varName = Gearef(${context_name}, $interface)->$varName;\n";
             return 1;
         }
-        $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = Gearef(cbc_context, $typeName);\n";
+        $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = Gearef($context_name, $typeName);\n";
         return 1;
     }
 }
@@ -415,12 +446,12 @@
                 $dataGearVar{$codeGearName} = [];
                 $outputVar{$codeGearName} = "";
                 $outputArgs{$codeGearName} = {};
-                my $newArgs = "struct Context *cbc_context,";
-                if ($args=~/^struct Context\s*\*\s*cbc_context/) {
+                my $newArgs = "struct Context *${context_name},";
+                if ($args=~/^struct Context\s*\*\s*${context_name}/) {
                     $newArgs = "";
                 }
                 if (!$args){
-                    $newArgs = "struct Context *cbc_context";
+                    $newArgs = "struct Context *${context_name}";
                 }
                 while($args) {
                     if ($args =~ s/(^\s*,\s*)//) {
@@ -439,12 +470,12 @@
                         for my $arg (@args) {
                             $arg =~ s/^\s*//;
                             last if ($arg =~ /\.\.\./);
-                            $arg =~ s/^(struct|union|const)?\s*(\w+)(\**)\s(\w+)//;
+                            $arg =~ s/^(struct|union|const|enum)?\s*(\w+)(\**)\s(\w+)//;
                             my $structType = $1;
                             my $typeName = $2;
                             my $ptrType = $3;
                             my $varName = $4;
-                        if ($structType =~ /const/) {
+                        if ($structType =~ /(const|enum)/) {
                             $typeName = "$structType $typeName";
                         }
                             my $typeField = lcfirst($typeName);
@@ -453,13 +484,13 @@
                                 $newArgs .= ",$structType $typeName **O_$varName";
                             }
                         }
-                    } elsif ($args =~ s/^(struct|union|const)?\s*(\w+)(\**)\s(\w+)//) {
+                    } elsif ($args =~ s/^(struct|union|const|enum)?\s*(\w+)(\**)\s(\w+)//) {
                         my $structType = $1;
                         my $typeName = $2;
                         my $ptrType = $3;
                         my $varName = $4;
                         $newArgs .= $&;    # assuming no duplicate
-                        if ($structType =~ /const/) {
+                        if ($structType =~ /(const|enum)/) {
                             $typeName = "$structType $typeName";
                         }
                         my $typeField = lcfirst($typeName);
@@ -472,7 +503,7 @@
                     }
                 }
                 # generate goto statement from stub to the CodeGear in the buffer
-                $dataGearName{$codeGearName} .= "\tgoto $codeGearName(cbc_context";
+                $dataGearName{$codeGearName} .= "\tgoto $codeGearName(${context_name}";
                 for my $arg ( @{$dataGearVar{$codeGearName}}) {
                     $dataGearName{$codeGearName} .= ", $arg";
                 }
@@ -486,7 +517,7 @@
                 }
                 next;
             } elsif (! $inCode) {
-                s/new\s+(\w+)\(\)/\&ALLOCATE(cbc_context, \1)->\1/g;   # replacing new
+                s/new\s+(\w+)\(\)/\&ALLOCATE(${context_name}, \1)->\1/g;   # replacing new
                 print $fd $_;
                 next;
             } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) {
@@ -516,7 +547,7 @@
                     $ntype = $localVarType{$next};
                     $ftype = lcfirst($ntype);
                 }
-                print $fd "\tGearef(cbc_context, $ntype)->$ftype = (union Data*) $next;\n";
+                print $fd "\tGearef(${context_name}, $ntype)->$ftype = (union Data*) $next;\n";
                 # Put interface argument
                 my $prot = $code{$ntype}->{$method};
                 my $i = 1;
@@ -531,19 +562,19 @@
                     $arg =~ s/^(\s)*(\w+)/$2/;
                     if ($pType =~ s/\_\_code$//) {
                         if ($arg =~ /(\w+)\(.*\)/) {
-                            print $fd "\tGearef(cbc_context, $ntype)->$pName = $1;\n";
+                            print $fd "\tGearef(${context_name}, $ntype)->$pName = $1;\n";
                         } else {
-                            print $fd "\tGearef(cbc_context, $ntype)->$pName = C_$arg;\n";
+                            print $fd "\tGearef(${context_name}, $ntype)->$pName = C_$arg;\n";
                         }
                     } elsif ($pType =~ /Data\**$/){
-                        print $fd "\tGearef(cbc_context, $ntype)->$pName = (union $pType) $arg;\n";
+                        print $fd "\tGearef(${context_name}, $ntype)->$pName = (union $pType) $arg;\n";
                     } else {
-                        print $fd "\tGearef(cbc_context, $ntype)->$pName = $arg;\n";
+                        print $fd "\tGearef(${context_name}, $ntype)->$pName = $arg;\n";
                     }
                     $i++;
                 }
-                # print $fd "${prev}cbc_context->before = C_$codeGearName;\n";
-                print $fd "${prev}goto meta(cbc_context, $next->$method);\n";
+                print $fd "${prev}context->before = C_$codeGearName;\n";
+                print $fd "${prev}goto meta(context, $next->$method);\n";
                 next;
             } elsif(/^(.*)par goto (\w+)\((.*)\);/) {
                 # handling par goto statement
@@ -568,41 +599,41 @@
                     print $fd "${prev}struct Element* element;\n";
                 }
                 my $initTask = << "EOFEOF";
-                ${prev}cbc_context->task = NEW(struct Context);
-                ${prev}initContext(cbc_context->task);
-                ${prev}cbc_context->task->next = C_$codeGearName;
-                ${prev}cbc_context->task->idgCount = $inputCount;
-                ${prev}cbc_context->task->idg = cbc_context->task->dataNum;
-                ${prev}cbc_context->task->maxIdg = cbc_context->task->idg + $inputCount;
-                ${prev}cbc_context->task->odg = cbc_context->task->maxIdg;
-                ${prev}cbc_context->task->maxOdg = cbc_context->task->odg + $outputCount;
+                ${prev}${context_name}\->task = NEW(struct Context);
+                ${prev}initContext(${context_name}\->task);
+                ${prev}${context_name}\->task->next = C_$codeGearName;
+                ${prev}${context_name}\->task->idgCount = $inputCount;
+                ${prev}${context_name}\->task->idg = ${context_name}\->task->dataNum;
+                ${prev}${context_name}\->task->maxIdg = ${context_name}\->task->idg + $inputCount;
+                ${prev}${context_name}\->task->odg = ${context_name}\->task->maxIdg;
+                ${prev}${context_name}\->task->maxOdg = ${context_name}\->task->odg + $outputCount;
 EOFEOF
                 print $fd $initTask;
                 if (@iterateCounts) {
-                    print $fd "${prev}cbc_context->task->iterate = 0;\n";
+                    print $fd "${prev}${context_name}\->task->iterate = 0;\n";
                     my $len = @iterateCounts;
                     if ($len == 1) {
-                        print $fd "${prev}cbc_context->task->iterator = createMultiDimIterator(cbc_context, $iterateCounts[0], 1, 1);\n";
+                        print $fd "${prev}${context_name}\->task->iterator = createMultiDimIterator(${context_name}, $iterateCounts[0], 1, 1);\n";
                     } elsif ($len == 2) {
-                        print $fd "${prev}cbc_context->task->iterator = createMultiDimIterator(cbc_context, $iterateCounts[0], $iterateCounts[1], 1);\n";
+                        print $fd "${prev}${context_name}\->task->iterator = createMultiDimIterator(${context_name}, $iterateCounts[0], $iterateCounts[1], 1);\n";
                     } elsif ($len == 3) {
-                        print $fd "${prev}cbc_context->task->iterator = createMultiDimIterator(cbc_context, $iterateCounts[0], $iterateCounts[1], $iterateCounts[2]);\n";
+                        print $fd "${prev}${context_name}\->task->iterator = createMultiDimIterator(${context_name}, $iterateCounts[0], $iterateCounts[1], $iterateCounts[2]);\n";
                     }
                 }
                 for my $dataGear (@dataGears) {
-                    print $fd "${prev}GET_META($dataGear)->wait = createSynchronizedQueue(cbc_context);\n";
+                    print $fd "${prev}GET_META($dataGear)->wait = createSynchronizedQueue(${context_name});\n";
                 }
                 for my $i (0..$inputCount-1) {
-                    print $fd "${prev}cbc_context->task->data[cbc_context->task->idg+$i] = (union Data*)@dataGears[$i];\n";
+                    print $fd "${prev}${context_name}\->task->data[${context_name}\->task->idg+$i] = (union Data*)@dataGears[$i];\n";
                 }
                 for my $i (0..$outputCount-1) {
-                    print $fd "${prev}cbc_context->task->data[cbc_context->task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n";
+                    print $fd "${prev}${context_name}\->task->data[${context_name}\->task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n";
                 }
                 my $putTask = << "EOFEOF";
-                ${prev}element = &ALLOCATE(cbc_context, Element)->Element;
-                ${prev}element->data = (union Data*)cbc_context->task;
-                ${prev}element->next = cbc_context->taskList;
-                ${prev}cbc_context->taskList = element;
+                ${prev}element = &ALLOCATE(${context_name}, Element)->Element;
+                ${prev}element->data = (union Data*)${context_name}\->task;
+                ${prev}element->next = ${context_name}\->taskList;
+                ${prev}${context_name}\->taskList = element;
 EOFEOF
                 print $fd $putTask;
                 next;
@@ -628,50 +659,50 @@
                         print $fd "\t*O_$arg = $v;\n";
                     }
                     if ($hasParGoto) {
-                        print $fd "${prev}Gearef(cbc_context, TaskManager)->taskList = cbc_context->taskList;\n";
-                        print $fd "${prev}Gearef(cbc_context, TaskManager)->next1 = C_$next;\n";
-                        print $fd "${prev}goto meta(cbc_context, C_$next);\n";
+                        print $fd "${prev}Gearef(${context_name}, TaskManager)->taskList = ${context_name}->taskList;\n";
+                        print $fd "${prev}Gearef(${context_name}, TaskManager)->next1 = C_$next;\n";
+                        print $fd "${prev}goto meta(${context_name}, C_$next);\n";
                     } else {
-                        # print $fd "${prev}cbc_context->before = C_$codeGearName;\n";
-                        print $fd "${prev}goto meta(cbc_context, $next);\n";
+                        print $fd "${prev}${context_name}->before = C_$codeGearName;\n";
+                        print $fd "${prev}goto meta(${context_name}, $next);\n";
                     }
                     next;
                 }
                 if ($hasParGoto) {
-                    print $fd "${prev}Gearef(cbc_context, TaskManager)->taskList = cbc_context->taskList;\n";
-                    print $fd "${prev}Gearef(cbc_context, TaskManager)->next1 = C_$next;\n";
-                    print $fd "${prev}goto parGotoMeta(cbc_context, C_$next);\n";
+                    print $fd "${prev}Gearef(${context_name}, TaskManager)->taskList = ${context_name}\->taskList;\n";
+                    print $fd "${prev}Gearef(${context_name}, TaskManager)->next1 = C_$next;\n";
+                    print $fd "${prev}goto parGotoMeta(${context_name}, C_$next);\n";
                     next;
                 } elsif ($next eq "meta") {
                     print $fd $_;
                     next;
                 } else {
-                    # print $fd "${prev}cbc_context->before = C_$codeGearName;\n";
-                    print $fd "${prev}goto meta(cbc_context, C_$next);\n";
+                    print $fd "${prev}${context_name}\->before = C_$codeGearName;\n";
+                    print $fd "${prev}goto meta(${context_name}, C_$next);\n";
                     next;
                 }
             } elsif(/^.*(struct|union)?\s(\w+)\*\s(\w+)\s?[=;]/) {
                 my $type    = $2;
                 my $varName = $3;
                 $localVarType{$varName} = $type;
-                s/new\s+(\w+)\(\)/\&ALLOCATE(cbc_context, \1)->\1/g;   # replacing new
+                s/new\s+(\w+)\(\)/\&ALLOCATE(${context_name}, \1)->\1/g;   # replacing new
             } elsif(/^}/) {
                 $hasParGoto = 0;
             } else {
-                s/new\s+(\w+)\(\)/\&ALLOCATE(cbc_context, \1)->\1/g;   # replacing new
+                s/new\s+(\w+)\(\)/\&ALLOCATE(${context_name}, \1)->\1/g;   # replacing new
             }
             # gather type name and type
         } elsif ($inMain) {
-            if (/^(.*)goto start_code\(main_context\);/) {
+            if (/^(.*)goto start_code\(main_${context_name}\);/) {
                 print $fd $_;
                 next;
             } elsif (/^(.*)goto (\w+)\((.*)\);/) {
                 my $prev = $1;
                 my $next = $2;
-                print $fd "${prev}struct Context* main_context = NEW(struct Context);\n";
-                print $fd "${prev}initContext(main_context);\n";
-                print $fd "${prev}main_cbc_context->next = C_$next;\n";
-                print $fd "${prev}goto start_code(main_context);\n";
+                print $fd "${prev}struct Context* main_${context_name} = NEW(struct Context);\n";
+                print $fd "${prev}initContext(main_${context_name});\n";
+                print $fd "${prev}main_${context_name}->next = C_$next;\n";
+                print $fd "${prev}goto start_code(main_${context_name});\n";
                 next;
             }
         }