Skip to content

Commit

Permalink
0xsl33p
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlogic committed Jan 20, 2024
1 parent 91b9b5f commit f70e250
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 114 deletions.
100 changes: 74 additions & 26 deletions src/main/java/arb/expressions/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,18 @@ public ClassVisitor constructClassVisitor()
return cw;
}

public void declareFields(ClassVisitor classVisitor)
public void declareFields(ClassVisitor cw)
{
declareConstants(classVisitor);
cw.visitField(Opcodes.ACC_PRIVATE, "isInitialized", "Z", null, null);

declareVariables(classVisitor);
declareConstants(cw);

declareIntermediateVariables(classVisitor);
declareVariables(cw);

declareIntermediateVariables(cw);

declareFunctionReferences(cw);

declareFunctionReferences(classVisitor);
}

public ClassVisitor declareConstants(ClassVisitor classVisitor)
Expand Down Expand Up @@ -478,11 +481,14 @@ public ClassVisitor generateEvaluationMethod(ClassVisitor classVisitor) throws E
expression.length()));
}

addChecksForNullReferences(methodVisitor);
generateConditionalInitializater(methodVisitor);

addChecksForNullVariableReferences(methodVisitor);

rootNode.generate(methodVisitor, rangeType);

methodVisitor.visitInsn(Opcodes.ARETURN);

methodVisitor.visitLabel(endLabel);

declareLocalVariables(methodVisitor, startLabel, endLabel);
Expand All @@ -494,23 +500,33 @@ public ClassVisitor generateEvaluationMethod(ClassVisitor classVisitor) throws E
return classVisitor;
}

public void addChecksForNullReferences(MethodVisitor methodVisitor)
private void generateConditionalInitializater(MethodVisitor methodVisitor)
{
// FIXME: dont bother generating the initializing code if there is nothing to
// initialize
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitFieldInsn(GETFIELD, className, "isInitialized", "Z");
Label alreadyInitialized = new Label();
methodVisitor.visitJumpInsn(Opcodes.IFNE, alreadyInitialized);

methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, className, "initializeContext", "()V", false);

methodVisitor.visitLabel(alreadyInitialized);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}

public void addChecksForNullVariableReferences(MethodVisitor methodVisitor)
{
if (context != null)
{
for (var variable : referencedVariables.keySet())
for (var variable : context.variables.map.keySet())

// for (var variable : referencedVariables.keySet())
{
addCheckForNullField(methodVisitor, variable, true);
}
}
for (var variable : referencedFunctions.keySet())
{
if (!variable.equals(functionName))
{
addCheckForNullField(methodVisitor, variable, false);
}

}
}

public void addCheckForNullField(MethodVisitor methodVisitor, String varName, boolean variable)
Expand Down Expand Up @@ -603,7 +619,7 @@ public void instantiateAndInitialize(MethodVisitor mv,
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, fieldType, initializeContext, "()V", false);
}

public MethodVisitor initializeContext(MethodVisitor mv, Mapping<?, ?> mapping)
public MethodVisitor generateContextInitializer(MethodVisitor mv, Mapping<?, ?> mapping)
{

if (mapping.func != null)
Expand Down Expand Up @@ -689,6 +705,7 @@ public F instantiate(boolean primary)
f = compiledClass.getDeclaredConstructor().newInstance();
}
injectVariableReferences(f);
compiledClass.getMethod(initializeContext).invoke(f);
return instance;
}
catch (Exception e)
Expand Down Expand Up @@ -1259,19 +1276,53 @@ public String toString()
functionClass);
}

public MethodVisitor initializeContext(MethodVisitor methodVisitor)
public MethodVisitor generateContextInitializer(MethodVisitor methodVisitor)
{
generateCodeToThrowErrorIfAlreadyInitialized(methodVisitor);


referencedFunctions.values().forEach(mapping ->
{
if (mapping.func != null)
{
initializeContext(methodVisitor, mapping);
generateContextInitializer(methodVisitor, mapping);
}
});

generateCodeToSetIsInitializedToTrue(methodVisitor);

return methodVisitor;
}

private void generateCodeToThrowErrorIfAlreadyInitialized(MethodVisitor methodVisitor)
{

methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitFieldInsn(GETFIELD, className, "isInitialized", "Z");
Label alreadyInitializedLabel = new Label();
methodVisitor.visitJumpInsn(Opcodes.IFEQ, alreadyInitializedLabel);

methodVisitor.visitTypeInsn(Opcodes.NEW, "java/lang/AssertionError");
methodVisitor.visitInsn(Opcodes.DUP);
methodVisitor.visitLdcInsn("Already initialized");
methodVisitor.visitMethodInsn(INVOKESPECIAL,
"java/lang/AssertionError",
"<init>",
"(Ljava/lang/Object;)V",
false);
methodVisitor.visitInsn(Opcodes.ATHROW);
methodVisitor.visitLabel(alreadyInitializedLabel);
methodVisitor.visitLineNumber(15, alreadyInitializedLabel);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}

