Skip to content

Commit

Permalink
Feature/refactor data flow analyses broken arrays map 2 (#138)
Browse files Browse the repository at this point in the history
* Attempt to fix arrays/arrays-maps-2

* Switch order of stuff

* Actually fix the thing

* Cleanup JavaWriter Constants

* Fix methodDescriptor for generic expansion methods in generic expansions

* Add Test for accessing nested classes

---------

Co-authored-by: Witixin1512 <[email protected]>
Co-authored-by: Silk <[email protected]>
Co-authored-by: kindlich <[email protected]>
  • Loading branch information
4 people authored May 17, 2024
1 parent f9dafe8 commit 17d8d35
Show file tree
Hide file tree
Showing 19 changed files with 466 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import org.openzen.zencode.shared.CompileError;
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.GenericName;
import org.openzen.zenscript.codemodel.VariableDefinition;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalExpression;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;
import org.openzen.zenscript.codemodel.statement.VarStatement;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.List;
Expand All @@ -19,6 +18,8 @@ public interface ExpressionCompiler extends TypeResolver {

Optional<TypeID> getThisType();

Optional<CompilingExpression> getThis(CodePosition position);

Optional<LocalType> getLocalType();

Optional<TypeID> getThrowableType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.openzen.zenscript.codemodel.compilation.impl.capture;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.compilation.CompilingExpression;
import org.openzen.zenscript.codemodel.compilation.ExpressionCompiler;
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;
import org.openzen.zenscript.codemodel.type.TypeID;

public class LocalThisExpression implements LocalExpression {
private final CodePosition position;
private final TypeID type;

public LocalThisExpression(CodePosition position, TypeID type) {
this.position = position;
this.type = type;
}

@Override
public LocalExpression capture(LambdaClosure closure) {
CapturedExpression value = new CapturedThisExpression(position, type, closure);
closure.add(value);
return new LocalCapturedExpression(value);
}

@Override
public CompilingExpression compile(ExpressionCompiler compiler) {
return new Compiling(compiler, position, type);
}

private static class Compiling extends AbstractCompilingExpression {
private final TypeID type;

public Compiling(ExpressionCompiler compiler, CodePosition position, TypeID type) {
super(compiler, position);

this.type = type;
}

@Override
public Expression eval() {
return compiler.at(position).getThis(type);
}

@Override
public CompilingExpression assign(CompilingExpression value) {
return this;
}

@Override
public void collect(SSAVariableCollector collector) {}

@Override
public void linkVariables(CodeBlockStatement.VariableLinker linker) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.openzen.zenscript.codemodel.compilation.*;
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalExpression;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalThisExpression;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.globals.IGlobal;
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
Expand Down Expand Up @@ -56,6 +57,11 @@ public Optional<TypeID> getThisType() {
return Optional.ofNullable(localType).map(LocalType::getThisType);
}

@Override
public Optional<CompilingExpression> getThis(CodePosition position) {
return getThisType().map(thisType -> locals.capture(position, new LocalThisExpression(position, thisType)).compile(this));
}

@Override
public Optional<LocalType> getLocalType() {
return Optional.ofNullable(localType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.compilation.CompilableExpression;
import org.openzen.zenscript.codemodel.compilation.CompilingVariable;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalExpression;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalParameterExpression;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalVariableExpression;
import org.openzen.zenscript.codemodel.compilation.impl.capture.*;
import org.openzen.zenscript.codemodel.compilation.statement.CompilingLoopStatement;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -112,6 +111,15 @@ else if (closure != null)
return parent.findLocalVariable(position, name);
}

public LocalExpression capture(CodePosition position, LocalExpression local) {
if (this.parent == null)
return local;
else if (closure != null)
return local.capture(closure);
else
return parent.capture(position, local);
}

public Optional<CompilableExpression> getDollar() {
return Optional.ofNullable(dollar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ public static int calcAccess(Modifiers modifiers) {
}

public static void tagMethodParameters(JavaBytecodeContext context, JavaCompiledModule module, FunctionHeader header, boolean isStatic, List<TypeParameter> baseTypeTypeParameters) {
int index = isStatic ? 0 : 1;
int index = 0;

for (TypeParameter baseTypeTypeParameter : baseTypeTypeParameters) {
module.setTypeParameterInfo(baseTypeTypeParameter, new JavaTypeParameterInfo(index));
index++;
}

if (!isStatic) index++;

for (int i = 0; i < header.typeParameters.length; i++) {
TypeParameter parameter = header.typeParameters[i];
module.setTypeParameterInfo(parameter, new JavaTypeParameterInfo(index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.openzen.zenscript.codemodel.CompareType;
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.OperatorType;
import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
import org.openzen.zenscript.codemodel.identifiers.MethodID;
import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol;
import org.openzen.zenscript.codemodel.expression.*;
Expand Down Expand Up @@ -311,7 +312,13 @@ public Void visitCapturedParameter(CapturedParameterExpression expression) {

@Override
public Void visitCapturedThis(CapturedThisExpression expression) {
return expression.accept(capturedExpressionVisitor);
// TODO - does this cover all situations?
int thisLocal = 0;
if (javaWriter.forDefinition instanceof ExpansionDefinition) {
thisLocal = javaWriter.forDefinition.typeParameters.length;
}
javaWriter.load(context.getType(expression.type), thisLocal);
return null;
}

@Override
Expand Down Expand Up @@ -412,7 +419,7 @@ public Void visitConstantSByte(ConstantSByteExpression expression) {

@Override
public Void visitConstantShort(ConstantShortExpression expression) {
getJavaWriter().siPush(expression.value);
getJavaWriter().constant(expression.value);
return null;
}

Expand Down Expand Up @@ -564,12 +571,19 @@ public Void visitFunction(FunctionExpression expression) {
for (CapturedExpression capture : expression.closure.captures) {
constructorWriter.dup();
Type type = context.getType(capture.type);
lambdaCW.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, "captured" + ++i, type.getDescriptor(), null, null).visitEnd();
String name;
++i;
if (capture instanceof CapturedThisExpression) {
name = "capturedThis";
} else {
name = "captured" + i;
}
lambdaCW.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, name, type.getDescriptor(), null, null).visitEnd();

capture.accept(this);

constructorWriter.load(type, i);
constructorWriter.putField(className, "captured" + i, type.getDescriptor());
constructorWriter.putField(className, name, type.getDescriptor());
}

constructorWriter.pop();
Expand All @@ -595,7 +609,6 @@ public Void visitGetLocalVariable(GetLocalVariableExpression varExpression) {
return null;
}


final int position = calculateMemberPosition(varExpression, expression);
functionWriter.loadObject(0);
functionWriter.getField(className, "captured" + position, context.getDescriptor(varExpression.variable.type));
Expand All @@ -609,6 +622,13 @@ public Void visitCapturedParameter(CapturedParameterExpression varExpression) {
functionWriter.getField(className, "captured" + position, context.getDescriptor(varExpression.parameter.type));
return null;
}

@Override
public Void visitCapturedThis(CapturedThisExpression expression) {
functionWriter.loadObject(0);
functionWriter.getField(className, "capturedThis", context.getDescriptor(expression.type));
return null;
}
});

expression.body.accept(CSV);
Expand Down Expand Up @@ -1243,7 +1263,11 @@ public Void visitSubtypeCast(SubtypeCastExpression expression) {

@Override
public Void visitThis(ThisExpression expression) {
javaWriter.load(context.getType(expression.type), 0);
int thisLocal = 0;
if (javaWriter.forDefinition instanceof ExpansionDefinition) {
thisLocal = javaWriter.forDefinition.typeParameters.length;
}
javaWriter.load(context.getType(expression.type), thisLocal);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void handleArray(final int z, final JavaLocalVariableInfo arrayTypeInfo)
ArrayTypeID listType = (ArrayTypeID) statement.list.type.withoutOptional();
if (listType.elementType == BasicTypeID.BYTE) {
javaWriter.arrayLoad(Type.BYTE_TYPE);
javaWriter.siPush((short) 255);
javaWriter.constant(0xFF);
javaWriter.iAnd();
} else if (listType.elementType == BasicTypeID.USHORT) {
javaWriter.arrayLoad(Type.SHORT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,19 @@ public Void nativeConstructor(JavaNativeMethod method, TypeID type, CallArgument

@Override
public Void nativeVirtualMethod(JavaNativeMethod method, TypeID returnType, Expression target, CallArguments arguments) {
if (arguments.expansionTypeArguments.length > 0) {
final JavaTypeExpressionVisitor javaTypeExpressionVisitor = new JavaTypeExpressionVisitor(context);
for (int i = 0; i < arguments.expansionTypeArguments.length; i++) {
arguments.expansionTypeArguments[i].accept(javaWriter, javaTypeExpressionVisitor);
}
}
target.accept(expressionVisitor);
return nativeStaticMethod(method, returnType, arguments);
}

@Override
public Void nativeStaticMethod(JavaNativeMethod method, TypeID returnType, CallArguments arguments) {
handleArguments(arguments.typeArguments.length, method, arguments);
if (method.compile) {
handleTypeArguments(method, arguments);
}

if (method.kind == JavaNativeMethod.Kind.STATIC) {
javaWriter.invokeStatic(method);
Expand Down Expand Up @@ -205,14 +208,14 @@ public Void nativeStaticMethod(JavaNativeMethod method, TypeID returnType, CallA
public Void nativeSpecialMethod(JavaNativeMethod method, TypeID returnType, Expression target, CallArguments arguments) {
target.accept(expressionVisitor);
handleArguments(arguments.typeArguments.length, method, arguments);
if (method.compile) {
handleTypeArguments(method, arguments);
}
javaWriter.invokeSpecial(method);
return null;
}

private void handleArguments(int typeArguments, JavaNativeMethod method, CallArguments arguments) {
if (method.compile) {
handleTypeArguments(method, arguments);
}
for (int index = 0; index < arguments.arguments.length; index++) {
Expression argument = arguments.arguments[index];
argument.accept(expressionVisitor);
Expand All @@ -227,9 +230,6 @@ private void handleTypeArguments(JavaNativeMethod method, CallArguments argument
if (arguments.typeArguments.length != method.typeParameterArguments.length)
throw new IllegalArgumentException("Number of type parameters doesn't match");

for (int i = 0; i < arguments.expansionTypeArguments.length; i++) {
arguments.expansionTypeArguments[i].accept(javaWriter, javaTypeExpressionVisitor);
}
for (int i = 0; i < arguments.typeArguments.length; i++) {
if (method.typeParameterArguments[i])
arguments.typeArguments[i].accept(javaWriter, javaTypeExpressionVisitor);
Expand Down Expand Up @@ -335,7 +335,7 @@ public Void builtinStaticMethod(BuiltinMethodSymbol method, TypeID returnType, C
case BYTE_ADD_STRING:
case BYTE_CAT_STRING:
arguments[0].accept(expressionVisitor);
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
javaWriter.invokeStatic(INTEGER_TO_STRING);
arguments[1].accept(expressionVisitor);
Expand Down Expand Up @@ -570,42 +570,42 @@ public Void builtinStaticMethod(BuiltinMethodSymbol method, TypeID returnType, C
javaWriter.i2b();
break;
case BYTE_TO_SHORT:
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
javaWriter.i2s();
break;
case BYTE_TO_USHORT:
case BYTE_TO_INT:
case BYTE_TO_UINT:
case BYTE_TO_USIZE:
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
break;
case BYTE_TO_LONG:
case BYTE_TO_ULONG:
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
javaWriter.i2l();
break;
case BYTE_TO_FLOAT:
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
javaWriter.i2f();
break;
case BYTE_TO_DOUBLE:
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
javaWriter.i2d();
break;
case BYTE_TO_CHAR:
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
break;
case BYTE_TO_STRING:
if (arguments[0].type.isOptional()) {
javaWriter.invokeStatic(OBJECTS_TOSTRING);
} else {
javaWriter.siPush((short) 0xFF);
javaWriter.constant(0xFF);
javaWriter.iAnd();
javaWriter.invokeStatic(INTEGER_TO_STRING);
}
Expand Down
Loading

0 comments on commit 17d8d35

Please sign in to comment.