use of org.jetbrains.org.objectweb.asm.commons.InstructionAdapter in project kotlin by JetBrains.
the class AsmUtil method genNotNullAssertions.
@NotNull
public static StackValue genNotNullAssertions(@NotNull GenerationState state, @NotNull final StackValue stackValue, @Nullable final RuntimeAssertionInfo runtimeAssertionInfo) {
if (state.isCallAssertionsDisabled())
return stackValue;
if (runtimeAssertionInfo == null || !runtimeAssertionInfo.getNeedNotNullAssertion())
return stackValue;
return new StackValue(stackValue.type) {
@Override
public void putSelector(@NotNull Type type, @NotNull InstructionAdapter v) {
Type innerType = stackValue.type;
stackValue.put(innerType, v);
if (innerType.getSort() == Type.OBJECT || innerType.getSort() == Type.ARRAY) {
v.dup();
v.visitLdcInsn(runtimeAssertionInfo.getMessage());
v.invokestatic("kotlin/jvm/internal/Intrinsics", "checkExpressionValueIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V", false);
}
StackValue.coerce(innerType, type, v);
}
};
}
use of org.jetbrains.org.objectweb.asm.commons.InstructionAdapter in project kotlin by JetBrains.
the class ClosureCodegen method generateConstructor.
@NotNull
protected Method generateConstructor() {
List<FieldInfo> args = calculateConstructorParameters(typeMapper, closure, asmType);
Type[] argTypes = fieldListToTypeArray(args);
Method constructor = new Method("<init>", Type.VOID_TYPE, argTypes);
MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(element, funDescriptor), visibilityFlag, "<init>", constructor.getDescriptor(), null, ArrayUtil.EMPTY_STRING_ARRAY);
if (state.getClassBuilderMode().generateBodies) {
mv.visitCode();
InstructionAdapter iv = new InstructionAdapter(mv);
Pair<Integer, Type> receiverIndexAndType = CallableReferenceUtilKt.generateClosureFieldsInitializationFromParameters(iv, closure, args);
if (shouldHaveBoundReferenceReceiver && receiverIndexAndType == null) {
throw new AssertionError("No bound reference receiver in constructor parameters: " + args);
}
int boundReferenceReceiverParameterIndex = shouldHaveBoundReferenceReceiver ? receiverIndexAndType.getFirst() : -1;
Type boundReferenceReceiverType = shouldHaveBoundReferenceReceiver ? receiverIndexAndType.getSecond() : null;
iv.load(0, superClassAsmType);
String superClassConstructorDescriptor;
if (superClassAsmType.equals(LAMBDA) || superClassAsmType.equals(FUNCTION_REFERENCE) || superClassAsmType.equals(CoroutineCodegenUtilKt.COROUTINE_IMPL_ASM_TYPE)) {
int arity = calculateArity();
iv.iconst(arity);
if (shouldHaveBoundReferenceReceiver) {
CallableReferenceUtilKt.loadBoundReferenceReceiverParameter(iv, boundReferenceReceiverParameterIndex, boundReferenceReceiverType);
superClassConstructorDescriptor = "(ILjava/lang/Object;)V";
} else {
superClassConstructorDescriptor = "(I)V";
}
} else {
assert !shouldHaveBoundReferenceReceiver : "Unexpected bound reference with supertype " + superClassAsmType;
superClassConstructorDescriptor = "()V";
}
iv.invokespecial(superClassAsmType.getInternalName(), "<init>", superClassConstructorDescriptor, false);
iv.visitInsn(RETURN);
FunctionCodegen.endVisit(iv, "constructor", element);
}
return constructor;
}
use of org.jetbrains.org.objectweb.asm.commons.InstructionAdapter in project kotlin by JetBrains.
the class ClosureCodegen method generateBridge.
protected void generateBridge(@NotNull Method bridge, @NotNull Method delegate) {
if (bridge.equals(delegate))
return;
MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(element, funDescriptor), ACC_PUBLIC | ACC_BRIDGE | ACC_SYNTHETIC, bridge.getName(), bridge.getDescriptor(), null, ArrayUtil.EMPTY_STRING_ARRAY);
if (!state.getClassBuilderMode().generateBodies)
return;
mv.visitCode();
InstructionAdapter iv = new InstructionAdapter(mv);
MemberCodegen.markLineNumberForDescriptor(DescriptorUtils.getParentOfType(funDescriptor, ClassDescriptor.class), iv);
iv.load(0, asmType);
Type[] myParameterTypes = bridge.getArgumentTypes();
List<ParameterDescriptor> calleeParameters = CollectionsKt.plus(org.jetbrains.kotlin.utils.CollectionsKt.<ParameterDescriptor>singletonOrEmptyList(funDescriptor.getExtensionReceiverParameter()), funDescriptor.getValueParameters());
int slot = 1;
for (int i = 0; i < calleeParameters.size(); i++) {
Type type = myParameterTypes[i];
StackValue.local(slot, type).put(typeMapper.mapType(calleeParameters.get(i)), iv);
slot += type.getSize();
}
iv.invokevirtual(asmType.getInternalName(), delegate.getName(), delegate.getDescriptor(), false);
StackValue.onStack(delegate.getReturnType()).put(bridge.getReturnType(), iv);
iv.areturn(bridge.getReturnType());
FunctionCodegen.endVisit(mv, "bridge", element);
}
use of org.jetbrains.org.objectweb.asm.commons.InstructionAdapter in project kotlin by JetBrains.
the class ScriptCodegen method genConstructor.
private void genConstructor(@NotNull ScriptDescriptor scriptDescriptor, @NotNull ClassBuilder classBuilder, @NotNull MethodContext methodContext) {
JvmMethodSignature jvmSignature = typeMapper.mapScriptSignature(scriptDescriptor, context.getEarlierScripts());
if (state.getReplSpecific().getShouldGenerateScriptResultValue()) {
FieldInfo resultFieldInfo = context.getResultFieldInfo();
classBuilder.newField(JvmDeclarationOrigin.NO_ORIGIN, ACC_PUBLIC | ACC_FINAL, resultFieldInfo.getFieldName(), resultFieldInfo.getFieldType().getDescriptor(), null, null);
}
MethodVisitor mv = classBuilder.newMethod(JvmDeclarationOriginKt.OtherOrigin(scriptDeclaration, scriptDescriptor.getUnsubstitutedPrimaryConstructor()), ACC_PUBLIC, jvmSignature.getAsmMethod().getName(), jvmSignature.getAsmMethod().getDescriptor(), null, null);
if (state.getClassBuilderMode().generateBodies) {
mv.visitCode();
InstructionAdapter iv = new InstructionAdapter(mv);
Type classType = typeMapper.mapType(scriptDescriptor);
ClassDescriptor superclass = DescriptorUtilsKt.getSuperClassNotAny(scriptDescriptor);
if (superclass == null) {
iv.load(0, classType);
iv.invokespecial("java/lang/Object", "<init>", "()V", false);
} else {
ConstructorDescriptor ctorDesc = superclass.getUnsubstitutedPrimaryConstructor();
if (ctorDesc == null)
throw new RuntimeException("Primary constructor not found for script template " + superclass.toString());
iv.load(0, classType);
int valueParamStart = context.getEarlierScripts().size() + 1;
List<ValueParameterDescriptor> valueParameters = scriptDescriptor.getUnsubstitutedPrimaryConstructor().getValueParameters();
for (ValueParameterDescriptor superclassParam : ctorDesc.getValueParameters()) {
ValueParameterDescriptor valueParam = null;
for (ValueParameterDescriptor vpd : valueParameters) {
if (vpd.getName().equals(superclassParam.getName())) {
valueParam = vpd;
break;
}
}
assert valueParam != null;
iv.load(valueParam.getIndex() + valueParamStart, typeMapper.mapType(valueParam.getType()));
}
CallableMethod ctorMethod = typeMapper.mapToCallableMethod(ctorDesc, false);
String sig = ctorMethod.getAsmMethod().getDescriptor();
iv.invokespecial(typeMapper.mapSupertype(superclass.getDefaultType(), null).getInternalName(), "<init>", sig, false);
}
iv.load(0, classType);
FrameMap frameMap = new FrameMap();
frameMap.enterTemp(OBJECT_TYPE);
for (ScriptDescriptor importedScript : context.getEarlierScripts()) {
frameMap.enter(importedScript, OBJECT_TYPE);
}
int offset = 1;
for (ScriptDescriptor earlierScript : context.getEarlierScripts()) {
Type earlierClassType = typeMapper.mapClass(earlierScript);
iv.load(0, classType);
iv.load(offset, earlierClassType);
offset += earlierClassType.getSize();
iv.putfield(classType.getInternalName(), context.getScriptFieldName(earlierScript), earlierClassType.getDescriptor());
}
final ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, methodContext, state, this);
generateInitializers(new Function0<ExpressionCodegen>() {
@Override
public ExpressionCodegen invoke() {
return codegen;
}
});
iv.areturn(Type.VOID_TYPE);
}
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
use of org.jetbrains.org.objectweb.asm.commons.InstructionAdapter in project kotlin by JetBrains.
the class MappingClassesForWhenByEnumCodegen method generateInitialization.
private void generateInitialization(@NotNull ClassBuilder cb, @NotNull List<WhenByEnumsMapping> mappings) {
MethodVisitor mv = cb.newMethod(JvmDeclarationOrigin.NO_ORIGIN, ACC_STATIC | ACC_SYNTHETIC, "<clinit>", "()V", null, ArrayUtil.EMPTY_STRING_ARRAY);
mv.visitCode();
InstructionAdapter v = new InstructionAdapter(mv);
for (WhenByEnumsMapping mapping : mappings) {
generateInitializationForMapping(cb, v, mapping);
}
v.areturn(Type.VOID_TYPE);
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
Aggregations