Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding input/output function

  • Loading branch information...
commit cee81d7ad42d72853efff8188a377114c932952a 1 parent c64879e
@hongsup hongsup authored
View
3  make_dirs.sh
@@ -3,7 +3,8 @@ OJB=obj
BIN=bin
CPP=apps/output_cpp
LIB=apps/output_cpp/gm_graph
-BUILD_DIRS="bin obj $CPP/bin $CPP/generated $CPP/bin $CPP/data $LIB/lib"
+GPS=apps/output_gps
+BUILD_DIRS="bin obj $CPP/bin $CPP/generated $CPP/bin $CPP/data $LIB/lib $GPS/generated"
for D in $BUILD_DIRS
do
if [ ! -d ${D} ]
View
90 src/backend_gps/gm_gps_gen_master.cc
@@ -5,6 +5,8 @@
#include "gm_code_writer.h"
#include "gm_frontend.h"
#include "gm_transform_helper.h"
+
+
void gm_gps_gen::write_headers()
{
Body.pushln("package gps.examples;"); // hardcodede
@@ -40,7 +42,9 @@ void gm_gps_gen::do_generate_master_class()
{
ast_procdef* proc = FE.get_current_proc();
- // create master state machine
+ //--------------------------------------------------------------------
+ // create master class
+ //--------------------------------------------------------------------
char temp[1024];
sprintf(temp, "public static class %sMaster extends Master {", proc->get_procname()->get_genname());
Body.pushln(temp);
@@ -49,12 +53,79 @@ void gm_gps_gen::do_generate_master_class()
Body.pushln("private int _master_state_nxt = 0;");
Body.pushln("private boolean _master_should_start_workers = false;");
Body.pushln("private boolean _master_should_finish = false;");
- //Body.pushln("private int get_master_state() {return _maser_state;}");
- //Body.pushln("private void set_master_state(int s) {_maser_state = s;}");
- //Body.pushln("private boolean get_master_cond_result() {return _master_cond_result ;}");
- //Body.pushln("private void set_master_cond_result(bool b) {_master_cond_result = b;}");
Body.NL();
+ //--------------------------------------------------------------------
+ // constructor
+ // (with command-line argument parsing)
+ //--------------------------------------------------------------------
+ sprintf(temp, "public %sMaster (CommandLine line) {", proc->get_procname()->get_genname());
+ Body.pushln(temp);
+
+ Body.pushln("// parse command-line arguments (if any)");
+ Body.pushln("java.util.HashMap<String,String> arg_map = new java.util.HashMap<String,String>()");
+ Body.pushln("gps.node.Utils.parseOtherOptions(line, arg_map);");
+ Body.NL();
+
+ // Iterate symbol table and
+ gm_symtab* args = proc->get_symtab_var(); assert(args != NULL);
+ std::vector<gm_symtab_entry*>& syms = args->get_entries();
+ std::vector<gm_symtab_entry*>::iterator I;
+ for (I=syms.begin(); I!=syms.end(); I++)
+ {
+ gm_symtab_entry* s = *I;
+
+ // input argument
+ if (!s->getType()->is_primitive()) continue;
+ if (s->isReadable()) {
+ sprintf(temp, "if (arg_map.containsKey(\"%s\")) {", s->getId()->get_genname());
+ Body.pushln(temp);
+ sprintf(temp, "String s = arg_map.get(\"%s\");", s->getId()->get_genname());
+ Body.pushln(temp);
+ sprintf(temp, "%s = ", s->getId()->get_genname());
+ Body.push(temp);
+ switch(s->getType()->getTypeSummary())
+ {
+ case GMTYPE_BOOL: Body.pushln("Boolean.parseBoolean(s);"); break;
+ case GMTYPE_INT: Body.pushln("Integer.parseInt(s);"); break;
+ case GMTYPE_LONG: Body.pushln("Long.parseLong(s);"); break;
+ case GMTYPE_FLOAT: Body.pushln("Float.parseFloat(s);"); break;
+ case GMTYPE_DOUBLE: Body.pushln("Double.parseDouble(s);"); break;
+ default: assert(false);
+ }
+ Body.pushln("}");
+ }
+ }
+ Body.pushln("}");
+ Body.NL();
+
+ //--------------------------------------------------------------------
+ // A method that saves final output values
+ //--------------------------------------------------------------------
+ Body.pushln("//save output");
+ Body.pushln("public void writeOutput(BufferedWrite bw) throws IOException {");
+ ast_typedecl* t = proc->get_return_type();
+ if ((t!= NULL) && (!t->is_void())) {
+ sprintf(temp, "bw.write(\"%s:\\t\" + %s + \"\\n\");",
+ GPS_RET_VALUE,
+ GPS_RET_VALUE);
+ Body.pushln(temp);
+ }
+ for (I=syms.begin(); I!=syms.end(); I++)
+ {
+ gm_symtab_entry* s = *I;
+
+ // output arguments
+ if (!s->getType()->is_primitive()) continue;
+ if (s->isWriteable()) {
+ sprintf(temp, "bw.write(\"%s:\\t\" + %s + \"\\n\");",
+ s->getId()->get_genname(),
+ s->getId()->get_genname());
+ Body.pushln(temp);
+ }
+ }
+ Body.pushln("}");
+ Body.NL();
}
@@ -84,6 +155,15 @@ void gm_gps_gen::do_generate_master_scalar()
Body.pushln(temp);
}
+ ast_procdef* proc = FE.get_current_proc();
+ ast_typedecl* t = proc->get_return_type();
+ if ((t!= NULL) && (!t->is_void())) {
+ sprintf(temp, "private %s %s; // the final return value of the procedure",
+ get_type_string(t, true),
+ GPS_RET_VALUE);
+ Body.pushln(temp);
+ }
+
Body.NL();
}
View
11 src/backend_gps/gm_gps_java.cc
@@ -120,3 +120,14 @@ void gm_gps_gen::generate_sent_assign(ast_assign *a)
this->gm_code_generator::generate_sent_assign(a);
}
}
+
+void gm_gps_gen::generate_sent_return(ast_return *r)
+{
+ if (r->get_expr() != NULL) {
+ _Body.push(GPS_RET_VALUE);
+ _Body.push(" = ");
+ generate_expr(r->get_expr());
+ _Body.pushln(";");
+ }
+
+}
View
5 src/inc/gm_backend_gps.h
@@ -177,7 +177,7 @@ class gm_gps_gen : public gm_backend , public gm_code_generator
virtual void generate_sent_vardecl(ast_vardecl *a) {assert(false);}
virtual void generate_sent_bfs(ast_bfs *a) {assert(false);}
virtual void generate_sent_foreach(ast_foreach *f) {assert(false);}
- virtual void generate_sent_return(ast_return *r) {}
+ virtual void generate_sent_return(ast_return *r);
virtual void generate_sent_assign(ast_assign *a);
virtual const char* get_type_string(int prim_type) {assert(false);}
@@ -190,5 +190,8 @@ class gm_gps_gen : public gm_backend , public gm_code_generator
extern gm_gps_gen GPS_BE;
+// string used in code generator
+static const char* GPS_RET_VALUE = "_ret_value";
+
#endif
View
3  test/gps/gps1.gm
@@ -1,4 +1,5 @@
+// Test of basic structure creation
Procedure gps (G: Graph, x: Int, A,B:Node_Property<Int>(G) ) : Int
{
Int z = 0;
@@ -7,6 +8,6 @@ Procedure gps (G: Graph, x: Int, A,B:Node_Property<Int>(G) ) : Int
z = Sum(t: G.Nodes) {t.A + t.B};
}
- // Test of Returen
+ // Test of Return
Return z;
}
View
7 test/gps/gps2.gm
@@ -1,6 +1,9 @@
-Procedure gps2(G: Graph, x: Int, A,B: N_P<Int>(G))
+
+Procedure gps2(G: Graph, x: Int, A,B: N_P<Int>(G); z: Int) // z is an output parameter
{
- Int z = x + 1;
+ // test of output parameter
+ z = x + 1;
+
Foreach(t: G.Nodes) {
t.A = t.B*(z+1);
}
View
12 test/gps/gps3.gm
@@ -3,23 +3,13 @@ Procedure gps3 (G: Graph, x: Int, A:Node_Property<Int>(G) ) : Int
{
Int z = 0;
Int h = x;
- /*
- // test of while
- While (h > 0) {
- // Test of reduction into z
- z = Sum(t: G.Nodes) {t.A};
- h = h-1;
- }
- */
- // test of do-while
+ // test of do-while and sequential operation
Do {
- // Test of reduction into z
z = Sum(t: G.Nodes) {t.A};
h = h+1;
}
While (h <= 0) ;
- // Test of Returen
Return z;
}
Please sign in to comment.
Something went wrong with that request. Please try again.