|  | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file | 
|  | // for details. All rights reserved. Use of this source code is governed by a | 
|  | // BSD-style license that can be found in the LICENSE file. | 
|  | package com.android.tools.r8.cf.code; | 
|  |  | 
|  | import com.android.tools.r8.cf.CfPrinter; | 
|  | import com.android.tools.r8.graph.AppView; | 
|  | import com.android.tools.r8.graph.CfCompareHelper; | 
|  | import com.android.tools.r8.graph.DexItemFactory; | 
|  | import com.android.tools.r8.graph.DexString; | 
|  | import com.android.tools.r8.graph.ProgramMethod; | 
|  | import com.android.tools.r8.graph.lens.GraphLens; | 
|  | import com.android.tools.r8.graph.lens.InitClassLens; | 
|  | import com.android.tools.r8.ir.conversion.CfSourceCode; | 
|  | import com.android.tools.r8.ir.conversion.CfState; | 
|  | import com.android.tools.r8.ir.conversion.IRBuilder; | 
|  | import com.android.tools.r8.ir.conversion.LensCodeRewriterUtils; | 
|  | import com.android.tools.r8.naming.NamingLens; | 
|  | import com.android.tools.r8.optimize.interfaces.analysis.CfAnalysisConfig; | 
|  | import com.android.tools.r8.optimize.interfaces.analysis.CfFrameState; | 
|  | import com.android.tools.r8.utils.structural.CompareToVisitor; | 
|  | import com.android.tools.r8.utils.structural.HashingVisitor; | 
|  | import org.objectweb.asm.MethodVisitor; | 
|  |  | 
|  | public class CfConstString extends CfInstruction { | 
|  |  | 
|  | private DexString string; | 
|  |  | 
|  | public CfConstString(DexString string) { | 
|  | this.string = string; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public int getCompareToId() { | 
|  | return CfCompareHelper.CONST_STRING_COMPARE_ID; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public int internalAcceptCompareTo( | 
|  | CfInstruction other, CompareToVisitor visitor, CfCompareHelper helper) { | 
|  | return string.acceptCompareTo(other.asConstString().string, visitor); | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public void internalAcceptHashing(HashingVisitor visitor) { | 
|  | string.acceptHashing(visitor); | 
|  | } | 
|  |  | 
|  | public DexString getString() { | 
|  | return string; | 
|  | } | 
|  |  | 
|  | public void setString(DexString string) { | 
|  | this.string = string; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public CfConstString asConstString() { | 
|  | return this; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public boolean isConstString() { | 
|  | return true; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public void write( | 
|  | AppView<?> appView, | 
|  | ProgramMethod context, | 
|  | DexItemFactory dexItemFactory, | 
|  | GraphLens graphLens, | 
|  | GraphLens codeLens, | 
|  | InitClassLens initClassLens, | 
|  | NamingLens namingLens, | 
|  | LensCodeRewriterUtils rewriter, | 
|  | MethodVisitor visitor) { | 
|  | visitor.visitLdcInsn(string.toString()); | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public int bytecodeSizeUpperBound() { | 
|  | // ldc or ldc_w | 
|  | return 3; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public void print(CfPrinter printer) { | 
|  | printer.print(this); | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public boolean canThrow() { | 
|  | // const-string* may throw in dex. (Not in CF, see CfSourceCode.canThrowHelper) | 
|  | return true; | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public void buildIR(IRBuilder builder, CfState state, CfSourceCode code) { | 
|  | builder.addConstString( | 
|  | state.push(builder.appView.dexItemFactory().stringType).register, string); | 
|  | } | 
|  |  | 
|  | @Override | 
|  | public CfFrameState evaluate(CfFrameState frame, AppView<?> appView, CfAnalysisConfig config) { | 
|  | // ... → | 
|  | // ..., value | 
|  | return frame.push(config, appView.dexItemFactory().stringType); | 
|  | } | 
|  | } |