001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.mir2mc;
014
015import org.jikesrvm.VM;
016import org.jikesrvm.architecture.ArchConstants;
017import org.jikesrvm.compilers.opt.OptOptions;
018import org.jikesrvm.compilers.opt.driver.CompilerPhase;
019import org.jikesrvm.compilers.opt.driver.OptimizingCompiler;
020import org.jikesrvm.compilers.opt.ir.IR;
021import org.jikesrvm.runtime.Magic;
022import org.jikesrvm.runtime.Memory;
023
024/**
025 * A compiler phase that generates machine code instructions and maps.
026 */
027final class AssemblerDriver extends CompilerPhase {
028
029  @Override
030  public String getName() {
031    return "Assembler Driver";
032  }
033
034  @Override
035  public boolean printingEnabled(OptOptions options, boolean before) {
036    //don't bother printing afterwards, PRINT_MACHINECODE handles that
037    return before && options.DEBUG_CODEGEN;
038  }
039
040  // this class has no instance fields.
041  @Override
042  public CompilerPhase newExecution(IR ir) {
043    return this;
044  }
045
046  @Override
047  public void perform(IR ir) {
048    OptOptions options = ir.options;
049    boolean shouldPrint =
050        (options.PRINT_MACHINECODE) &&
051        (!ir.options.hasMETHOD_TO_PRINT() || ir.options.fuzzyMatchMETHOD_TO_PRINT(ir.method.toString()));
052
053    if (IR.SANITY_CHECK) {
054      ir.verify("right before machine codegen", true);
055    }
056
057    //////////
058    // STEP 2: Generate the machinecode array.
059    // As part of the generation, the machinecode offset
060    // of every instruction will be set.
061    //////////
062    int codeLength;
063    if (VM.BuildForIA32) {
064      org.jikesrvm.compilers.opt.mir2mc.ia32.AssemblerOpt asm =
065          new org.jikesrvm.compilers.opt.mir2mc.ia32.AssemblerOpt(0, shouldPrint, ir);
066      codeLength = asm.generateCode();
067    } else {
068      if (VM.VerifyAssertions) VM._assert(VM.BuildForPowerPC);
069      org.jikesrvm.compilers.opt.mir2mc.ppc.AssemblerOpt asm =
070          new org.jikesrvm.compilers.opt.mir2mc.ppc.AssemblerOpt(0, shouldPrint, ir);
071      codeLength = asm.generateCode();
072    }
073
074    //////////
075    // STEP 3: Generate all the mapping information
076    // associated with the machine code.
077    //////////
078    // 3a: Create the exception table
079    ir.compiledMethod.createFinalExceptionTable(ir);
080    // 3b: Create the primary machine code map
081    ir.compiledMethod.createFinalMCMap(ir, codeLength);
082    // 3c: Create OSR maps
083    ir.compiledMethod.createFinalOSRMap(ir);
084    // 3d: Create code patching maps
085    if (ir.options.guardWithCodePatch()) {
086      ir.compiledMethod.createCodePatchMaps(ir);
087    }
088
089    if (shouldPrint) {
090      // print exception tables (if any)
091      ir.compiledMethod.printExceptionTable();
092      OptimizingCompiler.bottom("Final machine code", ir.method);
093    }
094
095    if (VM.runningVM) {
096      Memory.sync(Magic.objectAsAddress(ir.MIRInfo.machinecode),
097                     codeLength << ArchConstants.getLogInstructionWidth());
098    }
099  }
100
101  @Override
102  public void verify(IR ir) {
103    /* Do nothing, IR invariants violated by final expansion*/
104  }
105}