guards.nim 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 notin {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*(s: var seq[PNode], 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(s):
  446. if s[i] != nil and s[i].hasSubTree(n): s[i] = nil
  447. proc invalidateFacts*(m: var TModel, n: PNode) =
  448. invalidateFacts(m.s, n)
  449. proc valuesUnequal(a, b: PNode): bool =
  450. if a.isValue and b.isValue:
  451. result = not sameValue(a, b)
  452. proc impliesEq(fact, eq: PNode): TImplication =
  453. let (loc, val) = if isLocation(eq[1]): (1, 2) else: (2, 1)
  454. case fact[0].sym.magic
  455. of someEq:
  456. if sameTree(fact[1], eq[loc]):
  457. # this is not correct; consider: a == b; a == 1 --> unknown!
  458. if sameTree(fact[2], eq[val]): result = impYes
  459. elif valuesUnequal(fact[2], eq[val]): result = impNo
  460. elif sameTree(fact[2], eq[loc]):
  461. if sameTree(fact[1], eq[val]): result = impYes
  462. elif valuesUnequal(fact[1], eq[val]): result = impNo
  463. of mInSet:
  464. # remember: mInSet is 'contains' so the set comes first!
  465. if sameTree(fact[2], eq[loc]) and isValue(eq[val]):
  466. if inSet(fact[1], eq[val]): result = impYes
  467. else: result = impNo
  468. of mNot, mOr, mAnd: assert(false, "impliesEq")
  469. else: discard
  470. proc leImpliesIn(x, c, aSet: PNode): TImplication =
  471. if c.kind in {nkCharLit..nkUInt64Lit}:
  472. # fact: x <= 4; question x in {56}?
  473. # --> true if every value <= 4 is in the set {56}
  474. #
  475. var value = newIntNode(c.kind, firstOrd(nil, x.typ))
  476. # don't iterate too often:
  477. if c.intVal - value.intVal < 1000:
  478. var i, pos, neg: int
  479. while value.intVal <= c.intVal:
  480. if inSet(aSet, value): inc pos
  481. else: inc neg
  482. inc i; inc value.intVal
  483. if pos == i: result = impYes
  484. elif neg == i: result = impNo
  485. proc geImpliesIn(x, c, aSet: PNode): TImplication =
  486. if c.kind in {nkCharLit..nkUInt64Lit}:
  487. # fact: x >= 4; question x in {56}?
  488. # --> true iff every value >= 4 is in the set {56}
  489. #
  490. var value = newIntNode(c.kind, c.intVal)
  491. let max = lastOrd(nil, x.typ)
  492. # don't iterate too often:
  493. if max - getInt(value) < toInt128(1000):
  494. var i, pos, neg: int
  495. while value.intVal <= max:
  496. if inSet(aSet, value): inc pos
  497. else: inc neg
  498. inc i; inc value.intVal
  499. if pos == i: result = impYes
  500. elif neg == i: result = impNo
  501. proc compareSets(a, b: PNode): TImplication =
  502. if equalSets(nil, a, b): result = impYes
  503. elif intersectSets(nil, a, b).len == 0: result = impNo
  504. proc impliesIn(fact, loc, aSet: PNode): TImplication =
  505. case fact[0].sym.magic
  506. of someEq:
  507. if sameTree(fact[1], loc):
  508. if inSet(aSet, fact[2]): result = impYes
  509. else: result = impNo
  510. elif sameTree(fact[2], loc):
  511. if inSet(aSet, fact[1]): result = impYes
  512. else: result = impNo
  513. of mInSet:
  514. if sameTree(fact[2], loc):
  515. result = compareSets(fact[1], aSet)
  516. of someLe:
  517. if sameTree(fact[1], loc):
  518. result = leImpliesIn(fact[1], fact[2], aSet)
  519. elif sameTree(fact[2], loc):
  520. result = geImpliesIn(fact[2], fact[1], aSet)
  521. of someLt:
  522. if sameTree(fact[1], loc):
  523. result = leImpliesIn(fact[1], fact[2].pred, aSet)
  524. elif sameTree(fact[2], loc):
  525. # 4 < x --> 3 <= x
  526. result = geImpliesIn(fact[2], fact[1].pred, aSet)
  527. of mNot, mOr, mAnd: assert(false, "impliesIn")
  528. else: discard
  529. proc valueIsNil(n: PNode): TImplication =
  530. if n.kind == nkNilLit: impYes
  531. elif n.kind in {nkStrLit..nkTripleStrLit, nkBracket, nkObjConstr}: impNo
  532. else: impUnknown
  533. proc impliesIsNil(fact, eq: PNode): TImplication =
  534. case fact[0].sym.magic
  535. of mIsNil:
  536. if sameTree(fact[1], eq[1]):
  537. result = impYes
  538. of someEq:
  539. if sameTree(fact[1], eq[1]):
  540. result = valueIsNil(fact[2].skipConv)
  541. elif sameTree(fact[2], eq[1]):
  542. result = valueIsNil(fact[1].skipConv)
  543. of mNot, mOr, mAnd: assert(false, "impliesIsNil")
  544. else: discard
  545. proc impliesGe(fact, x, c: PNode): TImplication =
  546. assert isLocation(x)
  547. case fact[0].sym.magic
  548. of someEq:
  549. if sameTree(fact[1], x):
  550. if isValue(fact[2]) and isValue(c):
  551. # fact: x = 4; question x >= 56? --> true iff 4 >= 56
  552. if leValue(c, fact[2]): result = impYes
  553. else: result = impNo
  554. elif sameTree(fact[2], x):
  555. if isValue(fact[1]) and isValue(c):
  556. if leValue(c, fact[1]): result = impYes
  557. else: result = impNo
  558. of someLt:
  559. if sameTree(fact[1], x):
  560. if isValue(fact[2]) and isValue(c):
  561. # fact: x < 4; question N <= x? --> false iff N <= 4
  562. if leValue(fact[2], c): result = impNo
  563. # fact: x < 4; question 2 <= x? --> we don't know
  564. elif sameTree(fact[2], x):
  565. # fact: 3 < x; question: N-1 < x ? --> true iff N-1 <= 3
  566. if isValue(fact[1]) and isValue(c):
  567. if leValue(c.pred, fact[1]): result = impYes
  568. of someLe:
  569. if sameTree(fact[1], x):
  570. if isValue(fact[2]) and isValue(c):
  571. # fact: x <= 4; question x >= 56? --> false iff 4 <= 56
  572. if leValue(fact[2], c): result = impNo
  573. # fact: x <= 4; question x >= 2? --> we don't know
  574. elif sameTree(fact[2], x):
  575. # fact: 3 <= x; question: x >= 2 ? --> true iff 2 <= 3
  576. if isValue(fact[1]) and isValue(c):
  577. if leValue(c, fact[1]): result = impYes
  578. of mNot, mOr, mAnd: assert(false, "impliesGe")
  579. else: discard
  580. proc impliesLe(fact, x, c: PNode): TImplication =
  581. if not isLocation(x):
  582. if c.isValue:
  583. if leValue(x, x): return impYes
  584. else: return impNo
  585. return impliesGe(fact, c, x)
  586. case fact[0].sym.magic
  587. of someEq:
  588. if sameTree(fact[1], x):
  589. if isValue(fact[2]) and isValue(c):
  590. # fact: x = 4; question x <= 56? --> true iff 4 <= 56
  591. if leValue(fact[2], c): result = impYes
  592. else: result = impNo
  593. elif sameTree(fact[2], x):
  594. if isValue(fact[1]) and isValue(c):
  595. if leValue(fact[1], c): result = impYes
  596. else: result = impNo
  597. of someLt:
  598. if sameTree(fact[1], x):
  599. if isValue(fact[2]) and isValue(c):
  600. # fact: x < 4; question x <= N? --> true iff N-1 <= 4
  601. if leValue(fact[2], c.pred): result = impYes
  602. # fact: x < 4; question x <= 2? --> we don't know
  603. elif sameTree(fact[2], x):
  604. # fact: 3 < x; question: x <= 1 ? --> false iff 1 <= 3
  605. if isValue(fact[1]) and isValue(c):
  606. if leValue(c, fact[1]): result = impNo
  607. of someLe:
  608. if sameTree(fact[1], x):
  609. if isValue(fact[2]) and isValue(c):
  610. # fact: x <= 4; question x <= 56? --> true iff 4 <= 56
  611. if leValue(fact[2], c): result = impYes
  612. # fact: x <= 4; question x <= 2? --> we don't know
  613. elif sameTree(fact[2], x):
  614. # fact: 3 <= x; question: x <= 2 ? --> false iff 2 < 3
  615. if isValue(fact[1]) and isValue(c):
  616. if leValue(c, fact[1].pred): result = impNo
  617. of mNot, mOr, mAnd: assert(false, "impliesLe")
  618. else: discard
  619. proc impliesLt(fact, x, c: PNode): TImplication =
  620. # x < 3 same as x <= 2:
  621. let p = c.pred
  622. if p != c:
  623. result = impliesLe(fact, x, p)
  624. else:
  625. # 4 < x same as 3 <= x
  626. let q = x.pred
  627. if q != x:
  628. result = impliesLe(fact, q, c)
  629. proc `~`(x: TImplication): TImplication =
  630. case x
  631. of impUnknown: impUnknown
  632. of impNo: impYes
  633. of impYes: impNo
  634. proc factImplies(fact, prop: PNode): TImplication =
  635. case fact.getMagic
  636. of mNot:
  637. # Consider:
  638. # enum nkBinary, nkTernary, nkStr
  639. # fact: not (k <= nkBinary)
  640. # question: k in {nkStr}
  641. # --> 'not' for facts is entirely different than 'not' for questions!
  642. # it's provably wrong if every value > 4 is in the set {56}
  643. # That's because we compute the implication and 'a -> not b' cannot
  644. # be treated the same as 'not a -> b'
  645. # (not a) -> b compute as not (a -> b) ???
  646. # == not a or not b == not (a and b)
  647. let arg = fact[1]
  648. case arg.getMagic
  649. of mIsNil, mEqRef:
  650. return ~factImplies(arg, prop)
  651. of mAnd:
  652. # not (a and b) means not a or not b:
  653. # a or b --> both need to imply 'prop'
  654. let a = factImplies(arg[1], prop)
  655. let b = factImplies(arg[2], prop)
  656. if a == b: return ~a
  657. return impUnknown
  658. else:
  659. return impUnknown
  660. of mAnd:
  661. result = factImplies(fact[1], prop)
  662. if result != impUnknown: return result
  663. return factImplies(fact[2], prop)
  664. else: discard
  665. case prop[0].sym.magic
  666. of mNot: result = ~fact.factImplies(prop[1])
  667. of mIsNil: result = impliesIsNil(fact, prop)
  668. of someEq: result = impliesEq(fact, prop)
  669. of someLe: result = impliesLe(fact, prop[1], prop[2])
  670. of someLt: result = impliesLt(fact, prop[1], prop[2])
  671. of mInSet: result = impliesIn(fact, prop[2], prop[1])
  672. else: result = impUnknown
  673. proc doesImply*(facts: TModel, prop: PNode): TImplication =
  674. assert prop.kind in nkCallKinds
  675. for f in facts.s:
  676. # facts can be invalidated, in which case they are 'nil':
  677. if not f.isNil:
  678. result = f.factImplies(prop)
  679. if result != impUnknown: return
  680. proc impliesNotNil*(m: TModel, arg: PNode): TImplication =
  681. result = doesImply(m, m.o.opIsNil.buildCall(arg).neg(m.o))
  682. proc simpleSlice*(a, b: PNode): BiggestInt =
  683. # returns 'c' if a..b matches (i+c)..(i+c), -1 otherwise. (i)..(i) is matched
  684. # as if it is (i+0)..(i+0).
  685. if guards.sameTree(a, b):
  686. if a.getMagic in someAdd and a[2].kind in {nkCharLit..nkUInt64Lit}:
  687. result = a[2].intVal
  688. else:
  689. result = 0
  690. else:
  691. result = -1
  692. template isMul(x): untyped = x.getMagic in someMul
  693. template isDiv(x): untyped = x.getMagic in someDiv
  694. template isAdd(x): untyped = x.getMagic in someAdd
  695. template isSub(x): untyped = x.getMagic in someSub
  696. template isVal(x): untyped = x.kind in {nkCharLit..nkUInt64Lit}
  697. template isIntVal(x, y): untyped = x.intVal == y
  698. import macros
  699. macro `=~`(x: PNode, pat: untyped): bool =
  700. proc m(x, pat, conds: NimNode) =
  701. case pat.kind
  702. of nnkInfix:
  703. case $pat[0]
  704. of "*": conds.add getAst(isMul(x))
  705. of "/": conds.add getAst(isDiv(x))
  706. of "+": conds.add getAst(isAdd(x))
  707. of "-": conds.add getAst(isSub(x))
  708. else:
  709. error("invalid pattern")
  710. m(newTree(nnkBracketExpr, x, newLit(1)), pat[1], conds)
  711. m(newTree(nnkBracketExpr, x, newLit(2)), pat[2], conds)
  712. of nnkPar:
  713. if pat.len == 1:
  714. m(x, pat[0], conds)
  715. else:
  716. error("invalid pattern")
  717. of nnkIdent:
  718. let c = newTree(nnkStmtListExpr, newLetStmt(pat, x))
  719. conds.add c
  720. # XXX why is this 'isVal(pat)' and not 'isVal(x)'?
  721. if ($pat)[^1] == 'c': c.add(getAst(isVal(x)))
  722. else: c.add bindSym"true"
  723. of nnkIntLit:
  724. conds.add(getAst(isIntVal(x, pat.intVal)))
  725. else:
  726. error("invalid pattern")
  727. var conds = newTree(nnkBracket)
  728. m(x, pat, conds)
  729. when compiles(nestList(ident"and", conds)):
  730. result = nestList(ident"and", conds)
  731. #elif declared(macros.toNimIdent):
  732. # result = nestList(toNimIdent"and", conds)
  733. else:
  734. result = nestList(!"and", conds)
  735. proc isMinusOne(n: PNode): bool =
  736. n.kind in {nkCharLit..nkUInt64Lit} and n.intVal == -1
  737. proc pleViaModel(model: TModel; aa, bb: PNode): TImplication
  738. proc ple(m: TModel; a, b: PNode): TImplication =
  739. template `<=?`(a,b): untyped = ple(m,a,b) == impYes
  740. template `>=?`(a,b): untyped = ple(m, nkIntLit.newIntNode(b), a) == impYes
  741. # 0 <= 3
  742. if a.isValue and b.isValue:
  743. return if leValue(a, b): impYes else: impNo
  744. # use type information too: x <= 4 iff high(x) <= 4
  745. if b.isValue and a.typ != nil and a.typ.isOrdinalType:
  746. if lastOrd(nil, a.typ) <= b.intVal: return impYes
  747. # 3 <= x iff low(x) <= 3
  748. if a.isValue and b.typ != nil and b.typ.isOrdinalType:
  749. if a.intVal <= firstOrd(nil, b.typ): return impYes
  750. # x <= x
  751. if sameTree(a, b): return impYes
  752. # 0 <= x.len
  753. if b.getMagic in someLen and a.isValue:
  754. if a.intVal <= 0: return impYes
  755. # x <= y+c if 0 <= c and x <= y
  756. # x <= y+(-c) if c <= 0 and y >= x
  757. if b.getMagic in someAdd:
  758. if zero() <=? b[2] and a <=? b[1]: return impYes
  759. # x <= y-c if x+c <= y
  760. if b[2] <=? zero() and (canon(m.o.opSub.buildCall(a, b[2]), m.o) <=? b[1]):
  761. return impYes
  762. # x+c <= y if c <= 0 and x <= y
  763. if a.getMagic in someAdd and a[2] <=? zero() and a[1] <=? b: return impYes
  764. # x <= y*c if 1 <= c and x <= y and 0 <= y
  765. if b.getMagic in someMul:
  766. if a <=? b[1] and one() <=? b[2] and zero() <=? b[1]: return impYes
  767. if a.getMagic in someMul and a[2].isValue and a[1].getMagic in someDiv and
  768. a[1][2].isValue:
  769. # simplify (x div 4) * 2 <= y to x div (c div d) <= y
  770. if ple(m, buildCall(m.o.opDiv, a[1][1], `|div|`(a[1][2], a[2])), b) == impYes:
  771. return impYes
  772. # x*3 + x == x*4. It follows that:
  773. # x*3 + y <= x*4 if y <= x and 3 <= 4
  774. if a =~ x*dc + y and b =~ x2*ec:
  775. if sameTree(x, x2):
  776. let ec1 = m.o.opAdd.buildCall(ec, minusOne())
  777. if x >=? 1 and ec >=? 1 and dc >=? 1 and dc <=? ec1 and y <=? x:
  778. return impYes
  779. elif a =~ x*dc and b =~ x2*ec + y:
  780. #echo "BUG cam ehrer e ", a, " <=? ", b
  781. if sameTree(x, x2):
  782. let ec1 = m.o.opAdd.buildCall(ec, minusOne())
  783. if x >=? 1 and ec >=? 1 and dc >=? 1 and dc <=? ec1 and y <=? zero():
  784. return impYes
  785. # x+c <= x+d if c <= d. Same for *, - etc.
  786. if a.getMagic in someBinaryOp and a.getMagic == b.getMagic:
  787. if sameTree(a[1], b[1]) and a[2] <=? b[2]: return impYes
  788. elif sameTree(a[2], b[2]) and a[1] <=? b[1]: return impYes
  789. # x div c <= y if 1 <= c and 0 <= y and x <= y:
  790. if a.getMagic in someDiv:
  791. if one() <=? a[2] and zero() <=? b and a[1] <=? b: return impYes
  792. # x div c <= x div d if d <= c
  793. if b.getMagic in someDiv:
  794. if sameTree(a[1], b[1]) and b[2] <=? a[2]: return impYes
  795. # x div z <= x - 1 if z <= x
  796. if a[2].isValue and b.getMagic in someAdd and b[2].isMinusOne:
  797. if a[2] <=? a[1] and sameTree(a[1], b[1]): return impYes
  798. # slightly subtle:
  799. # x <= max(y, z) iff x <= y or x <= z
  800. # note that 'x <= max(x, z)' is a special case of the above rule
  801. if b.getMagic in someMax:
  802. if a <=? b[1] or a <=? b[2]: return impYes
  803. # min(x, y) <= z iff x <= z or y <= z
  804. if a.getMagic in someMin:
  805. if a[1] <=? b or a[2] <=? b: return impYes
  806. # use the knowledge base:
  807. return pleViaModel(m, a, b)
  808. #return doesImply(m, o.opLe.buildCall(a, b))
  809. type TReplacements = seq[tuple[a, b: PNode]]
  810. proc replaceSubTree(n, x, by: PNode): PNode =
  811. if sameTree(n, x):
  812. result = by
  813. elif hasSubTree(n, x):
  814. result = shallowCopy(n)
  815. for i in 0..n.safeLen-1:
  816. result[i] = replaceSubTree(n[i], x, by)
  817. else:
  818. result = n
  819. proc applyReplacements(n: PNode; rep: TReplacements): PNode =
  820. result = n
  821. for x in rep: result = result.replaceSubTree(x.a, x.b)
  822. proc pleViaModelRec(m: var TModel; a, b: PNode): TImplication =
  823. # now check for inferrable facts: a <= b and b <= c implies a <= c
  824. for i in 0..m.s.high:
  825. let fact = m.s[i]
  826. if fact != nil and fact.getMagic in someLe:
  827. # mark as used:
  828. m.s[i] = nil
  829. # i <= len-100
  830. # i <=? len-1
  831. # --> true if (len-100) <= (len-1)
  832. let x = fact[1]
  833. let y = fact[2]
  834. # x <= y.
  835. # Question: x <= b? True iff y <= b.
  836. if sameTree(x, a):
  837. if ple(m, y, b) == impYes: return impYes
  838. if y.getMagic in someAdd and b.getMagic in someAdd and sameTree(y[1], b[1]):
  839. if ple(m, b[2], y[2]) == impYes:
  840. return impYes
  841. # x <= y implies a <= b if a <= x and y <= b
  842. if ple(m, a, x) == impYes:
  843. if ple(m, y, b) == impYes:
  844. return impYes
  845. #if pleViaModelRec(m, y, b): return impYes
  846. # fact: 16 <= i
  847. # x y
  848. # question: i <= 15? no!
  849. result = impliesLe(fact, a, b)
  850. if result != impUnknown:
  851. return result
  852. when false:
  853. # given: x <= y; y==a; x <= a this means: a <= b if x <= b
  854. if sameTree(y, a):
  855. result = ple(m, b, x)
  856. if result != impUnknown:
  857. return result
  858. proc pleViaModel(model: TModel; aa, bb: PNode): TImplication =
  859. # compute replacements:
  860. var replacements: TReplacements = @[]
  861. for fact in model.s:
  862. if fact != nil and fact.getMagic in someEq:
  863. let a = fact[1]
  864. let b = fact[2]
  865. if a.kind == nkSym: replacements.add((a,b))
  866. else: replacements.add((b,a))
  867. var m: TModel
  868. var a = aa
  869. var b = bb
  870. if replacements.len > 0:
  871. m.s = @[]
  872. m.o = model.o
  873. # make the other facts consistent:
  874. for fact in model.s:
  875. if fact != nil and fact.getMagic notin someEq:
  876. # XXX 'canon' should not be necessary here, but it is
  877. m.s.add applyReplacements(fact, replacements).canon(m.o)
  878. a = applyReplacements(aa, replacements)
  879. b = applyReplacements(bb, replacements)
  880. else:
  881. # we have to make a copy here, because the model will be modified:
  882. m = model
  883. result = pleViaModelRec(m, a, b)
  884. proc proveLe*(m: TModel; a, b: PNode): TImplication =
  885. let x = canon(m.o.opLe.buildCall(a, b), m.o)
  886. #echo "ROOT ", renderTree(x[1]), " <=? ", renderTree(x[2])
  887. result = ple(m, x[1], x[2])
  888. if result == impUnknown:
  889. # try an alternative: a <= b iff not (b < a) iff not (b+1 <= a):
  890. let y = canon(m.o.opLe.buildCall(m.o.opAdd.buildCall(b, one()), a), m.o)
  891. result = ~ple(m, y[1], y[2])
  892. proc addFactLe*(m: var TModel; a, b: PNode) =
  893. m.s.add canon(m.o.opLe.buildCall(a, b), m.o)
  894. proc addFactLt*(m: var TModel; a, b: PNode) =
  895. let bb = m.o.opAdd.buildCall(b, minusOne())
  896. addFactLe(m, a, bb)
  897. proc settype(n: PNode): PType =
  898. result = newType(tySet, n.typ.owner)
  899. addSonSkipIntLit(result, n.typ)
  900. proc buildOf(it, loc: PNode; o: Operators): PNode =
  901. var s = newNodeI(nkCurly, it.info, it.len-1)
  902. s.typ = settype(loc)
  903. for i in 0..<it.len-1: s[i] = it[i]
  904. result = newNodeI(nkCall, it.info, 3)
  905. result[0] = newSymNode(o.opContains)
  906. result[1] = s
  907. result[2] = loc
  908. proc buildElse(n: PNode; o: Operators): PNode =
  909. var s = newNodeIT(nkCurly, n.info, settype(n[0]))
  910. for i in 1..<n.len-1:
  911. let branch = n[i]
  912. assert branch.kind != nkElse
  913. if branch.kind == nkOfBranch:
  914. for j in 0..<branch.len-1:
  915. s.add(branch[j])
  916. result = newNodeI(nkCall, n.info, 3)
  917. result[0] = newSymNode(o.opContains)
  918. result[1] = s
  919. result[2] = n[0]
  920. proc addDiscriminantFact*(m: var TModel, n: PNode) =
  921. var fact = newNodeI(nkCall, n.info, 3)
  922. fact[0] = newSymNode(m.o.opEq)
  923. fact[1] = n[0]
  924. fact[2] = n[1]
  925. m.s.add fact
  926. proc addAsgnFact*(m: var TModel, key, value: PNode) =
  927. var fact = newNodeI(nkCall, key.info, 3)
  928. fact[0] = newSymNode(m.o.opEq)
  929. fact[1] = key
  930. fact[2] = value
  931. m.s.add fact
  932. proc sameSubexprs*(m: TModel; a, b: PNode): bool =
  933. # This should be used to check whether two *path expressions* refer to the
  934. # same memory location according to 'm'. This is tricky:
  935. # lock a[i].guard:
  936. # ...
  937. # access a[i].guarded
  938. #
  939. # Here a[i] is the same as a[i] iff 'i' and 'a' are not changed via '...'.
  940. # However, nil checking requires exactly the same mechanism! But for now
  941. # we simply use sameTree and live with the unsoundness of the analysis.
  942. var check = newNodeI(nkCall, a.info, 3)
  943. check[0] = newSymNode(m.o.opEq)
  944. check[1] = a
  945. check[2] = b
  946. result = m.doesImply(check) == impYes
  947. proc addCaseBranchFacts*(m: var TModel, n: PNode, i: int) =
  948. let branch = n[i]
  949. if branch.kind == nkOfBranch:
  950. m.s.add buildOf(branch, n[0], m.o)
  951. else:
  952. m.s.add n.buildElse(m.o).neg(m.o)
  953. proc buildProperFieldCheck(access, check: PNode; o: Operators): PNode =
  954. if check[1].kind == nkCurly:
  955. result = copyTree(check)
  956. if access.kind == nkDotExpr:
  957. var a = copyTree(access)
  958. a[1] = check[2]
  959. result[2] = a
  960. # 'access.kind != nkDotExpr' can happen for object constructors
  961. # which we don't check yet
  962. else:
  963. # it is some 'not'
  964. assert check.getMagic == mNot
  965. result = buildProperFieldCheck(access, check[1], o).neg(o)
  966. proc checkFieldAccess*(m: TModel, n: PNode; conf: ConfigRef) =
  967. for i in 1..<n.len:
  968. let check = buildProperFieldCheck(n[0], n[i], m.o)
  969. if check != nil and m.doesImply(check) != impYes:
  970. message(conf, n.info, warnProveField, renderTree(n[0])); break