Skip to content

Commit 9012280

Browse files
committed
Improve constant calls for functions and static methods.
1 parent 078d6f5 commit 9012280

File tree

11 files changed

+573
-67
lines changed

11 files changed

+573
-67
lines changed

jphp-core/src/org/develnext/jphp/core/compiler/jvm/JvmCompiler.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public class JvmCompiler extends AbstractCompiler {
2929
protected final ModuleEntity module;
3030
protected NamespaceStmtToken namespace;
3131
protected List<DeclareStmtToken> declareStmtTokens = new ArrayList<>();
32-
private List<ClassStmtCompiler> classes = new ArrayList<ClassStmtCompiler>();
33-
private Map<String, ConstantEntity> constants = new LinkedHashMap<String, ConstantEntity>();
34-
private Map<String, FunctionEntity> functions = new LinkedHashMap<String, FunctionEntity>();
32+
private Map<String, ConstantEntity> constants = new LinkedHashMap<>();
33+
private Map<String, FunctionEntity> functions = new LinkedHashMap<>();
34+
private Map<String, ClassEntity> classes = new LinkedHashMap<>();
3535

3636
protected List<ConstStmtToken.Item> dynamicConstants = new ArrayList<>();
3737

@@ -46,7 +46,6 @@ public JvmCompiler(Environment environment, Context context) throws IOException
4646

4747
public JvmCompiler(Environment environment, Context context, SyntaxAnalyzer analyzer) {
4848
super(environment, context);
49-
this.classes = new ArrayList<>();
5049
this.module = new ModuleEntity(context);
5150
this.module.setId( scope.nextModuleIndex() );
5251

@@ -66,6 +65,10 @@ public FunctionEntity findFunction(String name){
6665
return functions.get(name.toLowerCase());
6766
}
6867

68+
public ClassEntity findClass(String name){
69+
return classes.get(name.toLowerCase());
70+
}
71+
6972
public NamespaceStmtToken getNamespace() {
7073
return namespace;
7174
}
@@ -193,6 +196,9 @@ public List<ExprStmtToken> process(List<Token> tokens, NamespaceStmtToken namesp
193196
} if (token instanceof ClassStmtToken){
194197
ClassStmtCompiler cmp = new ClassStmtCompiler(this, (ClassStmtToken)token);
195198
ClassEntity entity = cmp.compile();
199+
200+
classes.put(entity.getLowerName(), entity);
201+
196202
entity.setStatic(true);
197203
module.addClass(entity);
198204

@@ -234,7 +240,6 @@ public List<ExprStmtToken> process(List<Token> tokens, NamespaceStmtToken namesp
234240

235241
@Override
236242
public ModuleEntity compile(boolean autoRegister) {
237-
this.classes = new ArrayList<>();
238243
module.setInternalName("$php_module_m" + UUID.randomUUID().toString().replace("-", ""));
239244

240245
List<ExprStmtToken> externalCode = process(tokens, NamespaceStmtToken.getDefault());
@@ -307,10 +312,6 @@ public ModuleEntity getModule() {
307312
return module;
308313
}
309314

310-
public List<ClassStmtCompiler> getClasses() {
311-
return classes;
312-
}
313-
314315
public String getSourceFile() {
315316
return context.getFileName();
316317
}

jphp-core/src/org/develnext/jphp/core/compiler/jvm/statement/ExpressionStmtCompiler.java

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.develnext.jphp.core.compiler.jvm.statement.expr.operator.DynamicAccessCompiler;
1010
import org.develnext.jphp.core.compiler.jvm.statement.expr.operator.InstanceOfCompiler;
1111
import org.develnext.jphp.core.compiler.jvm.statement.expr.value.*;
12+
import org.develnext.jphp.core.compiler.jvm.statement.expr.value.CallCompiler.PushCallStatistic;
1213
import org.develnext.jphp.core.tokenizer.TokenMeta;
1314
import org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken;
1415
import org.develnext.jphp.core.tokenizer.token.Token;
@@ -73,8 +74,10 @@ public class ExpressionStmtCompiler extends StmtCompiler {
7374
private Map<Class<? extends Token>, BaseStatementCompiler> compilers;
7475
private static final Map<Class<? extends Token>, Class<? extends BaseStatementCompiler>> compilerRules;
7576

77+
private CallCompiler callCompiler = new CallCompiler(this);
78+
7679
static {
77-
compilerRules = new HashMap<Class<? extends Token>, Class<? extends BaseStatementCompiler>>();
80+
compilerRules = new HashMap<>();
7881
compilerRules.put(IfStmtToken.class, IfElseCompiler.class);
7982
compilerRules.put(SwitchStmtToken.class, SwitchCompiler.class);
8083
compilerRules.put(FunctionStmtToken.class, FunctionCompiler.class);
@@ -130,6 +133,8 @@ public class ExpressionStmtCompiler extends StmtCompiler {
130133
compilerRules.put(ListExprToken.class, ListCompiler.class);
131134
compilerRules.put(YieldExprToken.class, YieldValueCompiler.class);
132135

136+
//compilerRules.put(CallExprToken.class, CallCompiler.class);
137+
133138
// operation
134139
compilerRules.put(InstanceofExprToken.class, InstanceOfCompiler.class);
135140
compilerRules.put(DynamicAccessAssignExprToken.class, DynamicAccessCompiler.class);
@@ -198,7 +203,7 @@ public <T extends ValueExprToken> T writeValue(T token, boolean returnValue) {
198203
@SuppressWarnings("unchecked")
199204
public <T extends Token> BaseStatementCompiler<T> getCompiler(Class<T> clazz) {
200205
if (compilers == null) {
201-
compilers = new HashMap<Class<? extends Token>, BaseStatementCompiler>();
206+
compilers = new HashMap<>();
202207
}
203208

204209
BaseStatementCompiler<T> r = compilers.get(clazz);
@@ -250,7 +255,7 @@ public LabelNode makeLabel() {
250255
return el;
251256
}
252257

253-
protected Memory getMacros(ValueExprToken token) {
258+
public Memory getMacros(ValueExprToken token) {
254259
if (token instanceof SelfExprToken) {
255260
if (method.clazz.entity.isTrait()) {
256261
throw new IllegalArgumentException("Cannot use this in Traits");
@@ -881,7 +886,7 @@ public void writePushStaticCall(Method method) {
881886
);
882887
}
883888

884-
Memory writePushCompileFunction(CallExprToken function, CompileFunction compileFunction, boolean returnValue,
889+
public Memory writePushCompileFunction(CallExprToken function, CompileFunction compileFunction, boolean returnValue,
885890
boolean writeOpcode, PushCallStatistic statistic) {
886891
CompileFunction.Method method = compileFunction.find(function.getParameters().size());
887892
if (method == null) {
@@ -1178,7 +1183,7 @@ public void writePushParameters(Collection<ExprStmtToken> parameters, boolean us
11781183
}
11791184
}
11801185

1181-
Memory writePushParentDynamicMethod(CallExprToken function, boolean returnValue, boolean writeOpcode,
1186+
public Memory writePushParentDynamicMethod(CallExprToken function, boolean returnValue, boolean writeOpcode,
11821187
PushCallStatistic statistic) {
11831188
if (!writeOpcode)
11841189
return null;
@@ -1222,7 +1227,7 @@ Memory writePushParentDynamicMethod(CallExprToken function, boolean returnValue,
12221227
return null;
12231228
}
12241229

1225-
Memory writePushDynamicMethod(CallExprToken function, boolean returnValue, boolean writeOpcode,
1230+
/*public Memory writePushDynamicMethod(CallExprToken function, boolean returnValue, boolean writeOpcode,
12261231
PushCallStatistic statistic) {
12271232
if (!writeOpcode)
12281233
return null;
@@ -1247,9 +1252,9 @@ Memory writePushDynamicMethod(CallExprToken function, boolean returnValue, boole
12471252
statistic.returnType = StackItem.Type.REFERENCE;
12481253
12491254
return null;
1250-
}
1255+
}*/
12511256

1252-
boolean writePushFastStaticMethod(CallExprToken function, boolean returnValue) {
1257+
public boolean writePushFastStaticMethod(CallExprToken function, boolean returnValue) {
12531258
StaticAccessExprToken access = (StaticAccessExprToken) function.getName();
12541259

12551260
CompileClass compileClass = compiler.getEnvironment().scope.findCompileClass(access.getClazz().getWord());
@@ -1295,7 +1300,7 @@ else if (!additional.isEmpty())
12951300
return false;
12961301
}
12971302

1298-
Memory writePushStaticMethod(CallExprToken function, boolean returnValue, boolean writeOpcode,
1303+
/*public Memory writePushStaticMethod(CallExprToken function, boolean returnValue, boolean writeOpcode,
12991304
PushCallStatistic statistic) {
13001305
StaticAccessExprToken access = (StaticAccessExprToken) function.getName();
13011306
@@ -1307,22 +1312,45 @@ Memory writePushStaticMethod(CallExprToken function, boolean returnValue, boolea
13071312
if (writePushFastStaticMethod(function, returnValue))
13081313
return null;
13091314
1310-
writePushEnv();
1311-
writePushTraceInfo(function.getName());
1315+
Runnable writeParams = () -> {
1316+
writePushEnv();
1317+
writePushTraceInfo(function.getName());
1318+
};
1319+
13121320
String methodName = null;
13131321
13141322
ValueExprToken clazz = access.getClazz();
13151323
if ((clazz instanceof NameToken || (clazz instanceof SelfExprToken && !method.clazz.entity.isTrait()))
13161324
&& access.getField() instanceof NameToken) {
13171325
String className;
1318-
if (clazz instanceof SelfExprToken)
1326+
if (clazz instanceof SelfExprToken) {
13191327
className = getMacros(clazz).toString();
1320-
else
1328+
} else {
13211329
className = ((NameToken) clazz).getName();
1330+
}
1331+
1332+
methodName = ((NameToken) access.getField()).getName();
1333+
1334+
ClassEntity localClass = compiler.findClass(className);
1335+
if (localClass != null) {
1336+
MethodEntity localClassMethod = localClass.findMethod(methodName.toLowerCase());
1337+
if (localClassMethod != null && localClassMethod.isLikeConstant()) {
1338+
if (returnValue) {
1339+
writePushMemory(localClassMethod.getResult());
1340+
1341+
if (statistic != null) {
1342+
statistic.returnType = StackItem.Type.REFERENCE;
1343+
}
1344+
}
1345+
1346+
return null;
1347+
}
1348+
}
1349+
1350+
writeParams.run();
13221351
13231352
writePushString(className.toLowerCase());
13241353
1325-
methodName = ((NameToken) access.getField()).getName();
13261354
writePushString(methodName.toLowerCase());
13271355
13281356
writePushString(className);
@@ -1348,6 +1376,8 @@ Memory writePushStaticMethod(CallExprToken function, boolean returnValue, boolea
13481376
MethodCallCache.class, Integer.TYPE
13491377
);
13501378
} else {
1379+
writeParams.run();
1380+
13511381
if (clazz instanceof NameToken) {
13521382
writePushString(((NameToken) clazz).getName());
13531383
writePushDupLowerCase();
@@ -1401,17 +1431,15 @@ Memory writePushStaticMethod(CallExprToken function, boolean returnValue, boolea
14011431
MethodCallCache.class, Integer.TYPE
14021432
);
14031433
}
1404-
if (statistic != null)
1434+
1435+
if (statistic != null) {
14051436
statistic.returnType = StackItem.Type.REFERENCE;
1437+
}
14061438
14071439
return null;
1408-
}
1409-
1410-
public static class PushCallStatistic {
1411-
public StackItem.Type returnType = StackItem.Type.REFERENCE;
1412-
}
1440+
}*/
14131441

1414-
Memory writePushCall(CallExprToken function, boolean returnValue, boolean writeOpcode,
1442+
/*Memory writePushCall(CallExprToken function, boolean returnValue, boolean writeOpcode,
14151443
PushCallStatistic statistic) {
14161444
Token name = function.getName();
14171445
if (name instanceof NameToken) {
@@ -1481,8 +1509,9 @@ Memory writePushCall(CallExprToken function, boolean returnValue, boolean writeO
14811509
if (!returnValue)
14821510
writePopAll(1);
14831511
}
1512+
14841513
return null;
1485-
}
1514+
}*/
14861515

14871516
/*public void writeDefineVariables(Collection<VariableExprToken> values) {
14881517
for (VariableExprToken value : values) {
@@ -2197,7 +2226,10 @@ else if (value instanceof NameToken)
21972226
return true;
21982227
else if (value instanceof CallExprToken) {
21992228
PushCallStatistic statistic = new PushCallStatistic();
2200-
writePushCall((CallExprToken) value, true, false, statistic);
2229+
2230+
callCompiler.write((CallExprToken) value, true, false, statistic);
2231+
2232+
//writePushCall((CallExprToken) value, true, false, statistic);
22012233
return statistic.returnType.isConstant();
22022234
}
22032235

@@ -2221,7 +2253,8 @@ else if (value instanceof StringBuilderExprToken)
22212253
return StackItem.Type.STRING;
22222254
else if (value instanceof CallExprToken) {
22232255
PushCallStatistic statistic = new PushCallStatistic();
2224-
writePushCall((CallExprToken) value, true, false, statistic);
2256+
callCompiler.write((CallExprToken) value, true, false, statistic);
2257+
//writePushCall((CallExprToken) value, true, false, statistic);
22252258
return statistic.returnType;
22262259
} else if (value instanceof NameToken) {
22272260
Memory tmpMemory = writePushName((NameToken) value, true, false);
@@ -2249,7 +2282,8 @@ Memory tryWritePush(ValueExprToken value, boolean returnValue, boolean writeOpco
22492282
if (value instanceof ArrayExprToken) {
22502283
return writePushArray((ArrayExprToken) value, returnValue, writeOpcode);
22512284
} else if (value instanceof CallExprToken) {
2252-
return writePushCall((CallExprToken) value, returnValue, writeOpcode, null);
2285+
return callCompiler.write((CallExprToken) value, returnValue, writeOpcode, null);
2286+
//return writePushCall((CallExprToken) value, returnValue, writeOpcode, null);
22532287
} else if (value instanceof MacroToken) {
22542288
return tryWritePushMacro((MacroToken) value, writeOpcode);
22552289
} else if (value instanceof VariableExprToken) {
@@ -2284,7 +2318,7 @@ boolean methodExists(Class clazz, String method, Class... paramClasses) {
22842318
}
22852319
}
22862320

2287-
void writeSysCall(String internalClassName, int INVOKE_TYPE, String method, Class returnClazz, Class... paramClasses) {
2321+
public void writeSysCall(String internalClassName, int INVOKE_TYPE, String method, Class returnClazz, Class... paramClasses) {
22882322
Type[] args = new Type[paramClasses.length];
22892323
if (INVOKE_TYPE == INVOKEVIRTUAL || INVOKE_TYPE == INVOKEINTERFACE)
22902324
stackPop(); // this
@@ -2800,14 +2834,17 @@ public void writeDynamicAccessInfo(DynamicAccessExprToken dynamic, boolean addLo
28002834
}
28012835

28022836
public void writeDynamicAccessPrepare(DynamicAccessExprToken dynamic, boolean addLowerName) {
2803-
if (stackEmpty(true))
2837+
if (stackEmpty(true)) {
28042838
unexpectedToken(dynamic);
2839+
}
28052840

28062841
StackItem o = stackPop();
28072842
writePush(o);
28082843

2809-
if (stackPeek().isConstant())
2844+
if (stackPeek().isConstant()) {
28102845
unexpectedToken(dynamic);
2846+
}
2847+
28112848
writePopBoxing();
28122849

28132850
if (dynamic instanceof DynamicAccessAssignExprToken) {

0 commit comments

Comments
 (0)