From: Doni Pracner Date: Tue, 22 Apr 2014 20:17:55 +0000 (+0200) Subject: Merge branch 'vars' into work X-Git-Tag: v0.1.6~7 X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=commitdiff_plain;h=efe68ac5eb23f8a7711a44c4a7d4e830b64cd068;hp=5c55fb61ab79584c65492755dab20935474ed15c Merge branch 'vars' into work --- diff --git a/docs/mjc2wsl.txt b/docs/mjc2wsl.txt index 27584dc..788e0bc 100644 --- a/docs/mjc2wsl.txt +++ b/docs/mjc2wsl.txt @@ -65,5 +65,15 @@ work. Note however that the language specification is not allowing direct access to the memory, neither is the bytecode actually working directly with the heap, and that array and object access are done by specialised commands. -Still there is an option for using indexes out of bounds -that could produce unexpected behaviour. \ No newline at end of file +Still there is an option for using fields beyond the actuall +object that could produce unexpected behaviour. +Array access in the MJVM does check for indexe out of bounds +errors. + +The size of the heap is not taken into consideration when +allocating new arrays and objects. What this means is that +there will be no heap overflow exception thrown, since our +heap is seemingly endless. + +Null access does not throw explicit errors, although it +will cause problems with the list access. \ No newline at end of file diff --git a/src-wsl/transf-exp.wsl b/src-wsl/transf-exp.wsl index 5b1bd10..1bffdbd 100755 --- a/src-wsl/transf-exp.wsl +++ b/src-wsl/transf-exp.wsl @@ -20,7 +20,11 @@ License along with this program. If not, see C:"Automatic transformation tool for simplification of WSL"; C:"code automaticaly translated from assembly using asm2wsl."; -C:"a simple version"; +C:"experimental version"; + +C:"set the following so that Action Systems are not automaticaly treated as regular"; +Assume_A_S_Regular := 0; + MW_PROC @Process_Prog() == FOREACH Statement DO IF @Trans?(TR_Push_Pop) THEN @@ -31,6 +35,7 @@ MW_PROC @Process_Prog() == FOREACH Statement DO IF @ST(@I) = T_A_S THEN C:"don't need to test for this, works for T_A_S"; + @Trans(TR_Simplify_Action_System, ""); IF @Trans?(TR_Collapse_Action_System) THEN @Trans(TR_Collapse_Action_System, ""); FI; @@ -40,12 +45,23 @@ MW_PROC @Process_Prog() == @Delete FI OD; + FOREACH Statement DO + IF @Trans?(TR_Flag_Removal) THEN + @Trans(TR_Flag_Removal, ""); + FI + OD; C:"remove all the comments "; FOREACH Statement DO IF @ST(@I) = T_Comment THEN @Delete FI OD; + FOREACH Statement DO + IF @Trans?(TR_Simplify_Item) THEN + @Trans(TR_Simplify_Item,"") + FI + OD; + C:"Convert DO loops into WHILE loops"; FOREACH Statement DO IF @Trans?(TR_Floop_To_While) THEN @@ -74,10 +90,10 @@ IF @File_Exists?(inifilename) THEN @Process_Prog(); Get_New_Name(VAR filename); @PP_Item(@Program, 80, filename); - Prog_Stat(prog); - PRINT(""); - PRINT("After Conversion"); - Prog_Stat(@Program) + PRINT(""); + PRINT(filename); + Prog_Stat_Comp(prog, @Program); + PRINT("") FI; filename := @Read_Line(inifile) OD; @@ -106,6 +122,27 @@ PROC Prog_Stat(Pro VAR)== SKIP END +PROC Prog_Stat_Comp(Pro, After VAR)== + VAR < ma := 0, mb :=1 > : + ma := @McCabe(Pro); + mb := @McCabe(After); + PRINT ("McCabe ", ma, " ", mb, " ",(mb-ma)); + ma := @Stat_Count(Pro); + mb := @Stat_Count(After); + PRINT ("Statem ", ma, " ",mb, " ",(mb-ma)); + ma := @CFDF_Metric(Pro); + mb := @CFDF_Metric(After); + PRINT ("CF/DF ", ma," ", mb," ", (mb-ma)); + ma := @Total_Size(Pro); + mb := @Total_Size(After); + PRINT ("Size ", ma," ", mb, " ",(mb-ma)); + ma := @Struct_Metric(Pro); + mb := @Struct_Metric(After); + PRINT ("Struct ", ma, " ",mb, " ",(mb-ma)); + SKIP + ENDVAR +END + PROC Get_New_Name(VAR str) == IF @Ends_With?(str, ".wsl") THEN str := SUBSTR(str, 0, SLENGTH(str)-4) diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index 44c0c67..388f1ad 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -193,7 +193,7 @@ public class mjc2wsl{ +"C:\" with mjc2wsl v "+versionN+"\";\n"); ret.append("BEGIN "); - ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t"); + ret.append("VAR < \n\t"); ret.append("mjvm_locals := ARRAY(1,0), "); ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0), "); ret.append("\n\tmjvm_arrays := < >, "); @@ -219,6 +219,20 @@ public class mjc2wsl{ return ret.toString(); } + private String createStartVar(String... vars){ + StringBuilder ret = new StringBuilder("VAR < "); + ret.append(vars[0] + " := 0"); + for (int i=1; i : "); + + return ret.toString(); + } + + private String createEndVar(){ + return "ENDVAR;"; + } + private String createLocal(int i) { // arrays start at 1 in WSL, so we need an offset return "mjvm_locals[" + (i + 1) + "]"; @@ -415,41 +429,53 @@ public class mjc2wsl{ } case add: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("tempres := tempb + tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case sub: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("tempres := tempb - tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case mul: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("tempres := tempb * tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case div: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;"); prl("tempres := tempb DIV tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case rem: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;"); prl("tempres := tempb MOD tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case neg: { + prl(createStartVar("tempa")); prl(createTopEStack()); prl(createToEStack("-tempa")); + prl(createEndVar()); break; } @@ -547,10 +573,13 @@ public class mjc2wsl{ case jle: case jgt: case jge: { + prl(createStartVar("tempa", "tempb")); prl(createTopTwoEStack()); prl("IF tempb " + getRelationFor(op) + " tempa THEN CALL a" + (counter + get2()) + " ELSE CALL a" + (counter + 1) + " FI;"); + prl(createEndVar()); + break; } @@ -587,8 +616,10 @@ public class mjc2wsl{ prl(createComment("char is read like a number", C_SPEC)); } case read: { + prl(createStartVar("tempa")); prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));"); prl(createToEStack("tempa")); + prl(createEndVar()); break; } @@ -601,8 +632,11 @@ public class mjc2wsl{ } case print: { // TODO printing numbers needs different lengths of spacing + prl(createStartVar("tempa", "tempb")); + prl(createTopTwoEStack()); prl("Print_MJ(tempb,tempa);"); + prl(createEndVar()); break; }