n2state.cp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. (* ================================================================ *)
  2. (* *)
  3. (* Module of the V1.4+ gpcp tool to create symbol files from *)
  4. (* the metadata of .NET assemblies, using the PERWAPI interface. *)
  5. (* *)
  6. (* Copyright QUT 2004 - 2005. *)
  7. (* *)
  8. (* This code released under the terms of the GPCP licence. *)
  9. (* *)
  10. (* This Module: <N2State> *)
  11. (* Holds global state for the process, plus utilities. *)
  12. (* Original module, kjg December 2004 *)
  13. (* *)
  14. (* ================================================================ *)
  15. MODULE N2State;
  16. IMPORT GPCPcopyright,
  17. CompState,
  18. LitValue,
  19. ProgArgs,
  20. Console,
  21. Error,
  22. GPText,
  23. ForeignName,
  24. RW := NewSymFileRW,
  25. Id := IdDesc,
  26. Ty := TypeDesc,
  27. Sy := Symbols,
  28. Nh := NameHash,
  29. Sys := "[mscorlib]System",
  30. RTS;
  31. (* ---------------------------------------------------------- *)
  32. CONST prefix = "PeToCps: ";
  33. abtMsg = " ... Aborting";
  34. usgMsg = 'Usage: "PeToCps [options] filenames"';
  35. (* ---------------------------------------------------------- *)
  36. TYPE CharOpen* = POINTER TO ARRAY OF CHAR;
  37. (* ---------------------------------------------------------- *)
  38. TYPE CleanDump = POINTER TO RECORD (Sy.SymForAll) END;
  39. (* ---------------------------------------------------------- *)
  40. VAR netDflt- : BOOLEAN;
  41. verbose- : BOOLEAN;
  42. Verbose- : BOOLEAN;
  43. superVb- : BOOLEAN;
  44. generics- : BOOLEAN;
  45. cpCmpld- : BOOLEAN;
  46. (* ---------------------------------------------------------- *)
  47. VAR thisMod- : Id.BlkId;
  48. isCorLib- : BOOLEAN;
  49. hashSize- : INTEGER;
  50. ctorBkt- : INTEGER;
  51. initBkt- : INTEGER;
  52. srcNam- : CharOpen;
  53. basNam- : CharOpen;
  54. impSeq* : Sy.ScpSeq;
  55. typSeq- : Sy.TypeSeq;
  56. (* ---------------------------------------------------------- *)
  57. PROCEDURE^ AbortMsg*(IN str : ARRAY OF CHAR);
  58. PROCEDURE^ Message*(IN str : ARRAY OF CHAR);
  59. (* ---------------------------------------------------------- *)
  60. PROCEDURE ListTy*(ty : Sy.Type);
  61. BEGIN
  62. Sy.AppendType(typSeq, ty);
  63. END ListTy;
  64. (* ---------------------------------------------------------- *)
  65. PROCEDURE AddMangledNames(mod : Id.BlkId; asm, nms : CharOpen);
  66. BEGIN
  67. mod.hash := Nh.enterStr(ForeignName.MangledName(asm, nms));
  68. mod.scopeNm := ForeignName.QuotedName(asm, nms);
  69. END AddMangledNames;
  70. (* ---------------------------------------------------------- *)
  71. PROCEDURE GlobInit*(IN src, bas : ARRAY OF CHAR);
  72. BEGIN
  73. Nh.InitNameHash(hashSize);
  74. srcNam := BOX(src$);
  75. basNam := BOX(bas$);
  76. isCorLib := (bas = "mscorlib");
  77. CompState.CreateThisMod;
  78. thisMod := CompState.thisMod;
  79. Sy.ResetScpSeq(impSeq);
  80. ctorBkt := Nh.enterStr(".ctor");
  81. initBkt := Nh.enterStr("init");
  82. END GlobInit;
  83. (* ------------------------------------- *)
  84. PROCEDURE BlkIdInit*(blk : Id.BlkId; asm, nms : CharOpen);
  85. BEGIN
  86. blk.SetKind(Id.impId);
  87. AddMangledNames(blk, asm, nms);
  88. IF Sy.refused(blk, thisMod) THEN
  89. AbortMsg("BlkId insert failure -- " + Nh.charOpenOfHash(blk.hash)^);
  90. END;
  91. Sy.AppendScope(impSeq, blk)
  92. END BlkIdInit;
  93. (* ------------------------------------- *)
  94. PROCEDURE InsertImport*(blk : Id.BlkId);
  95. BEGIN
  96. IF Sy.refused(blk, thisMod) THEN
  97. AbortMsg("BlkId insert failure in <thisMod>");
  98. END;
  99. Sy.AppendScope(impSeq, blk)
  100. END InsertImport;
  101. (* ------------------------------------- *)
  102. PROCEDURE (dmpr : CleanDump)Op*(id : Sy.Idnt);
  103. BEGIN
  104. WITH id : Id.TypId DO
  105. IF id.type.dump >= Sy.tOffset THEN id.type.dump := 0 END;
  106. ELSE
  107. END;
  108. END Op;
  109. (* ------------------------------------- *)
  110. PROCEDURE ResetBlkIdFlags*(mod : Id.BlkId);
  111. VAR indx : INTEGER;
  112. impB : Sy.Scope;
  113. dmpr : CleanDump;
  114. typI : Sy.Type;
  115. BEGIN
  116. (*
  117. * Clear the "dump" marker from non built-in types
  118. *)
  119. FOR indx := 0 TO typSeq.tide - 1 DO
  120. typI := typSeq.a[indx];
  121. IF typI.dump >= Sy.tOffset THEN
  122. typI.dump := 0;
  123. typI.force := Sy.noEmit;
  124. END;
  125. END;
  126. mod.SetKind(Id.modId);
  127. IF superVb THEN
  128. Message("Preparing symfile <" + Nh.charOpenOfHash(mod.hash)^ + ">");
  129. END;
  130. FOR indx := 0 TO impSeq.tide - 1 DO
  131. impB := impSeq.a[indx];
  132. IF impB # mod THEN
  133. impB.SetKind(Id.impId);
  134. END;
  135. END;
  136. END ResetBlkIdFlags;
  137. (* ---------------------------------------------------------- *)
  138. PROCEDURE WLn(IN str : ARRAY OF CHAR);
  139. BEGIN
  140. Console.WriteString(str); Console.WriteLn;
  141. END WLn;
  142. PROCEDURE Message*(IN str : ARRAY OF CHAR);
  143. BEGIN
  144. Console.WriteString(prefix);
  145. Console.WriteString(str);
  146. Console.WriteLn;
  147. END Message;
  148. PROCEDURE CondMsg*(IN str : ARRAY OF CHAR);
  149. BEGIN
  150. IF verbose THEN Message(str) END;
  151. END CondMsg;
  152. PROCEDURE AbortMsg*(IN str : ARRAY OF CHAR);
  153. BEGIN
  154. Error.WriteString(prefix);
  155. Error.WriteString(str);
  156. Error.WriteLn;
  157. HALT(1);
  158. END AbortMsg;
  159. PROCEDURE Usage();
  160. BEGIN
  161. Message(usgMsg);
  162. Message("filenames should have explicit .EXE or .DLL extension");
  163. IF netDflt THEN
  164. WLn("Options: /big ==> allocate huge hash table");
  165. WLn(" /copyright ==> display copyright notice");
  166. WLn(" /help ==> display this message");
  167. WLn(" /verbose ==> chatter on about progress");
  168. WLn(" /Verbose ==> go on and on and on about progress");
  169. ELSE
  170. WLn("Options: -big ==> allocate huge hash table");
  171. WLn(" -copyright ==> display copyright notice");
  172. WLn(" -help ==> display this message");
  173. WLn(" -verbose ==> chatter on about progress");
  174. WLn(" -Verbose ==> go on and on and on about progress");
  175. END;
  176. END Usage;
  177. (* ---------------------------------------------------------- *)
  178. PROCEDURE ReportTim(tim : LONGINT);
  179. CONST millis = " mSec";
  180. BEGIN
  181. Console.WriteInt(SHORT(tim), 0);
  182. Console.WriteString(millis);
  183. END ReportTim;
  184. PROCEDURE Report*(IN nam, res : ARRAY OF CHAR; tim : LONGINT);
  185. BEGIN
  186. Console.WriteString(prefix);
  187. Console.WriteString(" Input file <");
  188. Console.WriteString(nam);
  189. Console.WriteString("> ");
  190. Console.WriteString(res);
  191. IF verbose THEN
  192. Console.WriteString(", time: ");
  193. ReportTim(tim);
  194. END;
  195. Console.WriteLn;
  196. END Report;
  197. PROCEDURE Summary*(flNm, okNm : INTEGER; tim : LONGINT);
  198. CONST sumPre = " Summary: ";
  199. BEGIN
  200. Console.WriteString(prefix);
  201. Console.WriteString(sumPre);
  202. IF flNm = 0 THEN
  203. Console.WriteString(" No input files specified");
  204. ELSE
  205. Console.WriteInt(flNm,1);
  206. Console.WriteString(" input files");
  207. IF okNm < flNm THEN
  208. Console.WriteInt(flNm - okNm, 0);
  209. Console.WriteString(" failed");
  210. END;
  211. IF verbose THEN
  212. Console.WriteLn;
  213. Console.WriteString(prefix);
  214. Console.WriteString(sumPre);
  215. Console.WriteString("Total elapsed time: ");
  216. ReportTim(tim);
  217. END;
  218. END;
  219. Console.WriteLn;
  220. END Summary;
  221. (* ---------------------------------------------------------- *)
  222. PROCEDURE ParseOption*(IN arg : ARRAY OF CHAR);
  223. BEGIN
  224. IF arg = "-big" THEN
  225. hashSize := 40000;
  226. ELSIF arg = "-verbose" THEN
  227. verbose := TRUE;
  228. Verbose := FALSE;
  229. superVb := FALSE;
  230. ELSIF arg = "-Verbose" THEN
  231. verbose := TRUE;
  232. Verbose := TRUE;
  233. superVb := FALSE;
  234. (*
  235. * ELSIF arg = "-generics" THEN
  236. * generics := TRUE;
  237. *)
  238. ELSIF arg = "-VERBOSE" THEN
  239. verbose := TRUE;
  240. Verbose := TRUE;
  241. superVb := TRUE;
  242. ELSIF arg = "-help" THEN
  243. Usage();
  244. ELSIF arg = "-copyright" THEN
  245. GPCPcopyright.Write();
  246. ELSE
  247. Message("Bad Option " + arg); Usage;
  248. END;
  249. END ParseOption;
  250. (* ---------------------------------------------------------- *)
  251. PROCEDURE EmitSymbolfile*(blk : Id.BlkId);
  252. BEGIN
  253. RW.EmitSymfile(blk);
  254. Message(" Output file <" +
  255. Nh.charOpenOfHash(blk.hash)^ +
  256. ".cps> created");
  257. END EmitSymbolfile;
  258. (* ---------------------------------------------------------- *)
  259. BEGIN
  260. netDflt := (RTS.defaultTarget = "net");
  261. generics := FALSE;
  262. verbose := FALSE;
  263. Verbose := FALSE;
  264. superVb := FALSE;
  265. cpCmpld := FALSE; (* pending the custom attribute *)
  266. hashSize := 5000;
  267. Sy.InitScpSeq(impSeq, 10);
  268. Sy.InitTypeSeq(typSeq, 10);
  269. CompState.ParseOption("/special");
  270. END N2State.