Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 863383b

Browse filesBrowse files
committed
First attampt to implemtent CloudLoop; other changes regart to CloudVar and CloudSharedVar
1 parent 5cd5e84 commit 863383b
Copy full SHA for 863383b
Expand file treeCollapse file tree

26 files changed

+253
-180
lines changed
Open diff view settings
Collapse file

‎src/lambdacloud/core/CloudFunc.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudFunc.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public CloudFunc compile(String name, Expr[] args, Expr[] exprs) {
7878
return this;
7979
}
8080

81-
public void apply(CloudVar output, CloudVar ...inputs) {
81+
public void apply(CloudSharedVar output, CloudSharedVar ...inputs) {
8282
if(CloudConfig.isLocal()) {
8383
if(inputs.length == 0) {
8484
switch(funcType) {
@@ -131,7 +131,7 @@ public void apply(CloudVar output, CloudVar ...inputs) {
131131
} catch (InterruptedException e) {
132132
e.printStackTrace();
133133
}
134-
CloudVar rlt = handler.getCloudVar();
134+
CloudSharedVar rlt = handler.getCloudVar();
135135
output.setLabel(rlt.getLabel());
136136
output.data = rlt.data;
137137
output.isOnCloud = rlt.isOnCloud;
Collapse file

‎src/lambdacloud/core/CloudIf.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudIf.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CloudIf extends CloudBase {
2121
Expr condition;
2222
List<Expr> trueStmts = new ArrayList<Expr>();
2323
List<Expr> falseStmts = new ArrayList<Expr>();
24-
public CloudIf(Expr condition, CloudVar ...args) {
24+
public CloudIf(Expr condition, CloudSharedVar ...args) {
2525
this.condition = condition;
2626
}
2727

Collapse file

‎src/lambdacloud/core/CloudLoop.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudLoop.java
+88-68Lines changed: 88 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import static com.sun.org.apache.bcel.internal.Constants.ACC_SUPER;
55

66
import java.util.ArrayList;
7+
import java.util.HashMap;
78
import java.util.List;
9+
import java.util.Map;
810

911
import com.sun.corba.se.impl.javax.rmi.CORBA.Util;
1012
import com.sun.org.apache.bcel.internal.generic.ALOAD;
@@ -16,6 +18,8 @@
1618
import com.sun.org.apache.bcel.internal.generic.DLOAD;
1719
import com.sun.org.apache.bcel.internal.generic.DSTORE;
1820
import com.sun.org.apache.bcel.internal.generic.GOTO;
21+
import com.sun.org.apache.bcel.internal.generic.IFLE;
22+
import com.sun.org.apache.bcel.internal.generic.IFLT;
1923
import com.sun.org.apache.bcel.internal.generic.IF_ICMPLT;
2024
import com.sun.org.apache.bcel.internal.generic.IINC;
2125
import com.sun.org.apache.bcel.internal.generic.ILOAD;
@@ -34,35 +38,100 @@
3438
import lambdacloud.net.CloudFuncHandler;
3539
import lambdacloud.net.CloudResp;
3640
import symjava.bytecode.BytecodeFunc;
41+
import symjava.relational.Gt;
42+
import symjava.relational.Lt;
43+
import symjava.relational.Relation;
3744
import symjava.symbolic.Expr;
45+
import symjava.symbolic.Symbol;
3846
import symjava.symbolic.utils.BytecodeUtils;
3947
import symjava.symbolic.utils.FuncClassLoader;
4048
import symjava.symbolic.utils.JIT;
49+
import symjava.symbolic.utils.Utils;
4150

4251
public class CloudLoop extends CloudBase {
43-
Expr condition;
44-
CloudVar[] condArgs;
45-
52+
Expr initExpr;
53+
Expr conditionExpr;
54+
Expr incrementExpr;
4655
List<Expr> bodyList = new ArrayList<Expr>();
47-
List<CloudVar[]> bodyArgs = new ArrayList<CloudVar[]>();
4856

4957
public CloudLoop(Expr conditionExpr) {
50-
58+
this.conditionExpr = conditionExpr;
5159
}
5260

5361
public CloudLoop(Expr initExpr, Expr conditionExpr) {
54-
62+
this.initExpr = initExpr;
63+
this.conditionExpr = conditionExpr;
5564
}
5665

5766
public CloudLoop(Expr initExpr, Expr conditionExpr, Expr incrementExpr) {
58-
67+
this.initExpr = initExpr;
68+
this.conditionExpr = conditionExpr;
69+
this.incrementExpr = incrementExpr;
5970
}
6071

6172
public CloudLoop appendBody(Expr expr) {
6273
return this;
6374
}
6475

65-
public CloudFunc compile() {
76+
public int declareLocal(Symbol var, MethodGen mg, InstructionList il) {
77+
//variable name
78+
//initial value
79+
//index in local table
80+
LocalVariableGen lg = mg.addLocalVariable(var.getLabel(),
81+
Type.DOUBLE, null, null);
82+
int idx = lg.getIndex();
83+
il.append(InstructionConstants.DCONST_0);
84+
lg.setStart(il.append(new DSTORE(idx)));
85+
return idx;
86+
}
87+
88+
@Override
89+
public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
90+
ConstantPoolGen cp, InstructionFactory factory,
91+
InstructionList il, Map<String, Integer> argsMap, int argsStartPos,
92+
Map<Expr, Integer> funcRefsMap) {
93+
if(!(conditionExpr instanceof Relation))
94+
throw new RuntimeException();
95+
Relation cond = (Relation)conditionExpr;
96+
97+
// Declare local variables
98+
List<Expr> allExprs = new ArrayList<Expr>();
99+
allExprs.add(initExpr);
100+
allExprs.add(conditionExpr);
101+
allExprs.add(incrementExpr);
102+
allExprs.addAll(bodyList);
103+
List<Expr> vars = Utils.extractSymbols(allExprs.toArray(new Expr[0]));
104+
for(Expr var : vars) {
105+
if(var instanceof Symbol) {
106+
Symbol s = (Symbol)var;
107+
int indexLVT = declareLocal(s, mg, il);
108+
s.setLVTIndex(indexLVT);
109+
}
110+
}
111+
112+
if(this.initExpr != null)
113+
this.initExpr.bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
114+
InstructionHandle loopStart = il.append(new NOP()); // Mark loop start position
115+
for(int i=0; i<bodyList.size(); i++) {
116+
Expr be = this.bodyList.get(i);
117+
be.bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
118+
}
119+
if(this.incrementExpr != null)
120+
this.incrementExpr.bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
121+
122+
InstructionHandle cmpStart = il.append(new NOP()); // Mark comparison start position
123+
if(cond instanceof Lt) { // l < r
124+
cond.lhs().bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
125+
cond.rhs().bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
126+
il.append(InstructionConstants.DCMPG);
127+
il.append(new IFLT(loopStart));
128+
} //else if (...)
129+
130+
131+
return il.insert(loopStart, new GOTO(cmpStart)); // goto comparison before the loop
132+
}
133+
134+
public BytecodeFunc compile(Expr[] args) {
66135
String packageName = "symjava.bytecode";
67136
String clsName = "CloudLoop" + java.util.UUID.randomUUID().toString().replaceAll("-", "");
68137
String fullClsName = packageName+"."+clsName;
@@ -82,83 +151,34 @@ public CloudFunc compile() {
82151
"apply", fullClsName, // method, class
83152
il, cp);
84153

85-
///////////////////////////////////
86-
//>>>>define local variables in condition and bodyList
87-
//double sum = 0;
88-
LocalVariableGen lg;
89-
lg = mg.addLocalVariable("sum",
90-
Type.DOUBLE, null, null);
91-
int idxSum = lg.getIndex();
92-
il.append(InstructionConstants.DCONST_0);
93-
lg.setStart(il.append(new DSTORE(idxSum))); // "sum" valid from here
94-
95-
//////////////////////////////////////////////////////////////
96-
// for(int i=0; i<10; i++) {
97-
// sum += args[i];
98-
// }
99-
/////////////////////////////////////////////////////////////
100-
//int i = 0;
101-
lg = mg.addLocalVariable("i",
102-
Type.INT, null, null);
103-
int idxI = lg.getIndex();
104-
il.append(InstructionConstants.ICONST_0);
105-
lg.setStart(il.append(new ISTORE(idxI))); // "i" valid from here
106-
107-
108-
InstructionHandle loopStart = il.append(new NOP()); // Mark loop start position
109-
for(int i=0; i<bodyList.size(); i++) {
110-
Expr be = bodyList.get(i);
111-
Expr beArgs = bodyArgs.get(i);
112-
BytecodeUtils.addToInstructionList(mg, cp, factory, il, argsIndex, be, beArgs, argsMap);
113-
154+
HashMap<String, Integer> argsMap = new HashMap<String, Integer>();
155+
if(args != null) {
156+
for(int i=0; i<args.length; i++) {
157+
argsMap.put(args[i].getLabel(), i);
158+
}
114159
}
115-
// //Loop body: sum = sum + args[i]
116-
// InstructionHandle loopStart = il.append(new ALOAD(1));
117-
// il.append(new ILOAD(idxI));
118-
// il.append(new DALOAD());
119-
// il.append(new DLOAD(idxSum));
120-
// il.append(new DADD());
121-
// il.append(new DSTORE(idxSum));
122-
123-
// //i++
124-
// il.append(new IINC(idxI, 1));
160+
this.bytecodeGen(fullClsName, mg, cp, factory, il, argsMap, 1, null);
125161

126-
// //Compare: i < 10
127-
// InstructionHandle loopCmp = il.append(new ILOAD(idxI));
128-
// il.append(new PUSH(cp, 10));
129-
// il.append(new IF_ICMPLT(loopStart));
130-
131-
//Mark loop compare
132-
InstructionHandle loopCmp = il.append(new NOP());
133-
//condition must be one of Lt,LE,Gt,Ge,Eq,Neq
134-
BytecodeUtils.addOthers(mg, cp, factory, il, argsIndex, condition, args, argsMap);
135-
136-
il.insert(loopStart, new GOTO(loopCmp));
137-
/////////////////////////////////////////////////////////////
138-
139-
il.append(new DLOAD(idxSum));
162+
il.append(InstructionConstants.DCONST_0);
140163
il.append(InstructionConstants.DRETURN);
141164

142165
mg.setMaxStack();
143166
cg.addMethod(mg.getMethod());
144167
il.dispose(); // Allow instruction handles to be reused
145168

146169
cg.addEmptyConstructor(ACC_PUBLIC);
147-
FuncClassLoader<BytecodeFunc> fcl = new FuncClassLoader<BytecodeFunc>();
148-
BytecodeFunc fun = fcl.newInstance(cg);
149-
double[] params = new double[10];
150-
for(int i=0; i<params.length; i++)
151-
params[i] = i;
152-
System.out.println(fun.apply(params));
153170

154171
try {
155172
cg.getJavaClass().dump("bin/symjava/bytecode/"+clsName+".class");
156173
} catch (java.io.IOException e) {
157174
System.err.println(e);
158175
}
176+
FuncClassLoader<BytecodeFunc> fcl = new FuncClassLoader<BytecodeFunc>();
177+
BytecodeFunc fun = fcl.newInstance(cg);
178+
return fun;
159179
}
160180

161-
public void apply(CloudVar ...inputs) {
181+
public void apply(CloudSharedVar ...inputs) {
162182

163183
}
164184
}
Collapse file

‎src/lambdacloud/core/CloudSharedVar.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudSharedVar.java
+12-12Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@
3131
* </pre></blockquote>
3232
*
3333
*/
34-
public class CloudVar extends Symbol {
34+
public class CloudSharedVar extends Symbol {
3535
double[] data = new double[0];
3636
boolean isOnCloud = false;
3737

38-
public CloudVar() {
38+
public CloudSharedVar() {
3939
super("CloudVar"+java.util.UUID.randomUUID().toString().replaceAll("-", ""));
4040
}
4141

42-
public CloudVar(String name) {
42+
public CloudSharedVar(String name) {
4343
super(name);
4444
}
4545

46-
public CloudVar(Expr expr) {
46+
public CloudSharedVar(Expr expr) {
4747
super("CloudVar"+java.util.UUID.randomUUID().toString().replaceAll("-", ""));
4848
this.compile(this.label, expr);
4949
}
5050

51-
public CloudVar(String name, Expr expr) {
51+
public CloudSharedVar(String name, Expr expr) {
5252
super(name);
5353
this.compile(name, expr);
5454
}
5555

56-
public CloudVar compile(String name, Expr expr) {
56+
public CloudSharedVar compile(String name, Expr expr) {
5757
if(CloudConfig.isLocal()) {
58-
CloudVar[] args = Utils.extractCloudVars(expr).toArray(new CloudVar[0]);
58+
CloudSharedVar[] args = Utils.extractCloudVars(expr).toArray(new CloudSharedVar[0]);
5959
BytecodeBatchFunc fexpr = JIT.compileBatchFunc(args, expr);
6060
data = new double[args[0].size()];
6161
fexpr.apply(data, 0, Utils.getDataFromCloudVars(args));
@@ -73,7 +73,7 @@ public CloudVar compile(String name, Expr expr) {
7373
* @param array
7474
* @return
7575
*/
76-
public CloudVar init(double ...array) {
76+
public CloudSharedVar init(double ...array) {
7777
this.data = array;
7878
return this;
7979
}
@@ -103,7 +103,7 @@ public double get(int index) {
103103
* @param size
104104
* @return
105105
*/
106-
public CloudVar resize(int size) {
106+
public CloudSharedVar resize(int size) {
107107
if(this.data == null)
108108
this.data = new double[size];
109109
else {
@@ -193,7 +193,7 @@ public boolean fetchToLocal() {
193193
e.printStackTrace();
194194
}
195195
CloudVarHandler h = client.getCloudVarHandler();
196-
CloudVar var = h.getCloudVar();
196+
CloudSharedVar var = h.getCloudVar();
197197
this.data = var.data;
198198
this.isOnCloud = var.isOnCloud();
199199
return this.isOnCloud;
@@ -226,8 +226,8 @@ public Expr diff(Expr expr) {
226226
return null;
227227
}
228228

229-
public static CloudVar valueOf(Expr expr) {
230-
return new CloudVar(expr);
229+
public static CloudSharedVar valueOf(Expr expr) {
230+
return new CloudSharedVar(expr);
231231
}
232232

233233
public Expr assign(Expr expr) {
Collapse file

‎src/lambdacloud/core/CloudStatements.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudStatements.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public CloudStatements append(Expr expr) {
1313
return this;
1414
}
1515

16-
public void apply(CloudVar ...inputs) {
16+
public void apply(CloudSharedVar ...inputs) {
1717

1818
}
1919
}
Collapse file

‎src/lambdacloud/core/CloudVar.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudVar.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import symjava.symbolic.Expr;
66
import symjava.symbolic.Symbol;
77

8-
public class CloudLocalVar extends Symbol {
8+
public class CloudVar extends Symbol {
99

10-
public CloudLocalVar(String name) {
10+
public CloudVar(String name) {
1111
super(name);
1212
this.isDeclaredAsLocal = true;
1313
}
Collapse file

‎src/lambdacloud/core/LC.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/LC.java
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public LC append(Expr expr) {
3636
return this;
3737
}
3838

39-
public CloudVar globalVar(String name) {
40-
return new CloudVar(name);
39+
public CloudSharedVar globalVar(String name) {
40+
return new CloudSharedVar(name);
4141
}
4242

43-
public CloudLocalVar localVar(String name) {
44-
return new CloudLocalVar(name);
43+
public CloudVar localVar(String name) {
44+
return new CloudVar(name);
4545
}
4646

4747
public void run() {
Collapse file

‎src/lambdacloud/core/operators/OPAsign.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/operators/OPAsign.java
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
package lambdacloud.core.operators;
22

3+
import java.util.Map;
4+
5+
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
6+
import com.sun.org.apache.bcel.internal.generic.DSTORE;
7+
import com.sun.org.apache.bcel.internal.generic.InstructionFactory;
8+
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
9+
import com.sun.org.apache.bcel.internal.generic.InstructionList;
10+
import com.sun.org.apache.bcel.internal.generic.MethodGen;
11+
312
import symjava.symbolic.Expr;
13+
import symjava.symbolic.Symbol;
414
import lambdacloud.core.CloudBase;
515

616
public class OPAsign extends CloudBase {
@@ -15,4 +25,15 @@ public void compile() {
1525

1626
}
1727

28+
@Override
29+
public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
30+
ConstantPoolGen cp, InstructionFactory factory,
31+
InstructionList il, Map<String, Integer> argsMap, int argsStartPos,
32+
Map<Expr, Integer> funcRefsMap) {
33+
if(!(lhs instanceof Symbol))
34+
throw new RuntimeException();
35+
Symbol var = (Symbol)lhs;
36+
rhs.bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
37+
return il.append(new DSTORE(var.getLVTIndex()));
38+
}
1839
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.