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     */
013    package org.jikesrvm.ia32;
014    
015    import org.jikesrvm.ArchitectureSpecific;
016    import org.jikesrvm.VM;
017    import org.jikesrvm.mm.mminterface.MemoryManager;
018    import org.vmmagic.pragma.Uninterruptible;
019    
020    /**
021     * CodeArray represents a code object (contiguous memory region containing code).
022     * The types of the access methods are platform-dependent.
023     */
024    @Uninterruptible
025    public abstract class CodeArray {
026      private final byte[] data;
027    
028      public CodeArray(int size) {
029        if (VM.VerifyAssertions && VM.runningVM) VM._assert(VM.NOT_REACHED);  // should be unreachable
030        data = new byte[size];
031      }
032    
033      public byte get(int index) {
034        if (VM.VerifyAssertions && VM.runningVM) VM._assert(VM.NOT_REACHED);  // should be hijacked
035        return data[index];
036      }
037    
038      public void set(int index, byte v) {
039        if (VM.VerifyAssertions && VM.runningVM) VM._assert(VM.NOT_REACHED);  // should be hijacked
040        data[index] = v;
041      }
042    
043      public int length() {
044        if (VM.VerifyAssertions && VM.runningVM) VM._assert(VM.NOT_REACHED);  // should be hijacked
045        return data.length;
046      }
047    
048      public Object getBacking() {
049        if (!VM.writingImage) {
050          VM.sysFail("CodeArray.getBacking called when not writing boot image");
051        }
052        return data;
053      }
054    
055      /**
056       * A helper class to contain the 'real' methods of CodeArray.
057       * Because Jikes RVM believes that CodeArray is really a Code[]
058       * (i.e., an array of primitives), we cannot define non-hijacked methods
059       * on the 'class' CodeArray.
060       */
061      public static class Factory {
062        static {
063          Code x = null; // force compilation of Code wrapper class
064        }
065        /**
066         * Allocate a code array big enough to contain numInstrs instructions.
067         * @param numInstrs the number of instructions to copy from instrs
068         * @param isHot is this an allocation of code for a hot method?
069         * @return a CodeArray containing the instructions
070         */
071        public static ArchitectureSpecific.CodeArray create(int numInstrs, boolean isHot) {
072          if (VM.runningVM) {
073            return MemoryManager.allocateCode(numInstrs, isHot);
074          } else {
075            return ArchitectureSpecific.CodeArray.create(numInstrs);
076          }
077        }
078      }
079    }