2
0

CPH.txt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. MODULE LindevCPH;
  2. (* THIS IS TEXT COPY OF CPH.odc *)
  3. (* DO NOT EDIT *)
  4. IMPORT DevCPT := LindevCPT;
  5. CONST
  6. (* UseCalls options *)
  7. longMop* = 0; longDop* = 1; longConv* = 2; longOdd* = 3;
  8. realMop* = 8; realDop* = 9; realConv* = 10;
  9. intMulDiv* = 11;
  10. force = 16; hide = 17;
  11. (* nodes classes *)
  12. Nvar = 0; Nvarpar = 1; Nfield = 2; Nderef = 3; Nindex = 4; Nguard = 5; Neguard = 6;
  13. Nconst = 7; Ntype = 8; Nproc = 9; Nupto = 10; Nmop = 11; Ndop = 12; Ncall = 13;
  14. Ninittd = 14; Nif = 15; Ncaselse = 16; Ncasedo = 17; Nenter = 18; Nassign = 19;
  15. Nifelse =20; Ncase = 21; Nwhile = 22; Nrepeat = 23; Nloop = 24; Nexit = 25;
  16. Nreturn = 26; Nwith = 27; Ntrap = 28; Ncomp = 30;
  17. Ndrop = 50; Nlabel = 51; Ngoto = 52; Njsr = 53; Nret = 54; Ncmp = 55;
  18. (*function number*)
  19. assign = 0; newfn = 1; incfn = 13; decfn = 14;
  20. inclfn = 15; exclfn = 16; copyfn = 18; assertfn = 32;
  21. getfn = 24; putfn = 25; getrfn = 26; putrfn = 27; sysnewfn = 30; movefn = 31;
  22. (* symbol values and ops *)
  23. times = 1; slash = 2; div = 3; mod = 4;
  24. and = 5; plus = 6; minus = 7; or = 8; eql = 9;
  25. neq = 10; lss = 11; leq = 12; gtr = 13; geq = 14;
  26. in = 15; is = 16; ash = 17; msk = 18; len = 19;
  27. conv = 20; abs = 21; cap = 22; odd = 23; not = 33;
  28. adr = 24; cc = 25; bit = 26; lsh = 27; rot = 28; val = 29;
  29. min = 34; max = 35; typfn = 36;
  30. thisrecfn = 45; thisarrfn = 46;
  31. shl = 50; shr = 51; lshr = 52; xor = 53;
  32. (* structure forms *)
  33. Undef = 0; Byte = 1; Bool = 2; Char8 = 3; Int8 = 4; Int16 = 5; Int32 = 6;
  34. Real32 = 7; Real64 = 8; Set = 9; String8 = 10; NilTyp = 11; NoTyp = 12;
  35. Pointer = 13; ProcTyp = 14; Comp = 15;
  36. Char16 = 16; String16 = 17; Int64 = 18;
  37. VString16to8 = 29; VString8 = 30; VString16 = 31;
  38. realSet = {Real32, Real64};
  39. Basic = 1; Array = 2; DynArr = 3; Record = 4;
  40. PROCEDURE UseThisCall (n: DevCPT.Node; IN name: ARRAY OF SHORTCHAR);
  41. VAR mod, nm, moda: DevCPT.Name; mobj, obj: DevCPT.Object; done: BOOLEAN;
  42. BEGIN
  43. IF (n.typ.form = Real64) OR (n.left.typ.form = Real64) THEN mod := "Real"
  44. ELSIF (n.typ.form = Real32) OR (n.left.typ.form = Real32) THEN mod := "SReal"
  45. ELSIF (n.typ.form = Int64) OR (n.left.typ.form = Int64) THEN mod := "Long"
  46. ELSE mod := "Int"
  47. END;
  48. moda := mod + "%";
  49. DevCPT.Find(moda, mobj);
  50. IF mobj = NIL THEN
  51. DevCPT.Import(moda, mod, done);
  52. IF done THEN DevCPT.Find(moda, mobj) END
  53. END;
  54. nm := name$; DevCPT.FindImport(nm, mobj, obj);
  55. n.class := Ncall; n.subcl := 0; n.obj := obj.link;
  56. n.left.link := n.right; n.right := n.left;
  57. n.left := DevCPT.NewNode(Nproc);
  58. n.left.obj := obj; n.left.typ := obj.typ;
  59. ASSERT(n.typ.form = obj.typ.form)
  60. END UseThisCall;
  61. PROCEDURE Convert (n: DevCPT.Node; typ: DevCPT.Struct);
  62. VAR new: DevCPT.Node; r: REAL;
  63. BEGIN
  64. IF n.class = Nconst THEN
  65. ASSERT((n.typ.form IN {Int32, Int64}) & (typ = DevCPT.intrealtyp));
  66. r := n.conval.realval + n.conval.intval;
  67. IF r = n.conval.realval + n.conval.intval THEN
  68. n.conval.realval := r; n.conval.intval := -1; n.typ := typ; n.obj := NIL
  69. END
  70. END;
  71. IF (n.typ # typ)
  72. & ((n.class # Nmop) OR (n.subcl # conv)
  73. OR ~DevCPT.Includes(n.typ.form, n.left.typ.form) & ~DevCPT.Includes(n.typ.form, typ.form)) THEN
  74. new := DevCPT.NewNode(0); new^ := n^;
  75. n.class := Nmop; n.subcl := conv; n.left := new; n.right := NIL; n.obj := NIL
  76. END;
  77. n.typ := typ
  78. END Convert;
  79. PROCEDURE UseCallForComp (n: DevCPT.Node);
  80. VAR new: DevCPT.Node;
  81. BEGIN
  82. new := DevCPT.NewNode(0);
  83. new.left := n.left; new.right := n.right;
  84. new.typ := DevCPT.int32typ;
  85. UseThisCall(new, "Comp");
  86. n.left := new;
  87. n.right := DevCPT.NewNode(Nconst); n.right.conval := DevCPT.NewConst();
  88. n.right.conval.intval := 0; n.right.conval.realval := 0; n.right.typ := DevCPT.int32typ;
  89. END UseCallForComp;
  90. PROCEDURE UseCallForConv (n: DevCPT.Node; opts: SET);
  91. VAR f, g: INTEGER; typ: DevCPT.Struct;
  92. BEGIN
  93. typ := n.typ; f := typ.form; g := n.left.typ.form;
  94. IF realConv IN opts THEN
  95. IF f IN realSet THEN
  96. IF g = Real32 THEN UseThisCall(n, "Long")
  97. ELSIF g = Real64 THEN UseThisCall(n, "Short")
  98. ELSIF g = Int64 THEN UseThisCall(n, "LFloat")
  99. ELSIF g = Int32 THEN UseThisCall(n, "Float")
  100. ELSE Convert(n.left, DevCPT.int32typ); UseThisCall(n, "Float")
  101. END
  102. ELSIF g IN realSet THEN
  103. IF f = Int64 THEN UseThisCall(n, "LFloor")
  104. ELSIF f = Int32 THEN UseThisCall(n, "Floor")
  105. ELSE n.typ := DevCPT.int32typ; UseThisCall(n, "Floor"); Convert(n, typ)
  106. END
  107. END
  108. END;
  109. IF longConv IN opts THEN
  110. IF f = Int64 THEN
  111. IF g = Int32 THEN UseThisCall(n, "Long")
  112. ELSIF ~(g IN realSet) THEN Convert(n.left, DevCPT.int32typ); UseThisCall(n, "IntToLong")
  113. END
  114. ELSIF g = Int64 THEN
  115. IF f = Int32 THEN UseThisCall(n, "Short")
  116. ELSIF ~(f IN realSet) THEN n.typ := DevCPT.int32typ; UseThisCall(n, "LongToInt"); Convert(n, typ)
  117. END
  118. END
  119. END
  120. END UseCallForConv;
  121. PROCEDURE UseCallForMop (n: DevCPT.Node; opts: SET);
  122. BEGIN
  123. CASE n.subcl OF
  124. | minus:
  125. IF (realMop IN opts) & (n.typ.form IN realSet) OR (longMop IN opts) & (n.typ.form = Int64) THEN
  126. UseThisCall(n, "Neg")
  127. END
  128. | abs:
  129. IF (realMop IN opts) & (n.typ.form IN realSet) OR (longMop IN opts) & (n.typ.form = Int64) THEN
  130. UseThisCall(n, "Abs")
  131. END
  132. | odd:
  133. IF (longOdd IN opts) & (n.left.typ.form = Int64) THEN UseThisCall(n, "Odd") END
  134. | conv:
  135. UseCallForConv(n, opts)
  136. ELSE
  137. END
  138. END UseCallForMop;
  139. PROCEDURE UseCallForDop (n: DevCPT.Node; opts: SET);
  140. BEGIN
  141. IF (realDop IN opts) & (n.left.typ.form IN realSet)
  142. OR (longDop IN opts) & (n.left.typ.form = Int64)
  143. OR (intMulDiv IN opts) & (n.subcl IN {times, div, mod}) & (n.typ.form = Int32) THEN
  144. CASE n.subcl OF
  145. | times: UseThisCall(n, "Mul")
  146. | slash: UseThisCall(n, "Div")
  147. | div: UseThisCall(n, "Div")
  148. | mod: UseThisCall(n, "Mod")
  149. | plus: UseThisCall(n, "Add")
  150. | minus: UseThisCall(n, "Sub")
  151. | ash: UseThisCall(n, "Ash")
  152. | min: UseThisCall(n, "Min")
  153. | max: UseThisCall(n, "Max")
  154. | eql..geq: UseCallForComp(n)
  155. ELSE
  156. END
  157. END
  158. END UseCallForDop;
  159. PROCEDURE UseCallForMove (n: DevCPT.Node; typ: DevCPT.Struct; opts: SET);
  160. VAR f, g: INTEGER;
  161. BEGIN
  162. f := n.typ.form; g := typ.form;
  163. IF f # g THEN
  164. IF (realConv IN opts) & ((f IN realSet) OR (g IN realSet))
  165. OR (longConv IN opts) & ((f = Int64) OR (g = Int64)) THEN
  166. Convert(n, typ);
  167. UseCallForConv(n, opts)
  168. END
  169. END
  170. END UseCallForMove;
  171. PROCEDURE UseCallForAssign (n: DevCPT.Node; opts: SET);
  172. BEGIN
  173. IF n.subcl = assign THEN UseCallForMove(n.right, n.left.typ, opts) END
  174. END UseCallForAssign;
  175. PROCEDURE UseCallForReturn (n: DevCPT.Node; opts: SET);
  176. BEGIN
  177. IF (n.left # NIL) & (n.obj # NIL) THEN UseCallForMove(n.left, n.obj.typ, opts) END
  178. END UseCallForReturn;
  179. PROCEDURE UseCallForParam (n: DevCPT.Node; fp: DevCPT.Object; opts: SET);
  180. BEGIN
  181. WHILE n # NIL DO
  182. UseCallForMove(n, fp.typ, opts);
  183. n := n.link; fp := fp.link
  184. END
  185. END UseCallForParam;
  186. PROCEDURE UseCalls* (n: DevCPT.Node; opts: SET);
  187. BEGIN
  188. WHILE n # NIL DO
  189. CASE n.class OF
  190. | Nmop:
  191. UseCalls(n.left, opts); UseCallForMop(n, opts)
  192. | Ndop:
  193. UseCalls(n.left, opts); UseCalls(n.right, opts); UseCallForDop(n, opts)
  194. | Ncase:
  195. UseCalls(n.left, opts); UseCalls(n.right.left, opts); UseCalls(n.right.right, opts)
  196. | Nassign:
  197. UseCalls(n.left, opts); UseCalls(n.right, opts); UseCallForAssign(n, opts)
  198. | Ncall:
  199. UseCalls(n.left, opts); UseCalls(n.right, opts); UseCallForParam(n.right, n.obj, opts)
  200. | Nreturn:
  201. UseCalls(n.left, opts); UseCallForReturn(n, opts)
  202. | Ncasedo:
  203. UseCalls(n.right, opts)
  204. | Ngoto, Ndrop, Nloop, Nfield, Nderef, Nguard:
  205. UseCalls(n.left, opts)
  206. | Nenter, Nifelse, Nif, Nwhile, Nrepeat, Nwith, Ncomp, Nupto, Nindex:
  207. UseCalls(n.left, opts); UseCalls(n.right, opts)
  208. | Njsr, Nret, Nlabel, Ntrap, Nexit, Ninittd, Ntype, Nproc, Nconst, Nvar, Nvarpar:
  209. END;
  210. n := n.link
  211. END
  212. END UseCalls;
  213. PROCEDURE UseReals* (n: DevCPT.Node; opts: SET);
  214. BEGIN
  215. WHILE n # NIL DO
  216. CASE n.class OF
  217. | Nmop:
  218. IF (longMop IN opts) & (n.typ.form = Int64) & ((n.subcl = abs) OR (n.subcl = minus)) THEN
  219. UseReals(n.left, opts - {hide} + {force}); n.typ := DevCPT.intrealtyp
  220. ELSIF n.subcl = conv THEN UseReals(n.left, opts - {force} + {hide})
  221. ELSE UseReals(n.left, opts - {force, hide})
  222. END
  223. | Ndop:
  224. IF (longDop IN opts) & (n.left.typ.form = Int64) THEN
  225. UseReals(n.left, opts - {hide} + {force}); UseReals(n.right, opts - {hide} + {force});
  226. IF n.typ.form = Int64 THEN n.typ := DevCPT.intrealtyp END
  227. ELSE UseReals(n.left, opts - {force, hide}); UseReals(n.right, opts - {force, hide})
  228. END
  229. | Ncase:
  230. UseReals(n.left, opts - {force, hide}); UseReals(n.right.left, opts - {force, hide});
  231. UseReals(n.right.right, opts - {force, hide})
  232. | Ncasedo:
  233. UseReals(n.right, opts - {force, hide})
  234. | Ngoto, Ndrop, Nloop, Nreturn, Nfield, Nderef, Nguard:
  235. UseReals(n.left, opts - {force, hide})
  236. | Nenter, Nassign, Ncall, Nifelse, Nif, Nwhile, Nrepeat, Nwith, Ncomp, Nupto, Nindex:
  237. UseReals(n.left, opts - {force, hide}); UseReals(n.right, opts - {force, hide})
  238. | Njsr, Nret, Nlabel, Ntrap, Nexit, Ninittd, Ntype, Nproc, Nconst, Nvar, Nvarpar:
  239. END;
  240. IF force IN opts THEN Convert(n, DevCPT.intrealtyp)
  241. ELSIF ~(hide IN opts) & (n.typ = DevCPT.intrealtyp) THEN Convert(n, DevCPT.int64typ)
  242. END;
  243. n := n.link
  244. END
  245. END UseReals;
  246. END LindevCPH.
  247. PROCEDURE Traverse (n: DevCPT.Node; opts: SET);
  248. BEGIN
  249. WHILE n # NIL DO
  250. CASE n.class OF
  251. | Ncase:
  252. Traverse(n.left, opts); Traverse(n.right.left, opts); Traverse(n.right.right, opts)
  253. | Ncasedo:
  254. Traverse(n.right, opts)
  255. | Ngoto, Ndrop, Nloop, Nreturn, Nmop, Nfield, Nderef, Nguard:
  256. Traverse(n.left, opts)
  257. | Nenter, Nassign, Ncall, Nifelse, Nif, Nwhile, Nrepeat, Nwith, Ncomp, Ndop, Nupto, Nindex:
  258. Traverse(n.left, opts); Traverse(n.right, opts)
  259. | Njsr, Nret, Nlabel, Ntrap, Nexit, Ninittd, Ntype, Nproc, Nconst, Nvar, Nvarpar:
  260. END;
  261. n := n.link
  262. END
  263. END Traverse;