private void generateCodeToSetIsInitializedToTrue(MethodVisitor methodVisitor)
{
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitFieldInsn(PUTFIELD, className, "isInitialized", "Z");
}

public void generateInvocationOfDefaultNoArgConstructor(MethodVisitor methodVisitor, boolean object)
{
methodVisitor.visitVarInsn(ALOAD, 0);
Expand All @@ -1293,6 +1344,8 @@ public ClassVisitor generateCopyConstructor(ClassVisitor classVisitor)

generateInvocationOfDefaultNoArgConstructor(methodVisitor, false);

addChecksForNullVariableReferences(methodVisitor);

for (Variable<D, R, F> variable : referencedVariables.values())
{
String variableName = variable.reference.name;
Expand Down Expand Up @@ -1323,12 +1376,6 @@ public ClassVisitor generateDefaultConstructor(ClassVisitor classVisitor)

initializeIntermediateVariables(methodVisitor);

loadThisOntoStack(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL,
className,
initializeContext,
"()V",
false);

methodVisitor.visitInsn(RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
Expand All @@ -1340,9 +1387,10 @@ public ClassVisitor generateInitializationMethod(ClassVisitor classVisitor)
MethodVisitor methodVisitor = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, initializeContext, "()V", null, null);
try
{

methodVisitor.visitCode();

initializeContext(methodVisitor);
generateContextInitializer(methodVisitor);

methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/arb/functions/C.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
public class C implements
RealFunction
{
public Integer const1 = new Integer("2");
public Real α;
public Real β;
public Real G;
public Real r1 = new Real();
public Real r2 = new Real();
private boolean isInitialized;
public Integer const1 = new Integer("2");
public Real α;
public Real β;
public Real G;
public Real r1 = new Real();
public Real r2 = new Real();
public C C;

public Real evaluate(Real in, int order, int bits, Real result)
{
if (!this.isInitialized)
{
this.initializeContext();
}

if (this.α == null)
{
throw new AssertionError("α is null");
Expand All @@ -30,13 +37,16 @@ else if (this.β == null)
}
}

public C()
{
this.initializeContext();
}

public void initializeContext()
{
if (this.isInitialized)
{
throw new AssertionError("Already initialized");
}
else
{
this.isInitialized = true;
}
}

public void close()
Expand Down
70 changes: 38 additions & 32 deletions src/main/java/arb/functions/F.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,53 @@
public class F implements
Function<Integer, Real>
{
public Integer const1 = new Integer("1");
public Real α;
public Real β;
public Real G;
public Real r1 = new Real();
public Integer i1 = new Integer();
public Real r2 = new Real();
public Real r3 = new Real();
public Real r4 = new Real();
public C C;
private boolean isInitialized;
public Integer const1 = new Integer("1");
public Real α;
public Real β;
public Real G;
public Real r1 = new Real();
public Integer i1 = new Integer();
public Real r2 = new Real();
public Real r3 = new Real();
public Real r4 = new Real();
public C C;
public F F;

public Real evaluate(Integer in, int order, int bits, Real result)
{
if (this.C == null)
if (!this.isInitialized)
{
throw new AssertionError("C is null");
this.initializeContext();
}
else
{
return ((Real) this.C.evaluate(this.r1.set(((Integer) in).sub(this.const1, bits, this.i1)),
order,
bits,
this.r2)).mul((Real) this.C.evaluate(this.r3.set((Integer) in),
order,
bits,
this.r4),
bits,
(Real) result);
}
}

public F()
{
this.initializeContext();
return ((Real) this.C.evaluate(this.r1.set(((Integer) in).sub(this.const1, bits, this.i1)),
order,
bits,
this.r2)).mul((Real) this.C.evaluate(this.r3.set((Integer) in),
order,
bits,
this.r4),
bits,
(Real) result);
}

public void initializeContext()
{
C var10001 = this.C = new C();
this.C.initializeContext();
this.C = var10001;
if (this.isInitialized)
{
throw new AssertionError("Already initialized");
}
else
{
C var10001 = this.C = new C();
this.C.α = this.α;
this.C.β = this.β;
this.C.G = this.G;
this.C.initializeContext();
this.C = var10001;
this.isInitialized = true;
}
}

public void close()
Expand All @@ -58,4 +64,4 @@ public void close()
this.r3.close();
this.r4.close();
}
}
}
Loading

0 comments on commit f70e250

Please sign in to comment.