ArrayDesc.java 3.3 KB

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