ArrayDesc.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**********************************************************************/
  2. /* Array Descriptor class for J2CPS */
  3. /* */
  4. /* (c) copyright QUT */
  5. /**********************************************************************/
  6. package J2CPS;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. public class ArrayDesc extends TypeDesc {
  10. static ArrayDesc[] arrayTypes = new ArrayDesc[10];
  11. static int numArrayTypes = 0;
  12. TypeDesc elemType;
  13. PtrDesc ptrType;
  14. int dim = 1;
  15. TypeDesc ultimateElemType;
  16. public int elemTypeFixUp = 0;
  17. public ArrayDesc(int eF) {
  18. typeOrd = TypeDesc.arrT;
  19. name = "ARRAY OF " + eF;
  20. elemTypeFixUp = eF;
  21. writeDetails = true;
  22. }
  23. public ArrayDesc (int dimNum,TypeDesc eType,boolean makePtr) {
  24. name = "ARRAY OF ";
  25. writeDetails = true;
  26. for (int i=1; i < dimNum; i++) {
  27. name = name + "ARRAY OF ";
  28. }
  29. name = name + eType.name;
  30. typeOrd = TypeDesc.arrT;
  31. dim = dimNum;
  32. elemType = eType;
  33. ultimateElemType = eType;
  34. if (makePtr) {
  35. ptrType = new PtrDesc(this);
  36. }
  37. }
  38. public void SetPtrType(PtrDesc ptrTy) {
  39. ptrType = ptrTy;
  40. }
  41. public static TypeDesc GetArrayType(String sig,int start,boolean getPtr) {
  42. TypeDesc uEType;
  43. if (sig.charAt(start) != '[') {
  44. System.out.println(sig.substring(start) + " is not an array type!");
  45. System.exit(1);
  46. }
  47. int dimCount = 0, ix = start;
  48. while (sig.charAt(ix) == '[') { ix++; dimCount++; }
  49. uEType = TypeDesc.GetType(sig,ix);
  50. ArrayDesc thisArr = FindArrayType(dimCount,uEType,getPtr);
  51. dimCount--;
  52. ArrayDesc arrD = thisArr;
  53. while (dimCount > 1) {
  54. arrD.elemType = FindArrayType(dimCount,uEType,true);
  55. if (arrD.elemType instanceof ArrayDesc) {
  56. arrD = (ArrayDesc)arrD.elemType;
  57. }
  58. dimCount--;
  59. }
  60. arrD.elemType = uEType;
  61. if (getPtr) { return thisArr.ptrType; } else { return thisArr; }
  62. }
  63. public static ArrayDesc FindArrayType(int dimNum, TypeDesc eType,
  64. boolean mkPtr) {
  65. for (int i=0; i < numArrayTypes; i++) {
  66. if ((arrayTypes[i].dim == dimNum) &&
  67. (arrayTypes[i].ultimateElemType == eType)) {
  68. if (mkPtr && arrayTypes[i].ptrType == null) {
  69. arrayTypes[i].ptrType = new PtrDesc(arrayTypes[i]);
  70. }
  71. return arrayTypes[i];
  72. }
  73. }
  74. arrayTypes[numArrayTypes++] = new ArrayDesc(dimNum,eType,mkPtr);
  75. if (numArrayTypes == arrayTypes.length) {
  76. ArrayDesc[] temp = arrayTypes;
  77. arrayTypes = new ArrayDesc[numArrayTypes * 2];
  78. System.arraycopy(temp, 0, arrayTypes, 0, numArrayTypes);
  79. }
  80. return arrayTypes[numArrayTypes-1];
  81. }
  82. @Override
  83. public String getTypeMnemonic() {
  84. return 'a' + elemType.getTypeMnemonic();
  85. }
  86. @Override
  87. public void writeType(DataOutputStream out, PackageDesc thisPack)
  88. throws IOException {
  89. // Array = TypeHeader arrSy TypeOrd (Byte | Number | ) endAr.
  90. out.writeByte(SymbolFile.arrSy);
  91. SymbolFile.writeTypeOrd(out,elemType);
  92. out.writeByte(SymbolFile.endAr);
  93. }
  94. public void AddImport(ClassDesc thisClass) {
  95. if (ultimateElemType instanceof ClassDesc) {
  96. thisClass.AddImport((ClassDesc)ultimateElemType);
  97. }
  98. }
  99. }