guards.nim 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the 'implies' relation for guards.
  10. import ast, astalgo, msgs, magicsys, nimsets, trees, types, renderer, idents,
  11. saturate, modulegraphs, options, lineinfos, int128
  12. const
  13. someEq = {mEqI, mEqF64, mEqEnum, mEqCh, mEqB, mEqRef, mEqProc,
  14. mEqStr, mEqSet, mEqCString}
  15. # set excluded here as the semantics are vastly different:
  16. someLe = {mLeI, mLeF64, mLeU, mLeEnum,
  17. mLeCh, mLeB, mLePtr, mLeStr}
  18. someLt = {mLtI, mLtF64, mLtU, mLtEnum,
  19. mLtCh, mLtB, mLtPtr, mLtStr}
  20. someLen = {mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq}
  21. someIn = {mInSet}
  22. someHigh = {mHigh}
  23. # we don't list unsigned here because wrap around semantics suck for
  24. # proving anything:
  25. someAdd = {mAddI, mAddF64, mSucc}
  26. someSub = {mSubI, mSubF64, mPred}
  27. someMul = {mMulI, mMulF64}
  28. someDiv = {mDivI, mDivF64}
  29. someMod = {mModI}
  30. someMax = {mMaxI}
  31. someMin = {mMinI}
  32. someBinaryOp = someAdd+someSub+someMul+someMax+someMin
  33. proc isValue(n: PNode): bool = n.kind in {nkCharLit..nkNilLit}
  34. proc isLocation(n: PNode): bool = not n.isValue
  35. proc isLet(n: PNode): bool =
  36. if n.kind == nkSym:
  37. if n.sym.kind in {skLet, skTemp, skForVar}:
  38. result = true
  39. elif n.sym.kind == skParam and skipTypes(n.sym.typ,
  40. abstractInst).kind != tyVar:
  41. result = true
  42. proc isVar(n: PNode): bool =
  43. n.kind == nkSym and n.sym.kind in {skResult, skVar} and
  44. {sfAddrTaken} * n.sym.flags == {}
  45. proc isLetLocation(m: PNode, isApprox: bool): bool =
  46. # consider: 'n[].kind' --> we really need to support 1 deref op even if this
  47. # is technically wrong due to aliasing :-( We could introduce "soft" facts
  48. # for this; this would still be very useful for warnings and also nicely
  49. # solves the 'var' problems. For now we fix this by requiring much more
  50. # restrictive expressions for the 'not nil' checking.
  51. var n = m
  52. var derefs = 0
  53. while true:
  54. case n.kind
  55. of nkDotExpr, nkCheckedFieldExpr, nkObjUpConv, nkObjDownConv:
  56. n = n[0]
  57. of nkDerefExpr, nkHiddenDeref:
  58. n = n[0]
  59. inc derefs
  60. of nkBracketExpr:
  61. if isConstExpr(n[1]) or isLet(n[1]) or isConstExpr(n[1].skipConv):
  62. n = n[0]
  63. else: return
  64. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  65. n = n[1]
  66. else:
  67. break
  68. result = n.isLet and derefs <= ord(isApprox)
  69. if not result and isApprox:
  70. result = isVar(n)
  71. proc interestingCaseExpr*(m: PNode): bool = isLetLocation(m, true)
  72. type
  73. Operators* = object
  74. opNot, opContains, opLe, opLt, opAnd, opOr, opIsNil, opEq: PSym
  75. opAdd, opSub, opMul, opDiv, opLen: PSym
  76. proc initOperators*(g: ModuleGraph): Operators =
  77. result.opLe = createMagic(g, "<=", mLeI)
  78. result.opLt = createMagic(g, "<", mLtI)
  79. result.opAnd = createMagic(g, "and", mAnd)
  80. result.opOr = createMagic(g, "or", mOr)
  81. result.opIsNil = createMagic(g, "isnil", mIsNil)
  82. result.opEq = createMagic(g, "==", mEqI)
  83. result.opAdd = createMagic(g, "+", mAddI)
  84. result.opSub = createMagic(g, "-", mSubI)
  85. result.opMul = createMagic(g, "*", mMulI)
  86. result.opDiv = createMagic(g, "div", mDivI)
  87. result.opLen = createMagic(g, "len", mLengthSeq)
  88. result.opNot = createMagic(g, "not", mNot)
  89. result.opContains = createMagic(g, "contains", mInSet)
  90. proc swapArgs(fact: PNode, newOp: PSym): PNode =
  91. result = newNodeI(nkCall, fact.info, 3)
  92. result[0] = newSymNode(newOp)
  93. result[1] = fact[2]
  94. result[2] = fact[1]
  95. proc neg(n: PNode; o: Operators): PNode =
  96. if n == nil: return nil
  97. case n.getMagic
  98. of mNot:
  99. result = n[1]
  100. of someLt:
  101. # not (a < b) == a >= b == b <= a
  102. result = swapArgs(n, o.opLe)
  103. of someLe:
  104. result = swapArgs(n, o.opLt)
  105. of mInSet:
  106. if n[1].kind != nkCurly: return nil
  107. let t = n[2].typ.skipTypes(abstractInst)
  108. result = newNodeI(nkCall, n.info, 3)
  109. result[0] = n[0]
  110. result[2] = n[2]
  111. if t.kind == tyEnum:
  112. var s = newNodeIT(nkCurly, n.info, n[1].typ)
  113. for e in t.n:
  114. let eAsNode = newIntNode(nkIntLit, e.sym.position)
  115. if not inSet(n[1], eAsNode): s.add eAsNode
  116. result[1] = s
  117. #elif t.kind notin {tyString, tySequence} and lengthOrd(t) < 1000:
  118. # result[1] = complement(n[1])
  119. else:
  120. # not ({2, 3, 4}.contains(x)) x != 2 and x != 3 and x != 4
  121. # XXX todo
  122. result = nil
  123. of mOr:
  124. # not (a or b) --> not a and not b
  125. let
  126. a = n[1].neg(o)
  127. b = n[2].neg(o)
  128. if a != nil and b != nil:
  129. result = newNodeI(nkCall, n.info, 3)
  130. result[0] = newSymNode(o.opAnd)
  131. result[1] = a
  132. result[2] = b
  133. elif a != nil:
  134. result = a
  135. elif b != nil:
  136. result = b
  137. else:
  138. # leave not (a == 4) as it is
  139. result = newNodeI(nkCall, n.info, 2)
  140. result[0] = newSymNode(o.opNot)
  141. result[1] = n
  142. proc buildCall(op: PSym; a: PNode): PNode =
  143. result = newNodeI(nkCall, a.info, 2)
  144. result[0] = newSymNode(op)
  145. result[1] = a
  146. proc buildCall(op: PSym; a, b: PNode): PNode =
  147. result = newNodeI(nkInfix, a.info, 3)
  148. result[0] = newSymNode(op)
  149. result[1] = a
  150. result[2] = b
  151. proc `|+|`(a, b: PNode): PNode =
  152. result = copyNode(a)
  153. if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal |+| b.intVal
  154. else: result.floatVal = a.floatVal + b.floatVal
  155. proc `|-|`(a, b: PNode): PNode =
  156. result = copyNode(a)
  157. if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal |-| b.intVal
  158. else: result.floatVal = a.floatVal - b.floatVal
  159. proc `|*|`(a, b: PNode): PNode =
  160. result = copyNode(a)
  161. if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal |*| b.intVal
  162. else: result.floatVal = a.floatVal * b.floatVal
  163. proc `|div|`(a, b: PNode): PNode =
  164. result = copyNode(a)
  165. if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal div b.intVal
  166. else: result.floatVal = a.floatVal / b.floatVal
  167. proc negate(a, b, res: PNode; o: Operators): PNode =
  168. if b.kind in {nkCharLit..nkUInt64Lit} and b.intVal != low(BiggestInt):
  169. var b = copyNode(b)
  170. b.intVal = -b.intVal
  171. if a.kind in {nkCharLit..nkUInt64Lit}:
  172. b.intVal = b.intVal |+| a.intVal
  173. result = b
  174. else:
  175. result = buildCall(o.opAdd, a, b)
  176. elif b.kind in {nkFloatLit..nkFloat64Lit}:
  177. var b = copyNode(b)
  178. b.floatVal = -b.floatVal
  179. result = buildCall(o.opAdd, a, b)
  180. else:
  181. result = res
  182. proc zero(): PNode = nkIntLit.newIntNode(0)
  183. proc one(): PNode = nkIntLit.newIntNode(1)
  184. proc minusOne(): PNode = nkIntLit.newIntNode(-1)
  185. proc lowBound*(conf: ConfigRef; x: PNode): PNode =
  186. result = nkIntLit.newIntNode(firstOrd(conf, x.typ))
  187. result.info = x.info
  188. proc highBound*(conf: ConfigRef; x: PNode; o: Operators): PNode =
  189. let typ = x.typ.skipTypes(abstractInst)
  190. result = if typ.kind == tyArray:
  191. nkIntLit.newIntNode(lastOrd(conf, typ))
  192. elif typ.kind == tySequence and x.kind == nkSym and
  193. x.sym.kind == skConst:
  194. nkIntLit.newIntNode(x.sym.ast.len-1)
  195. else:
  196. o.opAdd.buildCall(o.opLen.buildCall(x), minusOne())
  197. result.info = x.info
  198. proc reassociation(n: PNode; o: Operators): PNode =
  199. result = n
  200. # (foo+5)+5 --> foo+10; same for '*'
  201. case result.getMagic
  202. of someAdd:
  203. if result[2].isValue and
  204. result[1].getMagic in someAdd and result[1][2].isValue:
  205. result = o.opAdd.buildCall(result[1][1], result[1][2] |+| result[2])
  206. if result[2].intVal == 0:
  207. result = result[1]
  208. of someMul:
  209. if result[2].isValue and
  210. result[1].getMagic in someMul and result[1][2].isValue:
  211. result = o.opMul.buildCall(result[1][1], result[1][2] |*| result[2])
  212. if result[2].intVal == 1:
  213. result = result[1]
  214. elif result[2].intVal == 0:
  215. result = zero()
  216. else: discard
  217. proc pred(n: PNode): PNode =
  218. if n.kind in {nkCharLit..nkUInt64Lit} and n.intVal != low(BiggestInt):
  219. result = copyNode(n)
  220. dec result.intVal
  221. else:
  222. result = n
  223. proc buildLe*(o: Operators; a, b: PNode): PNode =
  224. result = o.opLe.buildCall(a, b)
  225. proc canon*(n: PNode; o: Operators): PNode =
  226. if n.safeLen >= 1:
  227. result = shallowCopy(n)
  228. for i in 0..<n.len:
  229. result[i] = canon(n[i], o)
  230. elif n.kind == nkSym and n.sym.kind == skLet and
  231. n.sym.astdef.getMagic in (someEq + someAdd + someMul + someMin +
  232. someMax + someHigh + someSub + someLen + someDiv):
  233. result = n.sym.astdef.copyTree
  234. else:
  235. result = n
  236. case result.getMagic
  237. of someEq, someAdd, someMul, someMin, someMax:
  238. # these are symmetric; put value as last:
  239. if result[1].isValue and not result[2].isValue:
  240. result = swapArgs(result, result[0].sym)
  241. # (4 + foo) + 2 --> (foo + 4) + 2
  242. of someHigh:
  243. # high == len+(-1)
  244. result = o.opAdd.buildCall(o.opLen.buildCall(result[1]), minusOne())
  245. of someSub:
  246. # x - 4 --> x + (-4)
  247. result = negate(result[1], result[2], result, o)
  248. of someLen:
  249. result[0] = o.opLen.newSymNode
  250. of someLt - {mLtF64}:
  251. # x < y same as x <= y-1:
  252. let y = n[2].canon(o)
  253. let p = pred(y)
  254. let minus = if p != y: p else: o.opAdd.buildCall(y, minusOne()).canon(o)
  255. result = o.opLe.buildCall(n[1].canon(o), minus)
  256. else: discard
  257. result = skipConv(result)
  258. result = reassociation(result, o)
  259. # most important rule: (x-4) <= a.len --> x <= a.len+4
  260. case result.getMagic
  261. of someLe:
  262. let x = result[1]
  263. let y = result[2]
  264. if x.kind in nkCallKinds and x.len == 3 and x[2].isValue and
  265. isLetLocation(x[1], true):
  266. case x.getMagic
  267. of someSub:
  268. result = buildCall(result[0].sym, x[1],
  269. reassociation(o.opAdd.buildCall(y, x[2]), o))
  270. of someAdd:
  271. # Rule A:
  272. let plus = negate(y, x[2], nil, o).reassociation(o)
  273. if plus != nil: result = buildCall(result[0].sym, x[1], plus)
  274. else: discard
  275. elif y.kind in nkCallKinds and y.len == 3 and y[2].isValue and
  276. isLetLocation(y[1], true):
  277. # a.len < x-3
  278. case y.getMagic
  279. of someSub:
  280. result = buildCall(result[0].sym, y[1],
  281. reassociation(o.opAdd.buildCall(x, y[2]), o))
  282. of someAdd:
  283. let plus = negate(x, y[2], nil, o).reassociation(o)
  284. # ensure that Rule A will not trigger afterwards with the
  285. # additional 'not isLetLocation' constraint:
  286. if plus != nil and not isLetLocation(x, true):
  287. result = buildCall(result[0].sym, plus, y[1])
  288. else: discard
  289. elif x.isValue and y.getMagic in someAdd and y[2].kind == x.kind:
  290. # 0 <= a.len + 3
  291. # -3 <= a.len
  292. result[1] = x |-| y[2]
  293. result[2] = y[1]
  294. elif x.isValue and y.getMagic in someSub and y[2].kind == x.kind:
  295. # 0 <= a.len - 3
  296. # 3 <= a.len
  297. result[1] = x |+| y[2]
  298. result[2] = y[1]
  299. else: discard
  300. proc buildAdd*(a: PNode; b: BiggestInt; o: Operators): PNode =
  301. canon(if b != 0: o.opAdd.buildCall(a, nkIntLit.newIntNode(b)) else: a, o)
  302. proc usefulFact(n: PNode; o: Operators): PNode =
  303. case n.getMagic
  304. of someEq:
  305. if skipConv(n[2]).kind == nkNilLit and (
  306. isLetLocation(n[1], false) or isVar(n[1])):
  307. result = o.opIsNil.buildCall(n[1])
  308. else:
  309. if isLetLocation(n[1], true) or isLetLocation(n[2], true):
  310. # XXX algebraic simplifications! 'i-1 < a.len' --> 'i < a.len+1'
  311. result = n
  312. elif n[1].getMagic in someLen or n[2].getMagic in someLen:
  313. result = n
  314. of someLe+someLt:
  315. if isLetLocation(n[1], true) or isLetLocation(n[2], true):
  316. # XXX algebraic simplifications! 'i-1 < a.len' --> 'i < a.len+1'
  317. result = n
  318. elif n[1].getMagic in someLen or n[2].getMagic in someLen:
  319. # XXX Rethink this whole idea of 'usefulFact' for semparallel
  320. result = n
  321. of mIsNil:
  322. if isLetLocation(n[1], false) or isVar(n[1]):
  323. result = n
  324. of someIn:
  325. if isLetLocation(n[1], true):
  326. result = n
  327. of mAnd:
  328. let
  329. a = usefulFact(n[1], o)
  330. b = usefulFact(n[2], o)
  331. if a != nil and b != nil:
  332. result = newNodeI(nkCall, n.info, 3)
  333. result[0] = newSymNode(o.opAnd)
  334. result[1] = a
  335. result[2] = b
  336. elif a != nil:
  337. result = a
  338. elif b != nil:
  339. result = b
  340. of mNot:
  341. let a = usefulFact(n[1], o)
  342. if a != nil:
  343. result = a.neg(o)
  344. of mOr:
  345. # 'or' sucks! (p.isNil or q.isNil) --> hard to do anything
  346. # with that knowledge...
  347. # DeMorgan helps a little though:
  348. # not a or not b --> not (a and b)
  349. # (x == 3) or (y == 2) ---> not ( not (x==3) and not (y == 2))
  350. # not (x != 3 and y != 2)
  351. let
  352. a = usefulFact(n[1], o).neg(o)
  353. b = usefulFact(n[2], o).neg(o)
  354. if a != nil and b != nil:
  355. result = newNodeI(nkCall, n.info, 3)
  356. result[0] = newSymNode(o.opAnd)
  357. result[1] = a
  358. result[2] = b
  359. result = result.neg(o)
  360. elif n.kind == nkSym and n.sym.kind == skLet:
  361. # consider:
  362. # let a = 2 < x
  363. # if a:
  364. # ...
  365. # We make can easily replace 'a' by '2 < x' here:
  366. if n.sym.astdef != nil:
  367. result = usefulFact(n.sym.astdef, o)
  368. elif n.kind == nkStmtListExpr:
  369. result = usefulFact(n.lastSon, o)
  370. type
  371. TModel* = object
  372. s*: seq[PNode] # the "knowledge base"
  373. o*: Operators
  374. beSmart*: bool
  375. proc addFact*(m: var TModel, nn: PNode) =
  376. let n = usefulFact(nn, m.o)
  377. if n != nil:
  378. if not m.beSmart:
  379. m.s.add n
  380. else:
  381. let c = canon(n, m.o)
  382. if c.getMagic == mAnd:
  383. addFact(m, c[1])
  384. addFact(m, c[2])
  385. else:
  386. m.s.add c
  387. proc addFactNeg*(m: var TModel, n: PNode) =
  388. let n = n.neg(m.o)
  389. if n != nil: addFact(m, n)
  390. proc sameOpr(a, b: PSym): bool =
  391. case a.magic
  392. of someEq: result = b.magic in someEq
  393. of someLe: result = b.magic in someLe
  394. of someLt: result = b.magic in someLt
  395. of someLen: result = b.magic in someLen
  396. of someAdd: result = b.magic in someAdd
  397. of someSub: result = b.magic in someSub
  398. of someMul: result = b.magic in someMul
  399. of someDiv: result = b.magic in someDiv
  400. else: result = a == b
  401. proc sameTree*(a, b: PNode): bool =
  402. result = false
  403. if a == b:
  404. result = true
  405. elif a != nil and b != nil and a.kind == b.kind:
  406. case a.kind
  407. of nkSym:
  408. result = a.sym == b.sym
  409. if not result and a.sym.magic != mNone:
  410. result = a.sym.magic == b.sym.magic or sameOpr(a.sym, b.sym)
  411. of nkIdent: result = a.ident.id == b.ident.id
  412. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  413. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  414. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  415. of nkType: result = a.typ == b.typ
  416. of nkEmpty, nkNilLit: result = true
  417. else:
  418. if a.len == b.len:
  419. for i in 0..<a.len:
  420. if not sameTree(a[i], b[i]): return
  421. result = true
  422. proc hasSubTree(n, x: PNode): bool =
  423. if n.sameTree(x): result = true
  424. else:
  425. for i in 0..n.safeLen-1:
  426. if hasSubTree(n[i], x): return true
  427. proc invalidateFacts*(m: var TModel, n: PNode) =
  428. # We are able to guard local vars (as opposed to 'let' variables)!
  429. # 'while p != nil: f(p); p = p.next'
  430. # This is actually quite easy to do:
  431. # Re-assignments (incl. pass to a 'var' param) trigger an invalidation
  432. # of every fact that contains 'v'.
  433. #
  434. # if x < 4:
  435. # if y < 5
  436. # x = unknown()
  437. # # we invalidate 'x' here but it's known that x >= 4
  438. # # for the else anyway
  439. # else:
  440. # echo x
  441. #
  442. # The same mechanism could be used for more complex data stored on the heap;
  443. # procs that 'write: []' cannot invalidate 'n.kind' for instance. In fact, we
  444. # could CSE these expressions then and help C's optimizer.
  445. for i in 0..high(m.s):
  446. if m.s[i] != nil and m.s[i].hasSubTree(n): m.s[i] = nil
  447. proc valuesUnequal(a, b: PNode): bool =
  448. if a.isValue and b.isValue:
  449. result = not sameValue(a, b)
  450. proc impliesEq(fact, eq: PNode): TImplication =
  451. let (loc, val) = if isLocation(eq[1]): (1, 2) else: (2, 1)
  452. case fact[0].sym.magic
  453. of someEq:
  454. if sameTree(fact[1], eq[loc]):
  455. # this is not correct; consider: a == b; a == 1 --> unknown!
  456. if sameTree(fact[2], eq[val]): result = impYes
  457. elif valuesUnequal(fact[2], eq[val]): result = impNo
  458. elif sameTree(fact[2], eq[loc]):
  459. if sameTree(fact[1], eq[val]): result = impYes
  460. elif valuesUnequal(fact[1], eq[val]): result = impNo
  461. of mInSet:
  462. # remember: mInSet is 'contains' so the set comes first!
  463. if sameTree(fact[2], eq[loc]) and isValue(eq[val]):
  464. if inSet(fact[1], eq[val]): result = impYes
  465. else: result = impNo
  466. of mNot, mOr, mAnd: assert(false, "impliesEq")
  467. else: discard
  468. proc leImpliesIn(x, c, aSet: PNode): TImplication =
  469. if c.kind in {nkCharLit..nkUInt64Lit}:
  470. # fact: x <= 4; question x in {56}?
  471. # --> true if every value <= 4 is in the set {56}
  472. #
  473. var value = newIntNode(c.kind, firstOrd(nil, x.typ))
  474. # don't iterate too often:
  475. if c.intVal - value.intVal < 1000:
  476. var i, pos, neg: int
  477. while value.intVal <= c.intVal:
  478. if inSet(aSet, value): inc pos
  479. else: inc neg
  480. inc i; inc value.intVal
  481. if pos == i: result = impYes
  482. elif neg == i: result = impNo
  483. proc geImpliesIn(x, c, aSet: PNode): TImplication =
  484. if c.kind in {nkCharLit..nkUInt64Lit}:
  485. # fact: x >= 4; question x in {56}?
  486. # --> true iff every value >= 4 is in the set {56}
  487. #
  488. var value = newIntNode(c.kind, c.intVal)
  489. let max = lastOrd(nil, x.typ)
  490. # don't iterate too often:
  491. if max - getInt(value) < toInt128(1000):
  492. var i, pos, neg: int
  493. while value.intVal <= max:
  494. if inSet(aSet, value): inc pos
  495. else: inc neg
  496. inc i; inc value.intVal
  497. if pos == i: result = impYes
  498. elif neg == i: result = impNo
  499. proc compareSets(a, b: PNode): TImplication =
  500. if equalSets(nil, a, b): result = impYes
  501. elif intersectSets(nil, a, b).len == 0: result = impNo
  502. proc impliesIn(fact, loc, aSet: PNode): TImplication =
  503. case fact[0].sym.magic
  504. of someEq:
  505. if sameTree(fact[1], loc):
  506. if inSet(aSet, fact[2]): result = impYes
  507. else: result = impNo
  508. elif sameTree(fact[2], loc):
  509. if inSet(aSet, fact[1]): result = impYes
  510. else: result = impNo
  511. of mInSet:
  512. if sameTree(fact[2], loc):
  513. result = compareSets(fact[1], aSet)
  514. of someLe:
  515. if sameTree(fact[1], loc):
  516. result = leImpliesIn(fact[1], fact[2], aSet)
  517. elif sameTree(fact[2], loc):
  518. result = geImpliesIn(fact[2], fact[1], aSet)
  519. of someLt:
  520. if sameTree(fact[1], loc):
  521. result = leImpliesIn(fact[1], fact[2].pred, aSet)
  522. elif sameTree(fact[2], loc):
  523. # 4 < x --> 3 <= x
  524. result = geImpliesIn(fact[2], fact[1].pred, aSet)
  525. of mNot, mOr, mAnd: assert(false, "impliesIn")
  526. else: discard
  527. proc valueIsNil(n: PNode): TImplication =
  528. if n.kind == nkNilLit: impYes
  529. elif n.kind in {nkStrLit..nkTripleStrLit, nkBracket, nkObjConstr}: impNo
  530. else: impUnknown
  531. proc impliesIsNil(fact, eq: PNode): TImplication =
  532. case fact[0].sym.magic
  533. of mIsNil:
  534. if sameTree(fact[1], eq[1]):
  535. result = impYes
  536. of someEq:
  537. if sameTree(fact[1], eq[1]):
  538. result = valueIsNil(fact[2].skipConv)
  539. elif sameTree(fact[2], eq[1]):
  540. result = valueIsNil(fact[1].skipConv)
  541. of mNot, mOr, mAnd: assert(false, "impliesIsNil")
  542. else: discard
  543. proc impliesGe(fact, x, c: PNode): TImplication =
  544. assert isLocation(x)
  545. case fact[0].sym.magic
  546. of someEq:
  547. if sameTree(fact[1], x):
  548. if isValue(fact[2]) and isValue(c):
  549. # fact: x = 4; question x >= 56? --> true iff 4 >= 56
  550. if leValue(c, fact[2]): result = impYes
  551. else: result = impNo
  552. elif sameTree(fact[2], x):
  553. if isValue(fact[1]) and isValue(c):
  554. if leValue(c, fact[1]): result = impYes
  555. else: result = impNo
  556. of someLt:
  557. if sameTree(fact[1], x):
  558. if isValue(fact[2]) and isValue(c):
  559. # fact: x < 4; question N <= x? --> false iff N <= 4
  560. if leValue(fact[2], c): result = impNo
  561. # fact: x < 4; question 2 <= x? --> we don't know
  562. elif sameTree(fact[2], x):
  563. # fact: 3 < x; question: N-1 < x ? --> true iff N-1 <= 3
  564. if isValue(fact[1]) and isValue(c):
  565. if leValue(c.pred, fact[1]): result = impYes
  566. of someLe:
  567. if sameTree(fact[1], x):
  568. if isValue(fact[2]) and isValue(c):
  569. # fact: x <= 4; question x >= 56? --> false iff 4 <= 56
  570. if leValue(fact[2], c): result = impNo
  571. # fact: x <= 4; question x >= 2? --> we don't know
  572. elif sameTree(fact[2], x):
  573. # fact: 3 <= x; question: x >= 2 ? --> true iff 2 <= 3
  574. if isValue(fact[1]) and isValue(c):
  575. if leValue(c, fact[1]): result = impYes
  576. of mNot, mOr, mAnd: assert(false, "impliesGe")
  577. else: discard
  578. proc impliesLe(fact, x, c: PNode): TImplication =
  579. if not isLocation(x):
  580. if c.isValue:
  581. if leValue(x, x): return impYes
  582. else: return impNo
  583. return impliesGe(fact, c, x)
  584. case fact[0].sym.magic
  585. of someEq:
  586. if sameTree(fact[1], x):
  587. if isValue(fact[2]) and isValue(c):
  588. # fact: x = 4; question x <= 56? --> true iff 4 <= 56
  589. if leValue(fact[2], c): result = impYes
  590. else: result = impNo
  591. elif sameTree(fact[2], x):
  592. if isValue(fact[1]) and isValue(c):
  593. if leValue(fact[1], c): result = impYes
  594. else: result = impNo
  595. of someLt:
  596. if sameTree(fact[1], x):
  597. if isValue(fact[2]) and isValue(c):
  598. # fact: x < 4; question x <= N? --> true iff N-1 <= 4
  599. if leValue(fact[2], c.pred): result = impYes
  600. # fact: x < 4; question x <= 2? --> we don't know
  601. elif sameTree(fact[2], x):
  602. # fact: 3 < x; question: x <= 1 ? --> false iff 1 <= 3
  603. if isValue(fact[1]) and isValue(c):
  604. if leValue(c, fact[1]): result = impNo
  605. of someLe:
  606. if sameTree(fact[1], x):
  607. if isValue(fact[2]) and isValue(c):
  608. # fact: x <= 4; question x <= 56? --> true iff 4 <= 56
  609. if leValue(fact[2], c): result = impYes
  610. # fact: x <= 4; question x <= 2? --> we don't know
  611. elif sameTree(fact[2], x):
  612. # fact: 3 <= x; question: x <= 2 ? --> false iff 2 < 3
  613. if isValue(fact[1]) and isValue(c):
  614. if leValue(c, fact[1].pred): result = impNo
  615. of mNot, mOr, mAnd: assert(false, "impliesLe")
  616. else: discard
  617. proc impliesLt(fact, x, c: PNode): TImplication =
  618. # x < 3 same as x <= 2:
  619. let p = c.pred
  620. if p != c:
  621. result = impliesLe(fact, x, p)
  622. else:
  623. # 4 < x same as 3 <= x
  624. let q = x.pred
  625. if q != x:
  626. result = impliesLe(fact, q, c)
  627. proc `~`(x: TImplication): TImplication =
  628. case x
  629. of impUnknown: impUnknown
  630. of impNo: impYes
  631. of impYes: impNo
  632. proc factImplies(fact, prop: PNode): TImplication =
  633. case fact.getMagic
  634. of mNot:
  635. # Consider:
  636. # enum nkBinary, nkTernary, nkStr
  637. # fact: not (k <= nkBinary)
  638. # question: k in {nkStr}
  639. # --> 'not' for facts is entirely different than 'not' for questions!
  640. # it's provably wrong if every value > 4 is in the set {56}
  641. # That's because we compute the implication and 'a -> not b' cannot
  642. # be treated the same as 'not a -> b'
  643. # (not a) -> b compute as not (a -> b) ???
  644. # == not a or not b == not (a and b)
  645. let arg = fact[1]
  646. case arg.getMagic
  647. of mIsNil, mEqRef:
  648. return ~factImplies(arg, prop)
  649. of mAnd:
  650. # not (a and b) means not a or not b:
  651. # a or b --> both need to imply 'prop'
  652. let a = factImplies(arg[1], prop)
  653. let b = factImplies(arg[2], prop)
  654. if a == b: return ~a
  655. return impUnknown
  656. else:
  657. return impUnknown
  658. of mAnd:
  659. result = factImplies(fact[1], prop)
  660. if result != impUnknown: return result
  661. return factImplies(fact[2], prop)
  662. else: discard
  663. case prop[0].sym.magic
  664. of mNot: result = ~fact.factImplies(prop[1])
  665. of mIsNil: result = impliesIsNil(fact, prop)
  666. of someEq: result = impliesEq(fact, prop)
  667. of someLe: result = impliesLe(fact, prop[1], prop[2])
  668. of someLt: result = impliesLt(fact, prop[1], prop[2])
  669. of mInSet: result = impliesIn(fact, prop[2], prop[1])
  670. else: result = impUnknown
  671. proc doesImply*(facts: TModel, prop: PNode): TImplication =
  672. assert prop.kind in nkCallKinds
  673. for f in facts.s:
  674. # facts can be invalidated, in which case they are 'nil':
  675. if not f.isNil:
  676. result = f.factImplies(prop)
  677. if result != impUnknown: return
  678. proc impliesNotNil*(m: TModel, arg: PNode): TImplication =
  679. result = doesImply(m, m.o.opIsNil.buildCall(arg).neg(m.o))
  680. proc simpleSlice*(a, b: PNode): BiggestInt =
  681. # returns 'c' if a..b matches (i+c)..(i+c), -1 otherwise. (i)..(i) is matched
  682. # as if it is (i+0)..(i+0).
  683. if guards.sameTree(a, b):
  684. if a.getMagic in someAdd and a[2].kind in {nkCharLit..nkUInt64Lit}:
  685. result = a[2].intVal
  686. else:
  687. result = 0
  688. else:
  689. result = -1
  690. template isMul(x): untyped = x.getMagic in someMul
  691. template isDiv(x): untyped = x.getMagic in someDiv
  692. template isAdd(x): untyped = x.getMagic in someAdd
  693. template isSub(x): untyped = x.getMagic in someSub
  694. template isVal(x): untyped = x.kind in {nkCharLit..nkUInt64Lit}
  695. template isIntVal(x, y): untyped = x.intVal == y
  696. import macros
  697. macro `=~`(x: PNode, pat: untyped): bool =
  698. proc m(x, pat, conds: NimNode) =
  699. case pat.kind
  700. of nnkInfix:
  701. case $pat[0]
  702. of "*": conds.add getAst(isMul(x))
  703. of "/": conds.add getAst(isDiv(x))
  704. of "+": conds.add getAst(isAdd(x))
  705. of "-": conds.add getAst(isSub(x))
  706. else:
  707. error("invalid pattern")
  708. m(newTree(nnkBracketExpr, x, newLit(1)), pat[1], conds)
  709. m(newTree(nnkBracketExpr, x, newLit(2)), pat[2], conds)
  710. of nnkPar:
  711. if pat.len == 1:
  712. m(x, pat[0], conds)
  713. else:
  714. error("invalid pattern")
  715. of nnkIdent:
  716. let c = newTree(nnkStmtListExpr, newLetStmt(pat, x))
  717. conds.add c
  718. # XXX why is this 'isVal(pat)' and not 'isVal(x)'?
  719. if ($pat)[^1] == 'c': c.add(getAst(isVal(x)))
  720. else: c.add bindSym"true"
  721. of nnkIntLit:
  722. conds.add(getAst(isIntVal(x, pat.intVal)))
  723. else:
  724. error("invalid pattern")
  725. var conds = newTree(nnkBracket)
  726. m(x, pat, conds)
  727. when compiles(nestList(ident"and", conds)):
  728. result = nestList(ident"and", conds)
  729. #elif declared(macros.toNimIdent):
  730. # result = nestList(toNimIdent"and", conds)
  731. else:
  732. result = nestList(!"and", conds)
  733. proc isMinusOne(n: PNode): bool =
  734. n.kind in {nkCharLit..nkUInt64Lit} and n.intVal == -1
  735. proc pleViaModel(model: TModel; aa, bb: PNode): TImplication
  736. proc ple(m: TModel; a, b: PNode): TImplication =
  737. template `<=?`(a,b): untyped = ple(m,a,b) == impYes
  738. template `>=?`(a,b): untyped = ple(m, nkIntLit.newIntNode(b), a) == impYes
  739. # 0 <= 3
  740. if a.isValue and b.isValue:
  741. return if leValue(a, b): impYes else: impNo
  742. # use type information too: x <= 4 iff high(x) <= 4
  743. if b.isValue and a.typ != nil and a.typ.isOrdinalType:
  744. if lastOrd(nil, a.typ) <= b.intVal: return impYes
  745. # 3 <= x iff low(x) <= 3
  746. if a.isValue and b.typ != nil and b.typ.isOrdinalType:
  747. if a.intVal <= firstOrd(nil, b.typ): return impYes
  748. # x <= x
  749. if sameTree(a, b): return impYes
  750. # 0 <= x.len
  751. if b.getMagic in someLen and a.isValue:
  752. if a.intVal <= 0: return impYes
  753. # x <= y+c if 0 <= c and x <= y
  754. # x <= y+(-c) if c <= 0 and y >= x
  755. if b.getMagic in someAdd:
  756. if zero() <=? b[2] and a <=? b[1]: return impYes
  757. # x <= y-c if x+c <= y
  758. if b[2] <=? zero() and (canon(m.o.opSub.buildCall(a, b[2]), m.o) <=? b[1]):
  759. return impYes
  760. # x+c <= y if c <= 0 and x <= y
  761. if a.getMagic in someAdd and a[2] <=? zero() and a[1] <=? b: return impYes
  762. # x <= y*c if 1 <= c and x <= y and 0 <= y
  763. if b.getMagic in someMul:
  764. if a <=? b[1] and one() <=? b[2] and zero() <=? b[1]: return impYes
  765. if a.getMagic in someMul and a[2].isValue and a[1].getMagic in someDiv and
  766. a[1][2].isValue:
  767. # simplify (x div 4) * 2 <= y to x div (c div d) <= y
  768. if ple(m, buildCall(m.o.opDiv, a[1][1], `|div|`(a[1][2], a[2])), b) == impYes:
  769. return impYes
  770. # x*3 + x == x*4. It follows that:
  771. # x*3 + y <= x*4 if y <= x and 3 <= 4
  772. if a =~ x*dc + y and b =~ x2*ec:
  773. if sameTree(x, x2):
  774. let ec1 = m.o.opAdd.buildCall(ec, minusOne())
  775. if x >=? 1 and ec >=? 1 and dc >=? 1 and dc <=? ec1 and y <=? x:
  776. return impYes
  777. elif a =~ x*dc and b =~ x2*ec + y:
  778. #echo "BUG cam ehrer e ", a, " <=? ", b
  779. if sameTree(x, x2):
  780. let ec1 = m.o.opAdd.buildCall(ec, minusOne())
  781. if x >=? 1 and ec >=? 1 and dc >=? 1 and dc <=? ec1 and y <=? zero():
  782. return impYes
  783. # x+c <= x+d if c <= d. Same for *, - etc.
  784. if a.getMagic in someBinaryOp and a.getMagic == b.getMagic:
  785. if sameTree(a[1], b[1]) and a[2] <=? b[2]: return impYes
  786. elif sameTree(a[2], b[2]) and a[1] <=? b[1]: return impYes
  787. # x div c <= y if 1 <= c and 0 <= y and x <= y:
  788. if a.getMagic in someDiv:
  789. if one() <=? a[2] and zero() <=? b and a[1] <=? b: return impYes
  790. # x div c <= x div d if d <= c
  791. if b.getMagic in someDiv:
  792. if sameTree(a[1], b[1]) and b[2] <=? a[2]: return impYes
  793. # x div z <= x - 1 if z <= x
  794. if a[2].isValue and b.getMagic in someAdd and b[2].isMinusOne:
  795. if a[2] <=? a[1] and sameTree(a[1], b[1]): return impYes
  796. # slightly subtle:
  797. # x <= max(y, z) iff x <= y or x <= z
  798. # note that 'x <= max(x, z)' is a special case of the above rule
  799. if b.getMagic in someMax:
  800. if a <=? b[1] or a <=? b[2]: return impYes
  801. # min(x, y) <= z iff x <= z or y <= z
  802. if a.getMagic in someMin:
  803. if a[1] <=? b or a[2] <=? b: return impYes
  804. # use the knowledge base:
  805. return pleViaModel(m, a, b)
  806. #return doesImply(m, o.opLe.buildCall(a, b))
  807. type TReplacements = seq[tuple[a, b: PNode]]
  808. proc replaceSubTree(n, x, by: PNode): PNode =
  809. if sameTree(n, x):
  810. result = by
  811. elif hasSubTree(n, x):
  812. result = shallowCopy(n)
  813. for i in 0..n.safeLen-1:
  814. result[i] = replaceSubTree(n[i], x, by)
  815. else:
  816. result = n
  817. proc applyReplacements(n: PNode; rep: TReplacements): PNode =
  818. result = n
  819. for x in rep: result = result.replaceSubTree(x.a, x.b)
  820. proc pleViaModelRec(m: var TModel; a, b: PNode): TImplication =
  821. # now check for inferrable facts: a <= b and b <= c implies a <= c
  822. for i in 0..m.s.high:
  823. let fact = m.s[i]
  824. if fact != nil and fact.getMagic in someLe:
  825. # mark as used:
  826. m.s[i] = nil
  827. # i <= len-100
  828. # i <=? len-1
  829. # --> true if (len-100) <= (len-1)
  830. let x = fact[1]
  831. let y = fact[2]
  832. # x <= y.
  833. # Question: x <= b? True iff y <= b.
  834. if sameTree(x, a):
  835. if ple(m, y, b) == impYes: return impYes
  836. if y.getMagic in someAdd and b.getMagic in someAdd and sameTree(y[1], b[1]):
  837. if ple(m, b[2], y[2]) == impYes:
  838. return impYes
  839. # x <= y implies a <= b if a <= x and y <= b
  840. if ple(m, a, x) == impYes:
  841. if ple(m, y, b) == impYes:
  842. return impYes
  843. #if pleViaModelRec(m, y, b): return impYes
  844. # fact: 16 <= i
  845. # x y
  846. # question: i <= 15? no!
  847. result = impliesLe(fact, a, b)
  848. if result != impUnknown:
  849. return result
  850. when false:
  851. # given: x <= y; y==a; x <= a this means: a <= b if x <= b
  852. if sameTree(y, a):
  853. result = ple(m, b, x)
  854. if result != impUnknown:
  855. return result
  856. proc pleViaModel(model: TModel; aa, bb: PNode): TImplication =
  857. # compute replacements:
  858. var replacements: TReplacements = @[]
  859. for fact in model.s:
  860. if fact != nil and fact.getMagic in someEq:
  861. let a = fact[1]
  862. let b = fact[2]
  863. if a.kind == nkSym: replacements.add((a,b))
  864. else: replacements.add((b,a))
  865. var m: TModel
  866. var a = aa
  867. var b = bb
  868. if replacements.len > 0:
  869. m.s = @[]
  870. m.o = model.o
  871. # make the other facts consistent:
  872. for fact in model.s:
  873. if fact != nil and fact.getMagic notin someEq:
  874. # XXX 'canon' should not be necessary here, but it is
  875. m.s.add applyReplacements(fact, replacements).canon(m.o)
  876. a = applyReplacements(aa, replacements)
  877. b = applyReplacements(bb, replacements)
  878. else:
  879. # we have to make a copy here, because the model will be modified:
  880. m = model
  881. result = pleViaModelRec(m, a, b)
  882. proc proveLe*(m: TModel; a, b: PNode): TImplication =
  883. let x = canon(m.o.opLe.buildCall(a, b), m.o)
  884. #echo "ROOT ", renderTree(x[1]), " <=? ", renderTree(x[2])
  885. result = ple(m, x[1], x[2])
  886. if result == impUnknown:
  887. # try an alternative: a <= b iff not (b < a) iff not (b+1 <= a):
  888. let y = canon(m.o.opLe.buildCall(m.o.opAdd.buildCall(b, one()), a), m.o)
  889. result = ~ple(m, y[1], y[2])
  890. proc addFactLe*(m: var TModel; a, b: PNode) =
  891. m.s.add canon(m.o.opLe.buildCall(a, b), m.o)
  892. proc addFactLt*(m: var TModel; a, b: PNode) =
  893. let bb = m.o.opAdd.buildCall(b, minusOne())
  894. addFactLe(m, a, bb)
  895. proc settype(n: PNode): PType =
  896. result = newType(tySet, n.typ.owner)
  897. addSonSkipIntLit(result, n.typ)
  898. proc buildOf(it, loc: PNode; o: Operators): PNode =
  899. var s = newNodeI(nkCurly, it.info, it.len-1)
  900. s.typ = settype(loc)
  901. for i in 0..<it.len-1: s[i] = it[i]
  902. result = newNodeI(nkCall, it.info, 3)
  903. result[0] = newSymNode(o.opContains)
  904. result[1] = s
  905. result[2] = loc
  906. proc buildElse(n: PNode; o: Operators): PNode =
  907. var s = newNodeIT(nkCurly, n.info, settype(n[0]))
  908. for i in 1..<n.len-1:
  909. let branch = n[i]
  910. assert branch.kind != nkElse
  911. if branch.kind == nkOfBranch:
  912. for j in 0..<branch.len-1:
  913. s.add(branch[j])
  914. result = newNodeI(nkCall, n.info, 3)
  915. result[0] = newSymNode(o.opContains)
  916. result[1] = s
  917. result[2] = n[0]
  918. proc addDiscriminantFact*(m: var TModel, n: PNode) =
  919. var fact = newNodeI(nkCall, n.info, 3)
  920. fact[0] = newSymNode(m.o.opEq)
  921. fact[1] = n[0]
  922. fact[2] = n[1]
  923. m.s.add fact
  924. proc addAsgnFact*(m: var TModel, key, value: PNode) =
  925. var fact = newNodeI(nkCall, key.info, 3)
  926. fact[0] = newSymNode(m.o.opEq)
  927. fact[1] = key
  928. fact[2] = value
  929. m.s.add fact
  930. proc sameSubexprs*(m: TModel; a, b: PNode): bool =
  931. # This should be used to check whether two *path expressions* refer to the
  932. # same memory location according to 'm'. This is tricky:
  933. # lock a[i].guard:
  934. # ...
  935. # access a[i].guarded
  936. #
  937. # Here a[i] is the same as a[i] iff 'i' and 'a' are not changed via '...'.
  938. # However, nil checking requires exactly the same mechanism! But for now
  939. # we simply use sameTree and live with the unsoundness of the analysis.
  940. var check = newNodeI(nkCall, a.info, 3)
  941. check[0] = newSymNode(m.o.opEq)
  942. check[1] = a
  943. check[2] = b
  944. result = m.doesImply(check) == impYes
  945. proc addCaseBranchFacts*(m: var TModel, n: PNode, i: int) =
  946. let branch = n[i]
  947. if branch.kind == nkOfBranch:
  948. m.s.add buildOf(branch, n[0], m.o)
  949. else:
  950. m.s.add n.buildElse(m.o).neg(m.o)
  951. proc buildProperFieldCheck(access, check: PNode; o: Operators): PNode =
  952. if check[1].kind == nkCurly:
  953. result = copyTree(check)
  954. if access.kind == nkDotExpr:
  955. var a = copyTree(access)
  956. a[1] = check[2]
  957. result[2] = a
  958. # 'access.kind != nkDotExpr' can happen for object constructors
  959. # which we don't check yet
  960. else:
  961. # it is some 'not'
  962. assert check.getMagic == mNot
  963. result = buildProperFieldCheck(access, check[1], o).neg(o)
  964. proc checkFieldAccess*(m: TModel, n: PNode; conf: ConfigRef) =
  965. for i in 1..<n.len:
  966. let check = buildProperFieldCheck(n[0], n[i], m.o)
  967. if check != nil and m.doesImply(check) != impYes:
  968. message(conf, n.info, warnProveField, renderTree(n[0])); break