ArrayDesc.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**********************************************************************/
  2. /* Array Descriptor class for j2cps */
  3. /* */
  4. /* (c) copyright QUT, John Gough 2000-2012, John Gough, 2012-2017 */
  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. /**
  13. * The type-descriptor of the element type of this array type
  14. */
  15. TypeDesc elemType;
  16. /**
  17. * The type-descriptor of the type that is a pointer to this array type
  18. */
  19. PtrDesc ptrType;
  20. int dim = 1;
  21. /**
  22. * The type-descriptor of the elements of this
  23. * (possibly multi-dimensional) array type.
  24. */
  25. TypeDesc ultimateElemType;
  26. public int elemTypeFixUp = 0;
  27. public ArrayDesc(int eF) {
  28. typeOrd = TypeDesc.arrT;
  29. name = "ARRAY OF " + eF;
  30. elemTypeFixUp = eF;
  31. writeDetails = true;
  32. }
  33. public ArrayDesc(int dimNum,TypeDesc eType,boolean makePtr) {
  34. this.name = "ARRAY OF ";
  35. this.writeDetails = true;
  36. for (int i=1; i < dimNum; i++) {
  37. this.name += "ARRAY OF ";
  38. }
  39. this.name += eType.name;
  40. this.typeOrd = TypeDesc.arrT;
  41. this.dim = dimNum;
  42. this.elemType = (dimNum == 1 ? eType : null);
  43. this.ultimateElemType = eType;
  44. if (makePtr) {
  45. this.ptrType = new PtrDesc(this);
  46. }
  47. }
  48. public void SetPtrType(PtrDesc ptrTy) {
  49. ptrType = ptrTy;
  50. }
  51. public static TypeDesc GetArrayTypeFromSig(
  52. String sig,int start,boolean getPtr) {
  53. TypeDesc uEType;
  54. if (sig.charAt(start) != '[') {
  55. System.out.println(sig.substring(start) + " is not an array type!");
  56. System.exit(1);
  57. }
  58. int dimCount = 0, ix = start;
  59. while (sig.charAt(ix) == '[') {
  60. ix++; dimCount++;
  61. }
  62. uEType = TypeDesc.GetType(sig,ix);
  63. ArrayDesc thisArr = FindOrCreateArrayType(dimCount,uEType,getPtr);
  64. ArrayDesc arrD = thisArr;
  65. while (--dimCount >= 1) {
  66. arrD.elemType = FindOrCreateArrayType(dimCount,uEType,true);
  67. if (arrD.elemType instanceof ArrayDesc) {
  68. arrD = (ArrayDesc)arrD.elemType;
  69. }
  70. }
  71. arrD.elemType = uEType;
  72. if (getPtr)
  73. return thisArr.ptrType;
  74. else
  75. return thisArr;
  76. }
  77. public static ArrayDesc FindOrCreateArrayType(
  78. int dimNum, TypeDesc eType, boolean mkPtr) {
  79. //
  80. // Try to find existing, identical array descriptor
  81. //
  82. for (int i=0; i < numArrayTypes; i++) {
  83. if ((arrayTypes[i].dim == dimNum) &&
  84. (arrayTypes[i].ultimateElemType == eType))
  85. {
  86. if (mkPtr && arrayTypes[i].ptrType == null)
  87. arrayTypes[i].ptrType = new PtrDesc(arrayTypes[i]);
  88. return arrayTypes[i];
  89. }
  90. }
  91. //
  92. // Otherwise allocate a new array descriptor
  93. //
  94. arrayTypes[numArrayTypes++] = new ArrayDesc(dimNum,eType,mkPtr);
  95. if (numArrayTypes == arrayTypes.length) {
  96. ArrayDesc[] temp = arrayTypes;
  97. arrayTypes = new ArrayDesc[numArrayTypes * 2];
  98. System.arraycopy(temp, 0, arrayTypes, 0, numArrayTypes);
  99. }
  100. return arrayTypes[numArrayTypes-1];
  101. }
  102. @Override
  103. public String getTypeMnemonic() {
  104. return 'a' + elemType.getTypeMnemonic();
  105. }
  106. /** Write an array definition the typelist section of a symbol file
  107. * <p>
  108. * An array type declaration consists of only an array marker
  109. * followed by the type-ordinal of the element type.
  110. *
  111. * @param out the symbol file output stream
  112. * @param thisPack the package which this symbol file describes
  113. * @throws IOException
  114. */
  115. @Override
  116. public void writeType(DataOutputStream out, PackageDesc thisPack)
  117. throws IOException {
  118. // Array = TypeHeader arrSy TypeOrd (Byte | Number | ) endAr.
  119. out.writeByte(SymbolFile.arrSy);
  120. SymbolFile.writeTypeOrd(out,elemType);
  121. out.writeByte(SymbolFile.endAr);
  122. }
  123. public void AddImportToArray(ClassDesc thisClass) {
  124. if (ultimateElemType instanceof ClassDesc) {
  125. thisClass.AddImportToClass((ClassDesc)ultimateElemType);
  126. }
  127. }
  128. }