1 package org.simantics.scl.compiler.constants.generic;
3 import org.cojen.classfile.TypeDesc;
4 import org.objectweb.asm.Label;
5 import org.simantics.scl.compiler.internal.codegen.references.Val;
6 import org.simantics.scl.compiler.internal.codegen.utils.LocalVariable;
7 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
8 import org.simantics.scl.compiler.internal.codegen.utils.PreparationStep;
9 import org.simantics.scl.compiler.internal.codegen.utils.SCLContextPreparationStep;
10 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
12 public class ThreadLocalStackItem implements StackItem, PreparationStep<LocalVariable> {
14 private static final TypeDesc THREAD_LOCAL =
15 TypeDesc.forClass(ThreadLocal.class);
20 public ThreadLocalStackItem(String variableName, TypeDesc type) {
21 this.variableName = variableName;
26 public void push(MethodBuilder mb, Val[] parameters) {
27 mb.loadLocal(mb.getPreparation(this));
31 public void prepare(MethodBuilder mb) {
32 LocalVariable var = mb.getPreparation(this);
34 var = mb.createLocalVariable("context", type);
35 LocalVariable sclContext = SCLContextPreparationStep.getCurrent(mb);
36 mb.loadLocal(sclContext);
37 mb.loadConstant(variableName);
38 mb.invokeVirtual("gnu/trove/map/hash/THashMap", "get",
39 TypeDesc.OBJECT, new TypeDesc[] {TypeDesc.OBJECT});
43 if(SCLCompilerConfiguration.NULL_CHECK_THREAD_LOCAL_VARIABLES) {
44 Label exit = mb.createLabel();
46 mb.ifNullBranch(exit, false);
48 // This code should me redundant because of static type caughts the problem
49 mb.newObject(TypeDesc.forClass(IllegalStateException.class));
51 mb.loadConstant("Thread local variable missing");
52 mb.invokeConstructor(TypeDesc.forClass(IllegalStateException.class),
53 new TypeDesc[] {TypeDesc.STRING});
59 mb.addPreparation(this, var);
64 public int hashCode() {
65 return 234234 + type.hashCode() + 31*variableName.hashCode();
69 public boolean equals(Object obj) {
74 if (getClass() != obj.getClass())
76 ThreadLocalStackItem other = (ThreadLocalStackItem) obj;
77 return variableName.equals(other.variableName)
78 && type.equals(other.type);