ArrayDesc.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ultimateElemType = eType;
  33. if (makePtr) {
  34. ptrType = new PtrDesc(this);
  35. }
  36. }
  37. public void SetPtrType(PtrDesc ptrTy) {
  38. ptrType = ptrTy;
  39. }
  40. public static TypeDesc GetArrayType(String sig,int start,boolean getPtr) {
  41. TypeDesc uEType;
  42. if (sig.charAt(start) != '[') {
  43. System.out.println(sig.substring(start) + " is not an array type!");
  44. System.exit(1);
  45. }
  46. int dimCount = 0, ix = start;
  47. while (sig.charAt(ix) == '[') { ix++; dimCount++; }
  48. uEType = TypeDesc.GetType(sig,ix);
  49. ArrayDesc thisArr = FindArrayType(dimCount,uEType,getPtr);
  50. dimCount--;
  51. ArrayDesc arrD = thisArr;
  52. while (dimCount > 1) {
  53. arrD.elemType = FindArrayType(dimCount,uEType,true);
  54. if (arrD.elemType instanceof ArrayDesc) {
  55. arrD = (ArrayDesc)arrD.elemType;
  56. }
  57. dimCount--;
  58. }
  59. arrD.elemType = uEType;
  60. if (getPtr) { return thisArr.ptrType; } else { return thisArr; }
  61. }
  62. public static ArrayDesc FindArrayType(int dimNum, TypeDesc eType,
  63. boolean mkPtr) {
  64. for (int i=0; i < numArrayTypes; i++) {
  65. if ((arrayTypes[i].dim == dimNum) &&
  66. (arrayTypes[i].ultimateElemType == eType)) {
  67. if (mkPtr && arrayTypes[i].ptrType == null) {
  68. arrayTypes[i].ptrType = new PtrDesc(arrayTypes[i]);
  69. }
  70. return arrayTypes[i];
  71. }
  72. }
  73. arrayTypes[numArrayTypes++] = new ArrayDesc(dimNum,eType,mkPtr);
  74. if (numArrayTypes == arrayTypes.length) {
  75. ArrayDesc[] temp = arrayTypes;
  76. arrayTypes = new ArrayDesc[numArrayTypes * 2];
  77. System.arraycopy(temp, 0, arrayTypes, 0, numArrayTypes);
  78. }
  79. return arrayTypes[numArrayTypes-1];
  80. }
  81. @Override
  82. public String getTypeMnemonic() {
  83. return 'a' + elemType.getTypeMnemonic();
  84. }
  85. @Override
  86. public void writeType(DataOutputStream out, PackageDesc thisPack)
  87. throws IOException {
  88. // Array = TypeHeader arrSy TypeOrd (Byte | Number | ) endAr.
  89. out.writeByte(SymbolFile.arrSy);
  90. SymbolFile.writeTypeOrd(out,elemType);
  91. out.writeByte(SymbolFile.endAr);
  92. }
  93. public void AddImport(ClassDesc thisClass) {
  94. if (ultimateElemType instanceof ClassDesc) {
  95. thisClass.AddImport((ClassDesc)ultimateElemType);
  96. }
  97. }
  98. }