< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.lir/src/org/graalvm/compiler/lir/asm/CompilationResultBuilder.java

Print this page




  63 import jdk.vm.ci.code.StackSlot;
  64 import jdk.vm.ci.code.TargetDescription;
  65 import jdk.vm.ci.code.site.ConstantReference;
  66 import jdk.vm.ci.code.site.DataSectionReference;
  67 import jdk.vm.ci.code.site.InfopointReason;
  68 import jdk.vm.ci.code.site.Mark;
  69 import jdk.vm.ci.meta.Constant;
  70 import jdk.vm.ci.meta.InvokeTarget;
  71 import jdk.vm.ci.meta.JavaConstant;
  72 import jdk.vm.ci.meta.JavaKind;
  73 import jdk.vm.ci.meta.VMConstant;
  74 import jdk.vm.ci.meta.Value;
  75 
  76 /**
  77  * Fills in a {@link CompilationResult} as its code is being assembled.
  78  *
  79  * @see CompilationResultBuilderFactory
  80  */
  81 public class CompilationResultBuilder {
  82 
  83     // @formatter:off
  84     @Option(help = "Include the LIR as comments with the final assembly.", type = OptionType.Debug)
  85     public static final OptionKey<Boolean> PrintLIRWithAssembly = new OptionKey<>(false);
  86     // @formatter:on
  87 
  88     private static class ExceptionInfo {
  89 
  90         public final int codeOffset;
  91         public final LabelRef exceptionEdge;
  92 
  93         ExceptionInfo(int pcOffset, LabelRef exceptionEdge) {
  94             this.codeOffset = pcOffset;
  95             this.exceptionEdge = exceptionEdge;
  96         }
  97     }
  98 
  99     /**
 100      * Wrapper for a code annotation that was produced by the {@link Assembler}.
 101      */
 102     public static final class AssemblerAnnotation extends CodeAnnotation {
 103 
 104         public final Assembler.CodeAnnotation assemblerCodeAnnotation;
 105 
 106         public AssemblerAnnotation(Assembler.CodeAnnotation assemblerCodeAnnotation) {


 278         if (data instanceof VMConstant) {
 279             compilationResult.recordDataPatchWithNote(pos, new ConstantReference((VMConstant) data), note);
 280         }
 281     }
 282 
 283     public AbstractAddress recordDataSectionReference(Data data) {
 284         assert data != null;
 285         DataSectionReference reference = compilationResult.getDataSection().insertData(data);
 286         int instructionStart = asm.position();
 287         compilationResult.recordDataPatch(instructionStart, reference);
 288         return asm.getPlaceholder(instructionStart);
 289     }
 290 
 291     public AbstractAddress recordDataReferenceInCode(DataPointerConstant constant) {
 292         return recordDataReferenceInCode(constant, constant.getAlignment());
 293     }
 294 
 295     public AbstractAddress recordDataReferenceInCode(Constant constant, int alignment) {
 296         assert constant != null;
 297         debug.log("Constant reference in code: pos = %d, data = %s", asm.position(), constant);












 298         Data data = dataCache.get(constant);
 299         if (data == null) {
 300             data = dataBuilder.createDataItem(constant);
 301             dataCache.put(constant, data);
 302         }
 303         data.updateAlignment(alignment);
 304         return recordDataSectionReference(data);
 305     }
 306 
 307     public AbstractAddress recordDataReferenceInCode(byte[] data, int alignment) {
 308         assert data != null;
 309         if (debug.isLogEnabled()) {
 310             debug.log("Data reference in code: pos = %d, data = %s", asm.position(), Arrays.toString(data));
 311         }
 312         return recordDataSectionReference(new RawData(data, alignment));
 313     }
 314 
 315     /**
 316      * Notifies this object of a branch instruction at offset {@code pos} in the code.
 317      *
 318      * @param isNegated negation status of the branch's condition.
 319      */
 320     @SuppressWarnings("unused")
 321     public void recordBranch(int pos, boolean isNegated) {
 322     }
 323 
 324     /**


 455      */
 456     public void emit(@SuppressWarnings("hiding") LIR lir) {
 457         assert this.lir == null;
 458         assert currentBlockIndex == 0;
 459         this.lir = lir;
 460         this.currentBlockIndex = 0;
 461         frameContext.enter(this);
 462         for (AbstractBlockBase<?> b : lir.codeEmittingOrder()) {
 463             assert (b == null && lir.codeEmittingOrder()[currentBlockIndex] == null) || lir.codeEmittingOrder()[currentBlockIndex].equals(b);
 464             emitBlock(b);
 465             currentBlockIndex++;
 466         }
 467         this.lir = null;
 468         this.currentBlockIndex = 0;
 469     }
 470 
 471     private void emitBlock(AbstractBlockBase<?> block) {
 472         if (block == null) {
 473             return;
 474         }
 475         boolean emitComment = debug.isDumpEnabled(DebugContext.BASIC_LEVEL) || PrintLIRWithAssembly.getValue(getOptions());
 476         if (emitComment) {
 477             blockComment(String.format("block B%d %s", block.getId(), block.getLoop()));
 478         }
 479 
 480         for (LIRInstruction op : lir.getLIRforBlock(block)) {
 481             if (emitComment) {
 482                 blockComment(String.format("%d %s", op.id(), op));
 483             }
 484 
 485             try {
 486                 if (beforeOp != null) {
 487                     beforeOp.accept(op);
 488                 }
 489                 emitOp(this, op);
 490                 if (afterOp != null) {
 491                     afterOp.accept(op);
 492                 }
 493             } catch (GraalError e) {
 494                 throw e.addContext("lir instruction", block + "@" + op.id() + " " + op.getClass().getName() + " " + op + "\n" + Arrays.toString(lir.codeEmittingOrder()));
 495             }




  63 import jdk.vm.ci.code.StackSlot;
  64 import jdk.vm.ci.code.TargetDescription;
  65 import jdk.vm.ci.code.site.ConstantReference;
  66 import jdk.vm.ci.code.site.DataSectionReference;
  67 import jdk.vm.ci.code.site.InfopointReason;
  68 import jdk.vm.ci.code.site.Mark;
  69 import jdk.vm.ci.meta.Constant;
  70 import jdk.vm.ci.meta.InvokeTarget;
  71 import jdk.vm.ci.meta.JavaConstant;
  72 import jdk.vm.ci.meta.JavaKind;
  73 import jdk.vm.ci.meta.VMConstant;
  74 import jdk.vm.ci.meta.Value;
  75 
  76 /**
  77  * Fills in a {@link CompilationResult} as its code is being assembled.
  78  *
  79  * @see CompilationResultBuilderFactory
  80  */
  81 public class CompilationResultBuilder {
  82 
  83     public static class Options {
  84         @Option(help = "Include the LIR as comments with the final assembly.", type = OptionType.Debug) //
  85         public static final OptionKey<Boolean> PrintLIRWithAssembly = new OptionKey<>(false);
  86     }
  87 
  88     private static class ExceptionInfo {
  89 
  90         public final int codeOffset;
  91         public final LabelRef exceptionEdge;
  92 
  93         ExceptionInfo(int pcOffset, LabelRef exceptionEdge) {
  94             this.codeOffset = pcOffset;
  95             this.exceptionEdge = exceptionEdge;
  96         }
  97     }
  98 
  99     /**
 100      * Wrapper for a code annotation that was produced by the {@link Assembler}.
 101      */
 102     public static final class AssemblerAnnotation extends CodeAnnotation {
 103 
 104         public final Assembler.CodeAnnotation assemblerCodeAnnotation;
 105 
 106         public AssemblerAnnotation(Assembler.CodeAnnotation assemblerCodeAnnotation) {


 278         if (data instanceof VMConstant) {
 279             compilationResult.recordDataPatchWithNote(pos, new ConstantReference((VMConstant) data), note);
 280         }
 281     }
 282 
 283     public AbstractAddress recordDataSectionReference(Data data) {
 284         assert data != null;
 285         DataSectionReference reference = compilationResult.getDataSection().insertData(data);
 286         int instructionStart = asm.position();
 287         compilationResult.recordDataPatch(instructionStart, reference);
 288         return asm.getPlaceholder(instructionStart);
 289     }
 290 
 291     public AbstractAddress recordDataReferenceInCode(DataPointerConstant constant) {
 292         return recordDataReferenceInCode(constant, constant.getAlignment());
 293     }
 294 
 295     public AbstractAddress recordDataReferenceInCode(Constant constant, int alignment) {
 296         assert constant != null;
 297         debug.log("Constant reference in code: pos = %d, data = %s", asm.position(), constant);
 298         Data data = createDataItem(constant);
 299         data.updateAlignment(alignment);
 300         return recordDataSectionReference(data);
 301     }
 302 
 303     public AbstractAddress recordDataReferenceInCode(Data data, int alignment) {
 304         assert data != null;
 305         data.updateAlignment(alignment);
 306         return recordDataSectionReference(data);
 307     }
 308 
 309     public Data createDataItem(Constant constant) {
 310         Data data = dataCache.get(constant);
 311         if (data == null) {
 312             data = dataBuilder.createDataItem(constant);
 313             dataCache.put(constant, data);
 314         }
 315         return data;

 316     }
 317 
 318     public AbstractAddress recordDataReferenceInCode(byte[] data, int alignment) {
 319         assert data != null;
 320         if (debug.isLogEnabled()) {
 321             debug.log("Data reference in code: pos = %d, data = %s", asm.position(), Arrays.toString(data));
 322         }
 323         return recordDataSectionReference(new RawData(data, alignment));
 324     }
 325 
 326     /**
 327      * Notifies this object of a branch instruction at offset {@code pos} in the code.
 328      *
 329      * @param isNegated negation status of the branch's condition.
 330      */
 331     @SuppressWarnings("unused")
 332     public void recordBranch(int pos, boolean isNegated) {
 333     }
 334 
 335     /**


 466      */
 467     public void emit(@SuppressWarnings("hiding") LIR lir) {
 468         assert this.lir == null;
 469         assert currentBlockIndex == 0;
 470         this.lir = lir;
 471         this.currentBlockIndex = 0;
 472         frameContext.enter(this);
 473         for (AbstractBlockBase<?> b : lir.codeEmittingOrder()) {
 474             assert (b == null && lir.codeEmittingOrder()[currentBlockIndex] == null) || lir.codeEmittingOrder()[currentBlockIndex].equals(b);
 475             emitBlock(b);
 476             currentBlockIndex++;
 477         }
 478         this.lir = null;
 479         this.currentBlockIndex = 0;
 480     }
 481 
 482     private void emitBlock(AbstractBlockBase<?> block) {
 483         if (block == null) {
 484             return;
 485         }
 486         boolean emitComment = debug.isDumpEnabled(DebugContext.BASIC_LEVEL) || Options.PrintLIRWithAssembly.getValue(getOptions());
 487         if (emitComment) {
 488             blockComment(String.format("block B%d %s", block.getId(), block.getLoop()));
 489         }
 490 
 491         for (LIRInstruction op : lir.getLIRforBlock(block)) {
 492             if (emitComment) {
 493                 blockComment(String.format("%d %s", op.id(), op));
 494             }
 495 
 496             try {
 497                 if (beforeOp != null) {
 498                     beforeOp.accept(op);
 499                 }
 500                 emitOp(this, op);
 501                 if (afterOp != null) {
 502                     afterOp.accept(op);
 503                 }
 504             } catch (GraalError e) {
 505                 throw e.addContext("lir instruction", block + "@" + op.id() + " " + op.getClass().getName() + " " + op + "\n" + Arrays.toString(lir.codeEmittingOrder()));
 506             }


< prev index next >