Skip to content

Commit

Permalink
Merge pull request #203 from jphp-compiler/stage-0.7.0
Browse files Browse the repository at this point in the history
Stage-0.7.0
  • Loading branch information
dim-s committed Jun 19, 2015
2 parents 3608c02 + bfef706 commit 53247cd
Show file tree
Hide file tree
Showing 142 changed files with 3,068 additions and 423 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def javaVersion = '1.7';
def projectVersion = '0.6.11';
def projectVersion = '0.7.0';

buildscript {
repositories {
Expand All @@ -18,7 +18,7 @@ allprojects {

project.group = 'org.develnext'
project.version = projectVersion
project.ext.isSnapshot = System.getProperty("release", "0") in ["0", "false"];
project.ext.isSnapshot = !Boolean.getBoolean("release");

if (project.ext.isSnapshot) {
version += '-SNAPSHOT'
Expand Down
149 changes: 149 additions & 0 deletions jphp-core/src/org/develnext/jphp/core/common/ObjectSizeCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package org.develnext.jphp.core.common;


import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

/**
* @author Kyrylo Holodnov
*/
public class ObjectSizeCalculator {
private static final int REFERENCE_SIZE;
private static final int HEADER_SIZE;
private static final int LONG_SIZE = 8;
private static final int INT_SIZE = 4;
private static final int BYTE_SIZE = 1;
private static final int BOOLEAN_SIZE = 1;
private static final int CHAR_SIZE = 2;
private static final int SHORT_SIZE = 2;
private static final int FLOAT_SIZE = 4;
private static final int DOUBLE_SIZE = 8;
private static final int ALIGNMENT = 8;

static {
try {
if (System.getProperties().get("java.vm.name").toString().contains("64")) {
//java.vm.name is something like "Java HotSpot(TM) 64-Bit Server VM"
REFERENCE_SIZE = 8;
HEADER_SIZE = 16;
} else {
REFERENCE_SIZE = 4;
HEADER_SIZE = 8;
}
} catch (Exception ex) {
ex.printStackTrace();
throw new AssertionError(ex);
}
}

public static long sizeOf(Object o) throws IllegalAccessException {
return sizeOf(o, new HashSet());
}

private static long sizeOf(Object o, Set visited) throws IllegalAccessException {
if (o == null) {
return 0;
}
ObjectWrapper objectWrapper = new ObjectWrapper(o);
if (visited.contains(objectWrapper)) {
//We have reference graph with cycles.
return 0;
}
visited.add(objectWrapper);
long size = HEADER_SIZE;
Class clazz = o.getClass();
if (clazz.isArray()) {
if (clazz == long[].class) {
long[] objs = (long[]) o;
size += objs.length * LONG_SIZE;
} else if (clazz == int[].class) {
int[] objs = (int[]) o;
size += objs.length * INT_SIZE;
} else if (clazz == byte[].class) {
byte[] objs = (byte[]) o;
size += objs.length * BYTE_SIZE;
} else if (clazz == boolean[].class) {
boolean[] objs = (boolean[]) o;
size += objs.length * BOOLEAN_SIZE;
} else if (clazz == char[].class) {
char[] objs = (char[]) o;
size += objs.length * CHAR_SIZE;
} else if (clazz == short[].class) {
short[] objs = (short[]) o;
size += objs.length * SHORT_SIZE;
} else if (clazz == float[].class) {
float[] objs = (float[]) o;
size += objs.length * FLOAT_SIZE;
} else if (clazz == double[].class) {
double[] objs = (double[]) o;
size += objs.length * DOUBLE_SIZE;
} else {
Object[] objs = (Object[]) o;
for (int i = 0; i < objs.length; i++) {
size += sizeOf(objs[i], visited) + REFERENCE_SIZE;
}
}
size += INT_SIZE;
} else {
Field[] fields = o.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (java.lang.reflect.Modifier.isStatic(fields[i].getModifiers())) {
continue;
}
fields[i].setAccessible(true);
String fieldType = fields[i].getGenericType().toString();
if (fieldType.equals("long")){
size += LONG_SIZE;
}else if (fieldType.equals("int")){
size += INT_SIZE;
}else if (fieldType.equals("byte")){
size += BYTE_SIZE;
}else if (fieldType.equals("boolean")){
size += BOOLEAN_SIZE;
}else if (fieldType.equals("char")){
size += CHAR_SIZE;
}else if (fieldType.equals("short")){
size += SHORT_SIZE;
}else if (fieldType.equals("float")){
size += FLOAT_SIZE;
}else if (fieldType.equals("double")){
size += DOUBLE_SIZE;
}else{
size += sizeOf(fields[i].get(o), visited) + REFERENCE_SIZE;
}
}
}
if ((size % ALIGNMENT) != 0) {
size = ALIGNMENT * (size / ALIGNMENT) + ALIGNMENT;
}
return size;
}

private static final class ObjectWrapper {

private Object object;

public ObjectWrapper(Object object) {
this.object = object;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if ((obj == null) || (obj.getClass() != ObjectWrapper.class)) {
return false;
}
return object == ((ObjectWrapper) obj).object;
}

@Override
public int hashCode() {
int hash = 3;
hash = 47 * hash + System.identityHashCode(object);
return hash;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,33 +279,21 @@ protected void writeConstructor() {
if (isClosure() || generatorEntity != null) {
constructor.desc = Type.getMethodDescriptor(
Type.getType(void.class),
Type.getType(Environment.class),
Type.getType(ClassEntity.class),
Type.getType(Memory.class),
Type.getType(Memory[].class)
);

if (generatorEntity != null) {
constructor.desc = Type.getMethodDescriptor(
Type.getType(void.class),
Type.getType(Environment.class),
Type.getType(ClassEntity.class),
Type.getType(Memory.class),
Type.getType(Memory[].class)
);
methodCompiler.addLocalVariable("~env", l0, Environment.class);
}

methodCompiler.addLocalVariable("~env", l0, Environment.class);
methodCompiler.addLocalVariable("~class", l0, ClassEntity.class);
methodCompiler.addLocalVariable("~self", l0, Memory.class);
methodCompiler.addLocalVariable("~uses", l0, Memory[].class);

methodCompiler.writeHeader();
expressionCompiler.writeVarLoad("~this");

if (generatorEntity != null) {
expressionCompiler.writeVarLoad("~env");
}

expressionCompiler.writeVarLoad("~env");
expressionCompiler.writeVarLoad("~class");
expressionCompiler.writeVarLoad("~self");
expressionCompiler.writeVarLoad("~uses");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void write(ClosureStmtToken closure, boolean returnValue) {
if (returnValue){
ClosureEntity entity = compiler.getModule().findClosure( closure.getId() );
boolean thisExists = closure.getFunction().isThisExists();
if (closure.getFunction().getUses().isEmpty() && !thisExists
boolean staticExists = closure.getFunction().isStaticExists();

if (closure.getFunction().getUses().isEmpty() && !thisExists && !staticExists
&& closure.getFunction().getStaticLocal().isEmpty()){
expr.writePushEnv();
expr.writePushConstString(compiler.getModule().getInternalName());
Expand All @@ -79,6 +81,7 @@ public void write(ClosureStmtToken closure, boolean returnValue) {
expr.stackPush(Memory.Type.REFERENCE);
expr.writePushDup();

expr.writePushEnv();
expr.writePushEnv();
expr.writePushConstString(compiler.getModule().getInternalName());
expr.writePushConstInt((int) entity.getId());
Expand All @@ -94,14 +97,15 @@ public void write(ClosureStmtToken closure, boolean returnValue) {
INVOKESPECIAL, entity.getInternalName(), Constants.INIT_METHOD,
Type.getMethodDescriptor(
Type.getType(void.class),
Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(Memory[].class)
Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(Memory[].class)
),
false
));
expr.stackPop();
expr.stackPop();
expr.stackPop();
expr.stackPop();
expr.stackPop();

expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
}
Expand Down
14 changes: 12 additions & 2 deletions jphp-core/src/org/develnext/jphp/core/syntax/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.*;

public class Scope {
protected boolean staticExists = false;
protected Set<VariableExprToken> variables;
protected Map<String, LabelStmtToken> labels;
protected final Scope parent;
Expand All @@ -14,13 +15,14 @@ public class Scope {

public Scope(Scope parent) {
this.parent = parent;
variables = new HashSet<VariableExprToken>();
labels = new HashMap<String, LabelStmtToken>();
variables = new HashSet<>();
labels = new HashMap<>();
}

public void appendScope(Scope scope) {
variables.addAll(scope.getVariables());
labels.putAll(scope.getLabels());
staticExists = staticExists || scope.staticExists;
}

public Set<VariableExprToken> getVariables() {
Expand All @@ -44,6 +46,14 @@ public Map<String, LabelStmtToken> getLabels() {
return labels;
}

public boolean isStaticExists() {
return staticExists;
}

public void setStaticExists(boolean staticExists) {
this.staticExists = staticExists;
}

public boolean isLevelForGoto() {
return levelForGoto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ClassGenerator(SyntaxAnalyzer analyzer) {
}

protected void processName(ClassStmtToken result, ListIterator<Token> iterator){
Token name = nextToken(iterator);
Token name = nextTokenSensitive(iterator);
if (name instanceof NameToken){
result.setName((NameToken)name);
if (name instanceof ParentExprToken)
Expand Down Expand Up @@ -222,13 +222,21 @@ protected void processBlock(ClassStmtToken result, NameToken firstTraitName, Lis
unexpectedToken(iterator.previous());

while (true) {
NameToken className = nextAndExpected(iterator, NameToken.class);
NameToken className = nextAndExpectedSensitive(iterator, NameToken.class);
NameToken methodName;

Token token = nextToken(iterator);
if (token instanceof StaticAccessExprToken) {
className = analyzer.getRealName(className);
methodName = nextAndExpected(iterator, NameToken.class);

Token nextTokenSensitive = nextTokenSensitive(iterator);

if (nextTokenSensitive instanceof NameToken) {
methodName = (NameToken) nextTokenSensitive;
} else {
unexpectedToken(nextTokenSensitive);
return;
}
} else {
iterator.previous();
methodName = className;
Expand All @@ -240,7 +248,8 @@ protected void processBlock(ClassStmtToken result, NameToken firstTraitName, Lis

Token what = nextToken(iterator);
if (what instanceof AsStmtToken) {
Token one = nextToken(iterator);
Token one = nextTokenSensitive(iterator, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class, FinalStmtToken.class, StaticExprToken.class);

if (one instanceof NameToken) {
result.addAlias(
className.getName(),
Expand All @@ -254,7 +263,7 @@ protected void processBlock(ClassStmtToken result, NameToken firstTraitName, Lis
else if (one instanceof PublicStmtToken)
modifier = Modifier.PUBLIC;

token = nextToken(iterator);
token = nextTokenSensitive(iterator);
String name = null;
if (token instanceof NameToken) {
NameToken two = (NameToken)token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator;
import org.develnext.jphp.core.tokenizer.TokenType;
import org.develnext.jphp.core.tokenizer.token.Token;
import org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken;
import org.develnext.jphp.core.tokenizer.token.expr.CommaToken;
import org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken;
import org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken;
import org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.ImportExprToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.NameToken;
import org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken;
import org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken;
import org.develnext.jphp.core.tokenizer.token.stmt.*;

import java.util.ListIterator;

Expand Down Expand Up @@ -42,6 +43,7 @@ protected void processBody(ConstStmtToken result, ListIterator<Token> iterator){
}*/

@Override
@SuppressWarnings("unchecked")
public ConstStmtToken getToken(Token current, ListIterator<Token> iterator) {
if (current instanceof ConstStmtToken){
ConstStmtToken result = (ConstStmtToken)current;
Expand All @@ -50,8 +52,9 @@ public ConstStmtToken getToken(Token current, ListIterator<Token> iterator) {
if (analyzer.getClazz() == null)
result.setNamespace(analyzer.getNamespace());

while (true){
Token next = nextToken(iterator);
while (true) {
Token next = analyzer.getClazz() == null ? nextToken(iterator) : nextTokenSensitive(iterator, ClassStmtToken.class);

if (next instanceof NameToken){
if (next instanceof FulledNameToken && !((FulledNameToken) next).isProcessed())
unexpectedToken(next, TokenType.T_STRING);
Expand Down
Loading

0 comments on commit 53247cd

Please sign in to comment.