2
0

CPH.txt 10 KB

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